Net通用基础框架Net Common Infrastructure缓存.docx
- 文档编号:28519
- 上传时间:2022-10-01
- 格式:DOCX
- 页数:12
- 大小:40.52KB
Net通用基础框架Net Common Infrastructure缓存.docx
《Net通用基础框架Net Common Infrastructure缓存.docx》由会员分享,可在线阅读,更多相关《Net通用基础框架Net Common Infrastructure缓存.docx(12页珍藏版)》请在冰豆网上搜索。
Net通用基础框架NetCommonInfrastructure缓存
缓存可以提高网站性能,减轻数据库压力。
网站中常用的缓存分为业务数据缓存和页面文件缓存两类,其中业务数据缓存常用AspnetCache,Memcached等,而页面文件缓存常用Squid和Nginx,今天介绍的内容是业务数据缓存。
∙Common.Cache类图
∙缓存接口ICache:
使用Add方法时,如果key存在,则返回false。
使用Set方法时,key不存在则添加,否则更新。
usingSystem;
usingSystem.Collections.Generic;
namespaceCommon.Cache
{
///
///缓存
///
publicinterfaceICache
{
///
///增加
///
///
///
///
///
boolAdd
///
///增加
///
///
///
///
///
///
boolAdd
///
///清除
///
voidClear();
///
///获取
///
///
///
///
TGet
///
///多线程获取
///
///
///
IDictionary
///
///移除
///
///
voidRemove(stringkey);
///
///设置
///
///
///
///
///
boolSet
///
///设置
///
///
///
///
///
///
boolSet
}
}
∙缓存基类
usingSystem;
usingSystem.Collections.Generic;
namespaceCommon.Cache
{
///
///缓存基类
///
publicabstractclassCacheBase:
ICache
{
privateTimeSpanmaxDuration=TimeSpan.FromDays(15);
///
///最长持续时间
///
publicTimeSpanMaxDuration
{
get
{
returnthis.maxDuration;
}
set
{
this.maxDuration=value;
}
}
///
///前缀
///
publicstringPrefix
{
get;
set;
}
publicboolAdd
{
returnthis.Add
}
publicabstractboolAdd
publicabstractvoidClear();
publicabstractTGet
///
///获取全名
///
///
///
publicvirtualstringGetFullName(stringkey)
{
stringresult=key;
if(!
string.IsNullOrWhiteSpace(this.Prefix))
{
result=string.Format("{0}.{1}",this.Prefix,key);
}
returnresult;
}
publicabstractIDictionary
publicabstractvoidRemove(stringkey);
publicboolSet
{
returnthis.Set
}
publicabstractboolSet
}
}
∙Aspnet缓存实现
usingSystem;
usingSystem.Collections;
usingSystem.Collections.Generic;
usingSystem.Web;
usingSystem.Web.Caching;
namespaceCommon.Cache
{
///
///Aspnet缓存
///
publicclassAspnetCache:
CacheBase
{
privateSystem.Web.Caching.Cachecache=HttpRuntime.Cache;
///
///构造函数
///
publicAspnetCache()
:
this("Common.Cache")
{
}
///
///构造函数
///
///
publicAspnetCache(stringprefix)
{
this.Prefix=prefix;
}
publicoverrideboolAdd
{
boolresult=false;
if(value!
=null)
{
if(duration<=TimeSpan.Zero)
{
duration=this.MaxDuration;
}
result=this.cache.Add(this.GetFullName(key),value,null,DateTime.Now.Add(duration),System.Web.Caching.Cache.NoSlidingExpiration,CacheItemPriority.Default,null)==null;
}
returnresult;
}
publicoverridevoidClear()
{
//获取键集合
IList
IDictionaryEnumeratorcaches=this.cache.GetEnumerator();
while(caches.MoveNext())
{
stringkey=caches.Key.ToString();
if(key.StartsWith(this.Prefix))
{
keys.Add(key);
}
}
//移除全部
foreach(stringkeyinkeys)
{
this.cache.Remove(key);
}
}
publicoverrideTGet
{
Tresult=default(T);
objectvalue=this.cache.Get(this.GetFullName(key));
if(valueisT)
{
result=(T)value;
}
returnresult;
}
publicoverrideIDictionary
{
IDictionary
foreach(stringkeyinkeys)
{
res
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- Net通用基础框架Net Common Infrastructure缓存 Net 通用 基础 框架 Infrastructure 缓存
