public interface ICacheProvider { void RemoveFromCache(string key); T GetFromMethod<T>(string cacheKey, TimeSpan cacheDuration, Func<T> method); T GetFromMethod<T>(string cacheKey, Func<T> method); }
public class AzureCacheProvider : ICacheProvider { // private readonly string _prefix = StaticHelper.GetCurrentVersion(); private readonly object _locker = new object(); private readonly DataCache _cache; public AzureCacheProvider() { lock (_locker) { { var factory = new DataCacheFactory(); _cache = factory.GetDefaultCache(); } } } private void RemoveFromCache(string key, DataCache cache) { lock (_locker) { cache.Remove(_prefix + key); HttpContext.Current.Items.Remove(_prefix + key); } } private T GetFromCache<T>(string key, TimeSpan expiration, Func<T> method, DataCache cache) { object cacheItem = HttpContext.Current.Items[_prefix + key]; if (cacheItem == null) { cacheItem = cache.Get(_prefix + key); if (cacheItem != null) HttpContext.Current.Items[_prefix + key] = cacheItem; } if (cacheItem == null) { lock (_locker) { cacheItem = cache.Get(_prefix + key); if (cacheItem == null) { cacheItem = method(); if (cacheItem != null) { HttpContext.Current.Items[_prefix + key] = cacheItem; cache.Put(_prefix + key, cacheItem, expiration); } } } } return (T)cacheItem; } public void RemoveFromCache(string key) { RemoveFromCache(key, _cache); } public T GetFromMethod<T>(string cacheKey, TimeSpan expirationSeconds, Func<T> method) { return GetFromCache(cacheKey, expirationSeconds, method, _cache); } public T GetFromMethod<T>(string cacheKey, Func<T> method) { return GetFromMethod(cacheKey, TimeSpan.FromMinutes(15), method); } }
HttpContext.Current.Items
here is used to store in memory already received in the current query objects from the cache, in case of repeated access to data.configSections
, then it must be added. Then put the following code inside: <section name="dataCacheClients" type="Microsoft.ApplicationServer.Caching.DataCacheClientsSection, Microsoft.ApplicationServer.Caching.Core" allowLocation="true" allowDefinition="Everywhere"/>
dataCacheClients
section: <dataCacheClients> <tracing sinkType="DiagnosticSink" traceLevel="Error" /> <dataCacheClient name="default"> <autoDiscover isEnabled="true" identifier=" Web-, " /> </dataCacheClient> </dataCacheClients>
<h1>Cache test</h1> Before: <%=DateTime.Now.Millisecond%> ms<br/> <%=Facade.Instance.Cache.GetFromMethod("currentTime", () => DateTime.Now)%><br/> After: <%=DateTime.Now.Millisecond%> ms<br/> <%using(Html.BeginForm("ClearCache","Admin"))%> <%{%> <input type="submit" value="Clear Cache"/> <%}%>
Facade.Instance.Cache
in the code above is an instance of an AzureCacheProvider
object obtained using IoC.ClearCache
action looks quite simple: public ActionResult ClearCache() { Facade.Instance.Cache.RemoveFromCache("currentTime"); return View("admin"); }
Cache test
Before: 553 ms
6/10/2012 11:09:40 AM
After: 569 ms
Cache test
Before: 508 ms
6/10/2012 11:09:40 AM
After: 508 ms
dataCacheClients
section with: <dataCacheClients> <dataCacheClient name="default"> <hosts> <host name="[ ID].cache.windows.net" cachePort="22233" /> </hosts> <securityProperties mode="Message"> <messageSecurity authorizationInfo="[ ]"></messageSecurity> </securityProperties> </dataCacheClient> </dataCacheClients>
Cache test
Before: 521 ms
6/10/2012 11:09:40 AM
After: 550 ms
Cache test
Before: 501 ms
6/10/2012 11:09:40 AM
After: 533 ms
Source: https://habr.com/ru/post/145800/
All Articles