Skip to content

Commit 5b65ea0

Browse files
committed
task: tidy up the sample app & fix default file loading
1 parent 8260013 commit 5b65ea0

File tree

8 files changed

+45
-56
lines changed

8 files changed

+45
-56
lines changed

CacheDatabaseQueriesApiSample/Controllers/DbTimeController.cs

+3-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using System;
2-
using System.Threading.Tasks;
32
using LazyCache;
43
using Microsoft.AspNetCore.Mvc;
54

@@ -21,11 +20,11 @@ public DbTimeController(DbTimeContext context)
2120

2221
[HttpGet]
2322
[Route("api/dbtime")]
24-
public DbTime Get()
23+
public DbTimeEntity Get()
2524
{
26-
Func<DbTime> cacheableAsyncFunc = () => dbContext.GeDbTime();
25+
Func<DbTimeEntity> actionThatWeWantToCache = () => dbContext.GeDbTime();
2726

28-
var cachedDatabaseTime = cache.GetOrAdd(cacheKey, cacheableAsyncFunc);
27+
var cachedDatabaseTime = cache.GetOrAdd(cacheKey, actionThatWeWantToCache);
2928

3029
return cachedDatabaseTime;
3130
}

CacheDatabaseQueriesApiSample/DbTime.cs

-23
This file was deleted.

CacheDatabaseQueriesApiSample/DbTimeContext.cs

+5-3
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,19 @@ public DbTimeContext(DbContextOptions<DbTimeContext> options)
1212
{
1313
}
1414

15-
public virtual DbSet<DbTime> Times { get; set; }
15+
// simulate a table in the database so we can get just one row with the current time
16+
private DbSet<DbTimeEntity> Times { get; set; }
1617

1718
public static int DatabaseRequestCounter()
1819
{
1920
return databaseRequestCounter;
2021
}
2122

22-
public DbTime GeDbTime()
23+
public DbTimeEntity GeDbTime()
2324
{
2425
// 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+
var result = Times
27+
.FromSql("WAITFOR DELAY '00:00:00:500'; SELECT 1 as [ID], GETDATE() as [TimeNowInTheDatabase]")
2628
.Single();
2729

2830
databaseRequestCounter++;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
using System;
2+
3+
namespace CacheDatabaseQueriesApiSample
4+
{
5+
/// <summary>
6+
/// Simulates loading a record from a table, but really just gets the current datatime from the database
7+
/// </summary>
8+
public class DbTimeEntity
9+
{
10+
public DbTimeEntity(DateTime now)
11+
{
12+
TimeNowInTheDatabase = now;
13+
}
14+
15+
public DbTimeEntity()
16+
{
17+
}
18+
19+
public virtual int id { get; set; }
20+
21+
public virtual DateTime TimeNowInTheDatabase { get; set; }
22+
}
23+
}
+6-11
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,5 @@
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;
1+
using Microsoft.AspNetCore;
72
using Microsoft.AspNetCore.Hosting;
8-
using Microsoft.Extensions.Configuration;
9-
using Microsoft.Extensions.Logging;
103

114
namespace CacheDatabaseQueriesApiSample
125
{
@@ -17,9 +10,11 @@ public static void Main(string[] args)
1710
BuildWebHost(args).Run();
1811
}
1912

20-
public static IWebHost BuildWebHost(string[] args) =>
21-
WebHost.CreateDefaultBuilder(args)
13+
public static IWebHost BuildWebHost(string[] args)
14+
{
15+
return WebHost.CreateDefaultBuilder(args)
2216
.UseStartup<Startup>()
2317
.Build();
18+
}
2419
}
25-
}
20+
}

CacheDatabaseQueriesApiSample/Properties/launchSettings.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,15 @@
1111
"IIS Express": {
1212
"commandName": "IISExpress",
1313
"launchBrowser": true,
14-
"launchUrl": "index.html",
14+
"launchUrl": "/",
1515
"environmentVariables": {
1616
"ASPNETCORE_ENVIRONMENT": "Development"
1717
}
1818
},
1919
"CacheDatabaseQueriesApiSample": {
2020
"commandName": "Project",
2121
"launchBrowser": true,
22-
"launchUrl": "index.html",
22+
"launchUrl": "/",
2323
"environmentVariables": {
2424
"ASPNETCORE_ENVIRONMENT": "Development"
2525
},
+5-12
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,8 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.Linq;
4-
using System.Threading.Tasks;
5-
using Microsoft.AspNetCore.Builder;
1+
using Microsoft.AspNetCore.Builder;
62
using Microsoft.AspNetCore.Hosting;
73
using Microsoft.EntityFrameworkCore;
84
using Microsoft.Extensions.Configuration;
95
using Microsoft.Extensions.DependencyInjection;
10-
using Microsoft.Extensions.Logging;
11-
using Microsoft.Extensions.Options;
126

137
namespace CacheDatabaseQueriesApiSample
148
{
@@ -25,21 +19,20 @@ public Startup(IConfiguration configuration)
2519
public void ConfigureServices(IServiceCollection services)
2620
{
2721
services.AddMvc();
28-
var connection = @"Server=(localdb)\projectsv13;Database=Master;Trusted_Connection=True;ConnectRetryCount=0";
22+
var connection =
23+
@"Server=(localdb)\projectsv13;Database=Master;Trusted_Connection=True;ConnectRetryCount=0";
2924
services.AddDbContext<DbTimeContext>(options => options.UseSqlServer(connection));
3025
}
3126

3227
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
3328
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
3429
{
3530
if (env.IsDevelopment())
36-
{
3731
app.UseDeveloperExceptionPage();
38-
}
3932

40-
app.UseStaticFiles();
4133
app.UseDefaultFiles();
34+
app.UseStaticFiles();
4235
app.UseMvc();
4336
}
4437
}
45-
}
38+
}

CacheDatabaseQueriesApiSample/wwwroot/index.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
<div class="centre-box">
1818
<div class="counter pull-right"><span class="counter-val">0</span> Database query(s)</div>
1919

20-
<h1>Sample app to demonstrate using an async cache in your API to save database SQL queries and speed up API calls</h1>
20+
<h1>Sample app to demonstrate using an cache in your API to save database SQL queries and speed up API calls</h1>
2121

2222
<p>
2323
Every 3 seconds we fetch the current time from the database, however because the sql query

0 commit comments

Comments
 (0)