zoukankan      html  css  js  c++  java
  • ASP.NET中CaChe缓存

     一。页面缓存

    页面输出缓存作为最简单的缓存形式,将已经生成的动/静太 页面全部内容保存在服务器内容中。当再有请求时,系统将缓存中的相关数据直接输出,直到缓存数据过期。在这个过程中,缓存不再要再次经过页面处理生命周 期。这样可以缩短请求响应时间,提高应用程序性能。显然,页面输出缓存适用于不需要频繁更新数据,而占用大量时间和资源才能编译生成的页面。

    页输出缓存提供了两种页缓存模型:整页缓存和部分页缓存。整页缓存允许将页的全部内容保存在内存中,并用于完成客户端请求。部分页缓存允许缓存页的部分内容,其他部分则为动态内容。 部分页缓存可采用两种工作方式:控件缓存和缓存后替换。

    在 ASP.NET 页中,在 @ OutputCache 指令中包括必需的 Duration 和 VaryByParam 或 VaryByControl 属性。必须将 Duration 属性设置为大于零的整数。如果希望只按 HTTP 标头值进行缓存,则必须将 VaryByParam 属性设置为“None”。< xmlnamespace prefix ="o" />

    1.整页缓存<%@ OutputCache Duration ="10" VaryByParam="*" Location="Any" %>,设置了缓存时间是10秒

    2.局部缓存,也就是利用控件进行缓存,比如把上面@ OutputCache 指令加在控件中,在页面再引用这个控件,就可以时间对这个控件里的内容进行缓存,

    再一个是缓存后替换

    //缓存数据集
        public static DataSet GetCatchData(DataSet ds,string CacheName)
        {
            if (HttpContext.Current.Cache[CacheName] == null)
            {
                HttpContext.Current.Cache.Insert(CacheName, ds, null, DateTime.Now.AddMinutes(1), System.Web.Caching.Cache.NoSlidingExpiration);
                return ds;
            }
            return (DataSet)HttpContext.Current.Cache[CacheName];
        }
        //缓存字符串
        public static string GetCatchData(string strValue, string CacheName)
        {
            if (HttpContext.Current.Cache[CacheName] == null)
            {
                HttpContext.Current.Cache.Insert(CacheName, strValue, null, DateTime.Now.AddMinutes(1), System.Web.Caching.Cache.NoSlidingExpiration);
                return strValue;
            }
            return (string)HttpContext.Current.Cache[CacheName];
        }

    二、应用程序缓存

    /// <summary>

    /// 获取当前应用程序指定CacheKey的Cache对象值

    /// </summary>

    /// <param name="CacheKey">索引键值</param>

    /// <returns>返回缓存对象</returns>

    public static object GetCache(string CacheKey)

    {

        System.Web.Caching.Cache objCache = HttpRuntime.Cache;

        return objCache[CacheKey];

    }

    /// <summary>

    /// 设置当前应用程序指定CacheKey的Cache对象值

    /// </summary>

    /// <param name="CacheKey">索引键值</param>

    /// <param name="objObject">缓存对象</param>

    public static void SetCache(string CacheKey, object objObject)

    {

        System.Web.Caching.Cache objCache = HttpRuntime.Cache;

        objCache.Insert(CacheKey, objObject);

    }

    /// <summary>

    /// 设置当前应用程序指定CacheKey的Cache对象值

    /// </summary>

    /// <param name="CacheKey">索引键值</param>

    /// <param name="objObject">缓存对象</param>

    /// <param name="absoluteExpiration">绝对过期时间</param>

    /// <param name="slidingExpiration">最后一次访问所插入对象时与该对象过期时之间的时间间隔</param>

    public static void SetCache(string CacheKey, object objObject, DateTime absoluteExpiration, TimeSpan slidingExpiration)

    {

        System.Web.Caching.Cache objCache = HttpRuntime.Cache;

        objCache.Insert(CacheKey, objObject, null, absoluteExpiration, slidingExpiration);

    }       

    protected void Page_Load(object sender, EventArgs e)

    {

        string CacheKey = "cachetest";

        object objModel = GetCache(CacheKey);//从缓存中获取

        if (objModel == null)//缓存里没有

        {

            objModel = DateTime.Now;//把当前时间进行缓存

            if (objModel != null)

            {

                int CacheTime = 30;//缓存时间30秒

                SetCache(CacheKey, objModel, DateTime.Now.AddSeconds(CacheTime), TimeSpan.Zero);//写入缓存

            }

        }

        Label1.Text = objModel.ToString();

    }

  • 相关阅读:
    [Android Pro] Android源码编译之Nexus5真机编译
    [设计模式] 策略模式(Strategy)
    [Android] Anreoid repo 切换分支
    [Android] repo 下载Android源码(国内镜像)
    [Android] osx下如何使用SublimeText阅读Android系统源码
    [Ubuntu] ubuntu的tty下挂载移动硬盘拷贝数据
    Elasticsearch
    Flink简介
    SQL中instr和like的使用区别
    count(1) 与 count(*) 比较
  • 原文地址:https://www.cnblogs.com/sarahVSEve/p/3493827.html
Copyright © 2011-2022 走看看