标签:
本框架支持缓存管理,内部机制使用开源库CacheManager。支持全局缓存、租户缓存,默认使用的系统缓存实现,可以在Web.config将其配置为其他缓存类型,比如支持Redis、内存等。
开源库地址:https://github.com/MichaCo/CacheManager
默认配置如下,见Web.config cacheManager元素内容:
<!--缓存配置--> <cacheManager xmlns="http://tempuri.org/CacheManagerCfg.xsd"> <managers> <cache name="defaultCache" updateMode="Up" enableStatistics="true" enablePerformanceCounters="true"> <handle ref="sysCache" name="defaultHandle" expirationMode="Absolute" timeout="3600s"/> </cache> </managers> <cacheHandles> <handleDef id="sysCache" type="CacheManager.SystemRuntimeCaching.MemoryCacheHandle`1, CacheManager.SystemRuntimeCaching" /> </cacheHandles> </cacheManager>
相关配置说明请参考:http://cachemanager.net/Documentation/Index/cachemanager_configuration
/// <summary> /// 获取缓存 /// </summary> /// <typeparam name="T">值类型</typeparam> /// <param name="key">key</param> /// <returns>值</returns> public T Get<T>(string key) /// <summary> /// 获取缓存 /// </summary> /// <typeparam name="T">值类型</typeparam> /// <param name="key">key</param> /// <param name="tenantKey">租户Key值,如果为NULL则框架会自动获取当前账户的租户Id</param> /// <returns>值</returns> public T GetByTenant<T>(string key, string tenantKey = null) /// <summary> /// 添加或更新缓存 /// </summary> /// <typeparam name="T">值类型</typeparam> /// <param name="key">key</param> /// <param name="value">值</param> public void AddOrUpdate<T>(string key, T value) /// <summary> /// 添加或更新缓存 /// </summary> /// <typeparam name="T">值类型</typeparam> /// <param name="key">key</param> /// <param name="value">值</param> /// <param name="expire">过期时间</param> public void AddOrUpdate<T>(string key, T value, TimeSpan expire) /// <summary> /// 根据租户缓存 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="key"></param> /// <param name="value"></param> /// <param name="tenantKey">租户Key值,如果为NULL则框架会自动获取当前账户的租户Id</param> public void AddOrUpdateByTenant<T>(string key, T value, string tenantKey = null) /// <summary> /// 根据租户缓存 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="key"></param> /// <param name="value"></param> /// <param name="expire"></param> /// <param name="tenantKey"></param> public void AddOrUpdateByTenant<T>(string key, T value, TimeSpan expire, string tenantKey = null) /// <summary> /// 移除 /// </summary> /// <param name="key">缓存Key</param> /// <returns>是否移除成功</returns> public bool Remove<T>(string key) /// <summary> /// 根据租户移除 /// </summary> /// <param name="key">缓存Key</param> /// <param name="tenantKey">租户Key值,如果为NULL则框架会自动获取当前账户的租户Id</param> /// <returns>是否移除成功</returns> public bool RemoveByTenant<T>(string key, string tenantKey = null) /// <summary> /// 清理所有 /// </summary> /// <typeparam name="T">值类型</typeparam> public void Clear<T>() /// <summary> /// 清理所有 /// </summary> /// <typeparam name="T">值类型</typeparam> /// <param name="tenantKey">租户Key值,如果为NULL则框架会自动获取当前账户的租户Id</param> public void ClearByTenant<T>(string tenantKey = null)
获取缓存对象:
using Magicodes.WeiChat.Infrastructure.Cache;
var cache =CacheManager.Current;
获取值(全局):
var value = cache.Get<int>("UserSummaryCount");
获取值(按租户)
var value = cache.GetByTenant<int>("UserSummaryCount");
添加或更新值(全局):
cache.AddOrUpdate("UserSummaryCount", value);
cache.AddOrUpdate("UserSummaryCount", value, TimeSpan.FromHours(1));
添加或更新值(按租户):
cache.AddOrUpdateByTenant("UserSummaryCount", value);
cache.AddOrUpdateByTenant("UserSummaryCount", value, TimeSpan.FromHours(1));
标签:
原文地址:http://www.cnblogs.com/codelove/p/5476404.html