缓存依赖文件或文件夹
//创建缓存依赖项
CacheDependency dep = new CacheDependency(fileName);//Server.MapPath("");
//创建缓存
HttpRuntime.Cache.Insert(key, obj, dep);
fileName:可以是文件或者文件夹目录
若fileName是文件,则你创建的缓存依赖该文件,该文件若更改,则缓存更新。
若fileName是一个文件夹目录,则缓存依赖该文件夹,该文件夹跟目录下的文件若有变动,则缓存更新。
缓存依赖多个文件或文件夹
CacheDependency dep1 = new CacheDependency(fileName1);
CacheDependency dep2 = new CacheDependency(fileName2);
CacheDependency[] deps = new CacheDependency[] { dep1, dep2 };
AggregateCacheDependency mulDep = new AggregateCacheDependency(); //多个依赖项
mulDep.Add(deps);
HttpRuntime.Cache.Insert(key, obj, mulDep);
MVC缓存
[OutputCache(Duration = 20)]
MVC数据库依赖缓存
1 2 3 | <connectionStrings> <add name= "Am_WeixinWeb" connectionString= "data source=192.168.1.200;initial catalog=Am_WeixinWeb;uid=sa;password=lh1234;" /> </connectionStrings> |
1 2 3 4 5 6 7 | <caching> <sqlCacheDependency enabled= "true" pollTime= "2000" > <databases> <add name= "Test" connectionStringName= "Am_WeixinWeb" /> </databases> </sqlCacheDependency> </caching> |
注:pollTime,毫秒为单位,意识是每隔2秒检测下数据库,检测表是否有发生变化。connectionStringName为数据库链接字符串。
1 2 3 4 5 6 7 | //mvc缓存依赖 [OutputCache(Duration = 20, SqlDependency = "Test:Am_recProScheme" )] //Test:为缓存配置的key,后面跟的是缓存依赖表 public ActionResult Index() { Response.Write(db.Am_recProScheme.FirstOrDefault().recContent); return View(); } |
效果:数据库Am_WeixinWeb中表Am_recProScheme中的数据有所变动,则清空缓存,重新写入。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 | using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Web.Caching; using System.Web; namespace Framework.Common { /// <summary> /// 缓存辅助类 /// </summary> public class CacheHelper { /// <summary> /// 创建缓存项的文件依赖 /// </summary> /// <param name="key">缓存Key</param> /// <param name="obj">object对象</param> /// <param name="fileName">文件绝对路径</param> public static void Insert( string key, object obj, string fileName) { //创建缓存依赖项 CacheDependency dep = new CacheDependency(fileName); //创建缓存 HttpRuntime.Cache.Insert(key, obj, dep); } /// <summary> /// 创建缓存项过期 /// </summary> /// <param name="key">缓存Key</param> /// <param name="obj">object对象</param> /// <param name="expires">过期时间(分钟)</param> public static void Insert( string key, object obj, int expires) { if ( string .IsNullOrEmpty(key) || obj == null ) { return ; } HttpRuntime.Cache.Insert(key, obj, null , Cache.NoAbsoluteExpiration, new TimeSpan(0, expires, 0)); } /// <summary> /// 获取缓存对象 /// </summary> /// <param name="key">缓存Key</param> /// <returns>object对象</returns> public static object Get( string key) { if ( string .IsNullOrEmpty(key)) { return null ; } return HttpRuntime.Cache.Get(key); } /// <summary> /// 获取缓存对象 /// </summary> /// <typeparam name="T">T对象</typeparam> /// <param name="key">缓存Key</param> /// <returns></returns> public static T Get<T>( string key) { object obj = Get(key); return obj == null ? default (T) : (T)obj; } } } |
参考:http://www.cnblogs.com/knowledgesea/p/3904929.html
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 提示词工程——AI应用必不可少的技术
· .NET周刊【3月第1期 2025-03-02】