-
Notifications
You must be signed in to change notification settings - Fork 60
/
Copy pathCacheController.cs
71 lines (57 loc) · 3.5 KB
/
CacheController.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
/*
* There are multiple ways to cache data in ASP.NET Core which are used in Orchard Core as well. For example the
* content items are cached using the IMemoryCache service. In-memory caching holds the copy of data in local server’s
* memory which could be enough in most cases. Some features, however, require some extra functionality and more
* complex ways of caching such as shapes. In Orchard Core there is a so-called Dynamic Cache which is built upon the
* Distributed Cache. Distributed cache maintains cache in a centralized location which is accessible by servers in a
* cluster. In Orchard Core this is also memory cache but opens up the possibility to use Redis for example. In this
* tutorial we will see a couple of examples of caching a DateTime object and shape caching will also be demonstrated.
*/
using Lombiq.TrainingDemo.Services;
using Lombiq.TrainingDemo.ViewModels;
using Microsoft.AspNetCore.Mvc;
using System.Threading.Tasks;
namespace Lombiq.TrainingDemo.Controllers;
public sealed class CacheController : Controller
{
// The actual caching is implemented in a service which we'll soon investigate.
private readonly IDateTimeCachingService _dateTimeCachingService;
public CacheController(IDateTimeCachingService dateTimeCachingService) => _dateTimeCachingService = dateTimeCachingService;
// In this action we'll cache a DateTime three different ways. You can open it under
// /Lombiq.TrainingDemo/Cache/Index
public async Task<ActionResult> Index()
{
// This one will be cached using the built-in ASP.NET Core IMemoryCache.
var memoryCachedDateTime = await _dateTimeCachingService.GetMemoryCachedDateTimeAsync();
// This one will be using the DynamicCache provided by Orchard Core. It will have a 30 second expiration.
var dynamicCachedDateTimeWith30SecondsExpiry =
await _dateTimeCachingService.GetDynamicCachedDateTimeWith30SecondsExpiryAsync();
// Finally this date will be cached only for this route.
var dynamicCachedDateTimeVariedByRoutes =
await _dateTimeCachingService.GetDynamicCachedDateTimeVariedByRoutesAsync();
// NEXT STATION: Services/DateTimeCachingService.cs
return View("Index", new CacheViewModel
{
MemoryCachedDateTime = memoryCachedDateTime,
DynamicCachedDateTimeWith30SecondsExpiry = dynamicCachedDateTimeWith30SecondsExpiry,
DynamicCachedDateTimeVariedByRoutes = dynamicCachedDateTimeVariedByRoutes,
});
}
// This action will result in the same page as Index, however, the route will be different so the route-specific
// cache can be tested. You can open it under /Lombiq.TrainingDemo/Cache/DifferentRoute
public Task<ActionResult> DifferentRoute() =>
Index();
// This action will invalidate the memory cache and all the route-specific caches. Calling this action will redirect
// back to the Index page and you will notice that the date value is updated. You can open it under
// /Lombiq.TrainingDemo/Cache/InvalidateDateTimeCache
public async Task<ActionResult> InvalidateDateTimeCache()
{
await _dateTimeCachingService.InvalidateCachedDateTimeAsync();
return RedirectToAction("Index");
}
// Now let's see how Orchard Core utilizes the Dynamic Cache the most. In this action you will see how shapes are
// cached. You can open it under /Lombiq.TrainingDemo/Cache/Shape
public ActionResult Shape() =>
View();
// NEXT STATION: Views/Cache/Shape.cshtml
}