欢迎您访问我爱IT技术网,今天小编为你分享的编程技术是:【ASP.NET性能优化之构建自定义文件缓存】,下面是详细的讲解!
ASP.NET性能优化之构建自定义文件缓存
public class FileCacheProvider : OutputCacheProvider
{
private static readonly ILog log=LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
public override void Initialize(string name, NameValueCollection attributes)
{
base.Initialize(name, attributes);
CachePath=HttpContext.Current.Server.MapPath(attributes["cachePath"]);
}
public override object Add(string key, object entry, DateTime utcExpiry)
{
Object obj=Get(key);
if (obj !=null) //这一步很重要
{
return obj;
}
Set(key,entry,utcExpiry);
return entry;
}
public override object Get(string key)
{
string path=ConvertKeyToPath(key);
if (!File.Exists(path))
{
return null;
}
CacheItem item=null;
using (FileStream file=File.OpenRead(path))
{
var formatter=new BinaryFormatter();
item=(CacheItem)formatter.Deserialize(file);
}
if (item.ExpiryDate <=DateTime.Now.ToUniversalTime())
{
log.Info(item.ExpiryDate + "*" + key);
Remove(key);
return null;
}
return item.Item;
}
public override void Set(string key, object entry, DateTime utcExpiry)
{
CacheItem item=new CacheItem(entry, utcExpiry);
string path=ConvertKeyToPath(key);
using (FileStream file=File.OpenWrite(path))
{
BinaryFormatter formatter=new BinaryFormatter();
formatter.Serialize(file, item);
}
}
public override void Remove(string key)
{
string path=ConvertKeyToPath(key);
if (File.Exists(path))
File.Delete(path);
}
public string CachePath
{
get;
set;
}
private string ConvertKeyToPath(string key)
{
string file=key.Replace('/', '-');
file +=".txt";
return Path.Combine(CachePath, file);
}
}
[Serializable]
public class CacheItem
{
public DateTime ExpiryDate;
public object Item;
public CacheItem(object entry, DateTime utcExpiry)
{
Item=entry;
ExpiryDate=utcExpiry;
}
}
关于ASP.NET性能优化之构建自定义文件缓存的用户互动如下:
相关问题:asp.net 服务器缓存技术
答:实现 要实现页面输出缓存,只要将一条 OutputCache 指令添加到页面即可。 <%@ OutputCache Duration="60" VaryByParam="*" %> 如同其他页面指令一样,该指令应该出现在 ASPX 页面的顶部,即在任何输出之前。它支持五个属性(或参数),其中两... >>详细
相关问题:什么是ASP.NET的 盒外缓存服务
答:ASP.NET 是在服务器上运行编译好的公共语言运行库代码。与 ASP 不同, ASP.NET 可利用早期绑定、实时编译、本机优化和盒外缓存服务。这相当于在编写代码行之前便显著提高了性能。 ASP.NET 采用基于文本的分层配置系统,简化了将设置应用于服务器... >>详细
相关问题:.net 缓存是什么 放在哪里 概念模糊了 大侠帮忙
答:在 ASP.NET 提供的许多特性中,缓存支持我最欣赏的特性,相比 ASP.NET 的所有其他特性,缓存对应用程序的性能具有最大的潜在影响,利用缓存和其他机制,ASP.NET 开发人员可以接受使用开销很大的控件(例如,DataGrid)构建站点时的额外开销,而... >>详细
- 【asp】asp.net url重写浅谈-net-url重写
- 【DataSet】DataSet、DataTable、DataRow区别详解
- 【asp】asp.net 动态添加多个用户控件-net-动态添
- 【ASP】ASP.NET中内嵌页面代码的一个问题-NET-内
- 【As】Asp.net中的页面乱码的问题-sp--pn-ne-et
- 【增加记录】asp.net中获取新增加记录的ID Access
- 【创建】ASP.NET Web API教程 创建域模型的方法详
- 【Asp】Asp.net 页面调用javascript变量的值-net-
- 【ASP】ASP.NET 5升级后如何删除旧版本的DNX-NET5
- 【404页面】ASP.NET设置404页面返回302HTTP状态码
- 评论列表(网友评论仅供网友表达个人看法,并不表明本站同意其观点或证实其描述)
-
