posts - 609,  comments - 13,  views - 64万
< 2025年3月 >
23 24 25 26 27 28 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 1 2 3 4 5

缓存依赖文件或文件夹

//创建缓存依赖项

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

posted on   邢帅杰  阅读(690)  评论(0编辑  收藏  举报
编辑推荐:
· 如何编写易于单元测试的代码
· 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】
点击右上角即可分享
微信分享提示