File tree 8 files changed +45
-56
lines changed
CacheDatabaseQueriesApiSample
8 files changed +45
-56
lines changed Original file line number Diff line number Diff line change 1
1
using System ;
2
- using System . Threading . Tasks ;
3
2
using LazyCache ;
4
3
using Microsoft . AspNetCore . Mvc ;
5
4
@@ -21,11 +20,11 @@ public DbTimeController(DbTimeContext context)
21
20
22
21
[ HttpGet ]
23
22
[ Route ( "api/dbtime" ) ]
24
- public DbTime Get ( )
23
+ public DbTimeEntity Get ( )
25
24
{
26
- Func < DbTime > cacheableAsyncFunc = ( ) => dbContext . GeDbTime ( ) ;
25
+ Func < DbTimeEntity > actionThatWeWantToCache = ( ) => dbContext . GeDbTime ( ) ;
27
26
28
- var cachedDatabaseTime = cache . GetOrAdd ( cacheKey , cacheableAsyncFunc ) ;
27
+ var cachedDatabaseTime = cache . GetOrAdd ( cacheKey , actionThatWeWantToCache ) ;
29
28
30
29
return cachedDatabaseTime ;
31
30
}
Load Diff This file was deleted.
Original file line number Diff line number Diff line change @@ -12,17 +12,19 @@ public DbTimeContext(DbContextOptions<DbTimeContext> options)
12
12
{
13
13
}
14
14
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 ; }
16
17
17
18
public static int DatabaseRequestCounter ( )
18
19
{
19
20
return databaseRequestCounter ;
20
21
}
21
22
22
- public DbTime GeDbTime ( )
23
+ public DbTimeEntity GeDbTime ( )
23
24
{
24
25
// 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]" )
26
28
. Single ( ) ;
27
29
28
30
databaseRequestCounter ++ ;
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change 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 ;
7
2
using Microsoft . AspNetCore . Hosting ;
8
- using Microsoft . Extensions . Configuration ;
9
- using Microsoft . Extensions . Logging ;
10
3
11
4
namespace CacheDatabaseQueriesApiSample
12
5
{
@@ -17,9 +10,11 @@ public static void Main(string[] args)
17
10
BuildWebHost ( args ) . Run ( ) ;
18
11
}
19
12
20
- public static IWebHost BuildWebHost ( string [ ] args ) =>
21
- WebHost . CreateDefaultBuilder ( args )
13
+ public static IWebHost BuildWebHost ( string [ ] args )
14
+ {
15
+ return WebHost . CreateDefaultBuilder ( args )
22
16
. UseStartup < Startup > ( )
23
17
. Build ( ) ;
18
+ }
24
19
}
25
- }
20
+ }
Original file line number Diff line number Diff line change 11
11
"IIS Express" : {
12
12
"commandName" : " IISExpress" ,
13
13
"launchBrowser" : true ,
14
- "launchUrl" : " index.html " ,
14
+ "launchUrl" : " / " ,
15
15
"environmentVariables" : {
16
16
"ASPNETCORE_ENVIRONMENT" : " Development"
17
17
}
18
18
},
19
19
"CacheDatabaseQueriesApiSample" : {
20
20
"commandName" : " Project" ,
21
21
"launchBrowser" : true ,
22
- "launchUrl" : " index.html " ,
22
+ "launchUrl" : " / " ,
23
23
"environmentVariables" : {
24
24
"ASPNETCORE_ENVIRONMENT" : " Development"
25
25
},
Original file line number Diff line number Diff line change 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 ;
6
2
using Microsoft . AspNetCore . Hosting ;
7
3
using Microsoft . EntityFrameworkCore ;
8
4
using Microsoft . Extensions . Configuration ;
9
5
using Microsoft . Extensions . DependencyInjection ;
10
- using Microsoft . Extensions . Logging ;
11
- using Microsoft . Extensions . Options ;
12
6
13
7
namespace CacheDatabaseQueriesApiSample
14
8
{
@@ -25,21 +19,20 @@ public Startup(IConfiguration configuration)
25
19
public void ConfigureServices ( IServiceCollection services )
26
20
{
27
21
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" ;
29
24
services . AddDbContext < DbTimeContext > ( options => options . UseSqlServer ( connection ) ) ;
30
25
}
31
26
32
27
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
33
28
public void Configure ( IApplicationBuilder app , IHostingEnvironment env )
34
29
{
35
30
if ( env . IsDevelopment ( ) )
36
- {
37
31
app . UseDeveloperExceptionPage ( ) ;
38
- }
39
32
40
- app . UseStaticFiles ( ) ;
41
33
app . UseDefaultFiles ( ) ;
34
+ app . UseStaticFiles ( ) ;
42
35
app . UseMvc ( ) ;
43
36
}
44
37
}
45
- }
38
+ }
Original file line number Diff line number Diff line change 17
17
< div class ="centre-box ">
18
18
< div class ="counter pull-right "> < span class ="counter-val "> 0</ span > Database query(s)</ div >
19
19
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 >
21
21
22
22
< p >
23
23
Every 3 seconds we fetch the current time from the database, however because the sql query
You can’t perform that action at this time.
0 commit comments