。net 文件依赖缓存

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Default2 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        string CacheKey = "cachetest";
        object objModel = GetCache(CacheKey);//从缓存中获取
        if (objModel == null)//缓存里没有
        {
            objModel = DateTime.Now;//把当前时间进行缓存
            if (objModel != null)
            {
                  System.Web.Caching.CacheDependency dep=new   System.Web.Caching.CacheDependency("C:\\test.txt");
                SetCache(CacheKey, objModel, dep);//写入缓存
            }
        }
        Label1.Text = objModel.ToString();
    }
    public static object GetCache(string CacheKey)
    {
        System.Web.Caching.Cache objCache = HttpRuntime.Cache;
        return objCache[CacheKey];
    }

    public static void SetCache(string CacheKey, object objObject,System.Web.Caching.CacheDependency dep)
    {
        System.Web.Caching.Cache objCache = HttpRuntime.Cache;
        objCache.Insert(CacheKey, objObject,dep,System.Web.Caching.Cache.NoAbsoluteExpiration,
            System.Web.Caching.Cache.NoSlidingExpiration, System.Web.Caching.CacheItemPriority.Default,null

            );
    }
}

  

posted @ 2016-11-17 16:06  尘梦  阅读(235)  评论(0编辑  收藏  举报