缓存依赖文件或文件夹

//创建缓存依赖项

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数据库依赖缓存

 

<connectionStrings>    
    <add name="Am_WeixinWeb" connectionString="data source=192.168.1.200;initial catalog=Am_WeixinWeb;uid=sa;password=lh1234;"  />
</connectionStrings>

 

<caching>
      <sqlCacheDependency enabled="true" pollTime="2000">
        <databases>
          <add name="Test" connectionStringName="Am_WeixinWeb" />
        </databases>
      </sqlCacheDependency>
</caching>

注:pollTime,毫秒为单位,意识是每隔2秒检测下数据库,检测表是否有发生变化。connectionStringName为数据库链接字符串。

 

//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中的数据有所变动,则清空缓存,重新写入。

 

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 2016-06-15 13:50  邢帅杰  阅读(686)  评论(0编辑  收藏  举报