Introduction to ICacheManager
ICacheManager is a interface in namespace Aaron.Core.Caching; it use ObjectCache that's in namespace System.Runtime.Caching to cache any object. This action is useful for data loading, means it saves the page load time.Easy to use, you just do as follows:
- Declare a field with private scope; and then, dependents it in constructor.
publicclass ArticleService : IArticleService { privatereadonly ICacheManager _cache; public ArticleService(ICacheManager cache) { _cache = cache; } }
- Use a method named Get to cache
public IList<Article> GetAllArticles() { return _cache.Get("key_of_cache", () => { // Write something here... }); }
- string key: it's a key to identify a value in ObjectCache.
- Func<T> acquire: it's a delegate to encapsulates a method and returns a T object.
publicstatic T Get<T>(this ICacheManager cacheManager, string key, Func<T> acquire)