Skip to content

Commit 4ba893b

Browse files
committed
feat: porting sample app. caching not working
1 parent 29a7683 commit 4ba893b

25 files changed

+255
-467
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<Project Sdk="Microsoft.NET.Sdk.Web">
2+
3+
<PropertyGroup>
4+
<TargetFramework>netcoreapp2.0</TargetFramework>
5+
</PropertyGroup>
6+
7+
<ItemGroup>
8+
<PackageReference Include="Microsoft.AspNetCore.All" Version="2.0.5" />
9+
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="2.0.1" />
10+
</ItemGroup>
11+
12+
<ItemGroup>
13+
<DotNetCliToolReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Tools" Version="2.0.2" />
14+
</ItemGroup>
15+
16+
<ItemGroup>
17+
<ProjectReference Include="..\LazyCache\LazyCache.csproj" />
18+
</ItemGroup>
19+
20+
</Project>

Samples/ApiAsyncCachingSample/Controllers/DbQueriesController.cs renamed to CacheDatabaseQueriesApiSample/Controllers/DbQueriesController.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
using System.Web.Http;
2-
using ApiAsyncCachingSample.Models;
1+
using CacheDatabaseQueriesApiSample;
2+
using Microsoft.AspNetCore.Mvc;
33

44
namespace ApiAsyncCachingSample.Controllers
55
{
6-
public class DbQueriesController : ApiController
6+
public class DbQueriesController : Controller
77
{
88
[HttpGet]
99
[Route("api/dbQueries")]
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
using System;
2+
using System.Threading.Tasks;
3+
using LazyCache;
4+
using Microsoft.AspNetCore.Mvc;
5+
6+
namespace CacheDatabaseQueriesApiSample.Controllers
7+
{
8+
public class DbTimeController : Controller
9+
{
10+
private readonly IAppCache cache;
11+
private readonly string cacheKey = "DbTimeController.Get";
12+
private readonly DbTimeContext dbContext;
13+
14+
15+
public DbTimeController(DbTimeContext context)
16+
{
17+
// this could (and should) be passed in using dependency injection
18+
cache = new CachingService();
19+
dbContext = context;
20+
}
21+
22+
[HttpGet]
23+
[Route("api/dbtime")]
24+
public DbTime Get()
25+
{
26+
Func<DbTime> cacheableAsyncFunc = () => dbContext.GeDbTime();
27+
28+
var cachedDatabaseTime = cache.GetOrAdd(cacheKey, cacheableAsyncFunc);
29+
30+
return cachedDatabaseTime;
31+
}
32+
33+
[HttpDelete]
34+
[Route("api/dbtime")]
35+
public IActionResult DeleteFromCache()
36+
{
37+
cache.Remove(cacheKey);
38+
var friendlyMessage = new {Message = $"Item with key '{cacheKey}' removed from server in-memory cache"};
39+
return Ok(friendlyMessage);
40+
}
41+
}
42+
}

Samples/ApiAsyncCachingSample/Models/DbTime.cs renamed to CacheDatabaseQueriesApiSample/DbTime.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
using System;
22

3-
namespace ApiAsyncCachingSample.Models
3+
namespace CacheDatabaseQueriesApiSample
44
{
55
public class DbTime
66
{
@@ -14,7 +14,9 @@ public DbTime()
1414

1515
}
1616

17-
public DateTime TimeNowInTheDatabase { get; set; }
17+
public virtual int id { get; set; }
18+
19+
public virtual DateTime TimeNowInTheDatabase { get; set; }
1820

1921

2022
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
using System.Linq;
2+
using Microsoft.EntityFrameworkCore;
3+
4+
namespace CacheDatabaseQueriesApiSample
5+
{
6+
public class DbTimeContext : DbContext
7+
{
8+
private static int databaseRequestCounter; //just for demo - don't use static fields for statistics!
9+
10+
public DbTimeContext(DbContextOptions<DbTimeContext> options)
11+
: base(options)
12+
{
13+
}
14+
15+
public virtual DbSet<DbTime> Times { get; set; }
16+
17+
public static int DatabaseRequestCounter()
18+
{
19+
return databaseRequestCounter;
20+
}
21+
22+
public DbTime GeDbTime()
23+
{
24+
// get the current time from SQL server right now asynchronously (simulating a slow query)
25+
var result = Times.FromSql("WAITFOR DELAY '00:00:00:500'; SELECT 1 as ID, GETDATE() as [TimeNowInTheDatabase]")
26+
.Single();
27+
28+
databaseRequestCounter++;
29+
30+
return result;
31+
}
32+
}
33+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.IO;
4+
using System.Linq;
5+
using System.Threading.Tasks;
6+
using Microsoft.AspNetCore;
7+
using Microsoft.AspNetCore.Hosting;
8+
using Microsoft.Extensions.Configuration;
9+
using Microsoft.Extensions.Logging;
10+
11+
namespace CacheDatabaseQueriesApiSample
12+
{
13+
public class Program
14+
{
15+
public static void Main(string[] args)
16+
{
17+
BuildWebHost(args).Run();
18+
}
19+
20+
public static IWebHost BuildWebHost(string[] args) =>
21+
WebHost.CreateDefaultBuilder(args)
22+
.UseStartup<Startup>()
23+
.Build();
24+
}
25+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
{
2+
"iisSettings": {
3+
"windowsAuthentication": false,
4+
"anonymousAuthentication": true,
5+
"iisExpress": {
6+
"applicationUrl": "http://localhost:52671/",
7+
"sslPort": 0
8+
}
9+
},
10+
"profiles": {
11+
"IIS Express": {
12+
"commandName": "IISExpress",
13+
"launchBrowser": true,
14+
"launchUrl": "index.html",
15+
"environmentVariables": {
16+
"ASPNETCORE_ENVIRONMENT": "Development"
17+
}
18+
},
19+
"CacheDatabaseQueriesApiSample": {
20+
"commandName": "Project",
21+
"launchBrowser": true,
22+
"launchUrl": "index.html",
23+
"environmentVariables": {
24+
"ASPNETCORE_ENVIRONMENT": "Development"
25+
},
26+
"applicationUrl": "http://localhost:52672/"
27+
}
28+
}
29+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Threading.Tasks;
5+
using Microsoft.AspNetCore.Builder;
6+
using Microsoft.AspNetCore.Hosting;
7+
using Microsoft.EntityFrameworkCore;
8+
using Microsoft.Extensions.Configuration;
9+
using Microsoft.Extensions.DependencyInjection;
10+
using Microsoft.Extensions.Logging;
11+
using Microsoft.Extensions.Options;
12+
13+
namespace CacheDatabaseQueriesApiSample
14+
{
15+
public class Startup
16+
{
17+
public Startup(IConfiguration configuration)
18+
{
19+
Configuration = configuration;
20+
}
21+
22+
public IConfiguration Configuration { get; }
23+
24+
// This method gets called by the runtime. Use this method to add services to the container.
25+
public void ConfigureServices(IServiceCollection services)
26+
{
27+
services.AddMvc();
28+
var connection = @"Server=(localdb)\projectsv13;Database=Master;Trusted_Connection=True;ConnectRetryCount=0";
29+
services.AddDbContext<DbTimeContext>(options => options.UseSqlServer(connection));
30+
}
31+
32+
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
33+
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
34+
{
35+
if (env.IsDevelopment())
36+
{
37+
app.UseDeveloperExceptionPage();
38+
}
39+
40+
app.UseStaticFiles();
41+
app.UseDefaultFiles();
42+
app.UseMvc();
43+
}
44+
}
45+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"Logging": {
3+
"IncludeScopes": false,
4+
"LogLevel": {
5+
"Default": "Debug",
6+
"System": "Information",
7+
"Microsoft": "Information"
8+
}
9+
}
10+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"Logging": {
3+
"IncludeScopes": false,
4+
"Debug": {
5+
"LogLevel": {
6+
"Default": "Warning"
7+
}
8+
},
9+
"Console": {
10+
"LogLevel": {
11+
"Default": "Warning"
12+
}
13+
}
14+
}
15+
}

Samples/ApiAsyncCachingSample/index.html renamed to CacheDatabaseQueriesApiSample/wwwroot/index.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ <h1>Sample app to demonstrate using an async cache in your API to save database
7070
},
7171

7272
updateLogWithTime: function (dbTime, duration) {
73-
var message = "API reports that the time now in the database is " + dbTime.TimeNowInTheDatabase + " (" + duration + "ms)";
73+
var message = "API reports that the time now in the database is " + dbTime.timeNowInTheDatabase + " (" + duration + "ms)";
7474
app.log(message);
7575
},
7676

@@ -80,7 +80,7 @@ <h1>Sample app to demonstrate using an async cache in your API to save database
8080
url: "/api/dbtime"
8181
})
8282
.done(function (data) {
83-
app.log(data.Message);
83+
app.log(data.message);
8484
});
8585
},
8686

LazyCache.sln

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,23 @@
11

22
Microsoft Visual Studio Solution File, Format Version 12.00
3-
# Visual Studio 14
4-
VisualStudioVersion = 14.0.25420.1
3+
# Visual Studio 15
4+
VisualStudioVersion = 15.0.27130.2024
55
MinimumVisualStudioVersion = 10.0.40219.1
6-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "LazyCache", "LazyCache\LazyCache.csproj", "{E6A1EF20-94AD-4A1C-9A89-3B2FA8AD8EC7}"
6+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "LazyCache", "LazyCache\LazyCache.csproj", "{E6A1EF20-94AD-4A1C-9A89-3B2FA8AD8EC7}"
77
EndProject
8-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "LazyCache.UnitTests", "LazyCache.UnitTests\LazyCache.UnitTests.csproj", "{7F6C8799-CA9E-43BD-AE97-72C31BB4E106}"
8+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "LazyCache.UnitTests", "LazyCache.UnitTests\LazyCache.UnitTests.csproj", "{7F6C8799-CA9E-43BD-AE97-72C31BB4E106}"
99
EndProject
1010
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "info", "info", "{81C0E096-59B7-4129-851B-8183FDB9B02B}"
1111
ProjectSection(SolutionItems) = preProject
12+
appveyor.yml = appveyor.yml
13+
build.ps1 = build.ps1
1214
Readme.md = Readme.md
1315
ReleaseNotes.md = ReleaseNotes.md
1416
EndProjectSection
1517
EndProject
1618
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Samples", "Samples", "{335BA426-C839-4996-8476-F3EE4056C40E}"
1719
EndProject
18-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ApiAsyncCachingSample", "Samples\ApiAsyncCachingSample\ApiAsyncCachingSample.csproj", "{FB8EDC6D-CD41-47AA-9758-D8D796F7728C}"
20+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CacheDatabaseQueriesApiSample", "CacheDatabaseQueriesApiSample\CacheDatabaseQueriesApiSample.csproj", "{5D6A88DD-230C-4057-B8EB-A987FF4F29DB}"
1921
EndProject
2022
Global
2123
GlobalSection(SolutionConfigurationPlatforms) = preSolution
@@ -31,15 +33,18 @@ Global
3133
{7F6C8799-CA9E-43BD-AE97-72C31BB4E106}.Debug|Any CPU.Build.0 = Debug|Any CPU
3234
{7F6C8799-CA9E-43BD-AE97-72C31BB4E106}.Release|Any CPU.ActiveCfg = Release|Any CPU
3335
{7F6C8799-CA9E-43BD-AE97-72C31BB4E106}.Release|Any CPU.Build.0 = Release|Any CPU
34-
{FB8EDC6D-CD41-47AA-9758-D8D796F7728C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
35-
{FB8EDC6D-CD41-47AA-9758-D8D796F7728C}.Debug|Any CPU.Build.0 = Debug|Any CPU
36-
{FB8EDC6D-CD41-47AA-9758-D8D796F7728C}.Release|Any CPU.ActiveCfg = Release|Any CPU
37-
{FB8EDC6D-CD41-47AA-9758-D8D796F7728C}.Release|Any CPU.Build.0 = Release|Any CPU
36+
{5D6A88DD-230C-4057-B8EB-A987FF4F29DB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
37+
{5D6A88DD-230C-4057-B8EB-A987FF4F29DB}.Debug|Any CPU.Build.0 = Debug|Any CPU
38+
{5D6A88DD-230C-4057-B8EB-A987FF4F29DB}.Release|Any CPU.ActiveCfg = Release|Any CPU
39+
{5D6A88DD-230C-4057-B8EB-A987FF4F29DB}.Release|Any CPU.Build.0 = Release|Any CPU
3840
EndGlobalSection
3941
GlobalSection(SolutionProperties) = preSolution
4042
HideSolutionNode = FALSE
4143
EndGlobalSection
4244
GlobalSection(NestedProjects) = preSolution
43-
{FB8EDC6D-CD41-47AA-9758-D8D796F7728C} = {335BA426-C839-4996-8476-F3EE4056C40E}
45+
{5D6A88DD-230C-4057-B8EB-A987FF4F29DB} = {335BA426-C839-4996-8476-F3EE4056C40E}
46+
EndGlobalSection
47+
GlobalSection(ExtensibilityGlobals) = postSolution
48+
SolutionGuid = {5040E431-0FAA-4DC7-A678-D218CD57D542}
4449
EndGlobalSection
4550
EndGlobal

LazyCache/LazyCache.csproj

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,18 @@
22

33
<PropertyGroup>
44
<TargetFramework>netstandard2.0</TargetFramework>
5+
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
6+
<Version>2.0.0</Version>
7+
<Authors>https://github.com/alastairtree</Authors>
8+
<Company>https://github.com/alastairtree</Company>
9+
<Description>Lazy cache is a simple, thread safe in-memory caching service</Description>
10+
<PackageProjectUrl>https://github.com/alastairtree/LazyCache</PackageProjectUrl>
11+
<Copyright>Copyright 2014 - 2018 Alastair Crabtree</Copyright>
12+
<PackageLicenseUrl>https://github.com/alastairtree/LazyCache/blob/master/LICENSE</PackageLicenseUrl>
13+
<PackageIconUrl>https://raw.githubusercontent.com/alastairtree/LazyCache/master/artwork/logo-128.png</PackageIconUrl>
14+
<RepositoryUrl>https://github.com/alastairtree/LazyCache</RepositoryUrl>
15+
<PackageTags>Caching Performance Speed In-memory ObjectCache Generics ServiceCacheing Lazy Cache Lazy-Load MemoryCache CachingService AppCache ApplicationCache Memcached</PackageTags>
16+
<PackageReleaseNotes>See https://raw.githubusercontent.com/alastairtree/LazyCache/master/ReleaseNotes.md</PackageReleaseNotes>
517
</PropertyGroup>
618

719
<ItemGroup>

LazyCache/LazyCache.nuspec

Lines changed: 0 additions & 19 deletions
This file was deleted.

0 commit comments

Comments
 (0)