zoukankan      html  css  js  c++  java
  • HttpRuntime.Cache的使用经验

    配置文件

    <appSettings>  
    <add key="EnableCache" value="true"/>  
    <add key="CacheDurationSeconds" value="300"/>
    </appSettings>

    操作方法

    代码
    using System;
    using System.Web.Configuration;
    public class SiteHelper
    {
        static public object GetCache(string CacheId)
        {
            object objCache = System.Web.HttpRuntime.Cache.Get(CacheId);
           
    // 判断 Cache 是否启用
            if (WebConfigurationManager.AppSettings["EnableCache"] ==null
                ||!Convert.ToBoolean(WebConfigurationManager.AppSettings["EnableCache"]))
            {
                objCache =null;
                System.Web.HttpRuntime.Cache.Remove(CacheId);
            }

           
    return objCache;
        }

       
    ///<summary>
        /// 写入 Cache 资料 ( 预设 60 秒 )
        ///</summary>
        ///<param name="CacheId"></param>
        ///<param name="objCache"></param>
        static public void SetCache(string CacheId, object objCache)
        {
            if (WebConfigurationManager.AppSettings["CacheDurationSeconds"] !=null)
            {
                SetCache(CacheId, objCache, Convert.ToInt32(WebConfigurationManager.AppSettings["CacheDurationSeconds"]));
            }
            else
            {
                SetCache(CacheId, objCache, 60);
            }
        }

       
    static public void SetCache(string CacheId, object objCache, int cacheDurationSeconds)
        {
            if (objCache !=null)
            {
                System.Web.HttpRuntime.Cache.Insert(CacheId, objCache, null, System.Web.Caching.Cache.NoAbsoluteExpiration,new TimeSpan(0, 0, cacheDurationSeconds), System.Web.Caching.CacheItemPriority.High, null);
            }
        }
    }

    使用方法

    代码
    string strCache1 = SiteHelper.GetCache("Cache1") asstring;
    if (strCache1 ==null) {     Response.Write("<p>Cache is empty</p>");
        strCache1
    ="OK";     SiteHelper.SetCache("Cache1", strCache1, 30); }
    Response.Write(strCache1);

    常见问题#Cache显示与清空问题

    代码
    List<string> cacheKeys =new List<string>();
    IDictionaryEnumerator cacheEnum = Cache.GetEnumerator();
    while (cacheEnum.MoveNext())
    {
        cacheKeys.Add(cacheEnum.Key.ToString());
    }
    foreach (string cacheKey in cacheKeys)
    {
        Cache.Remove(cacheKey);
    }
    代码
        //清除所有缓存
        protected void RemoveAllCache()
        {
            System.Web.Caching.Cache _cache = HttpRuntime.Cache;
            IDictionaryEnumerator CacheEnum = _cache.GetEnumerator();
            ArrayList al =new ArrayList();
            while (CacheEnum.MoveNext())
            {
                al.Add(CacheEnum.Key);
            }
            foreach (string key in al)
            {
                _cache.Remove(key);
            }
            show();
        }    
    //显示所有缓存
        void show()
        {
            string str ="";
            IDictionaryEnumerator CacheEnum = HttpRuntime.Cache.GetEnumerator();

           
    while (CacheEnum.MoveNext())
            {
                str +="缓存名<b>["+ CacheEnum.Key +"]</b><br />";
            }
            this.Label1.Text ="当前网站总缓存数:"+ HttpRuntime.Cache.Count +"<br />"+ str;
        }

    添加到扩展方法

    代码
    using System;
    using System.Web.Caching;
    using System.Collections;
    using System.Collections.Generic;

    ///<summary>
    /// 扩充 System.Web.Caching 命名空间的 Extension Methods
    ///</summary>
    static public class CacheExtensionMethod
    {
        public static void Clear(this Cache x)
        {
            List<string> cacheKeys =new List<string>();
            IDictionaryEnumerator cacheEnum = x.GetEnumerator();
            while (cacheEnum.MoveNext())
            {
                cacheKeys.Add(cacheEnum.Key.ToString());
            }
            foreach (string cacheKey in cacheKeys)
            {
                x.Remove(cacheKey);
            }
        }
    }

    转自:http://blog.csdn.net/fengloveyun/article/details/5934158

  • 相关阅读:
    使用nexus 管理pip 私有包
    gitingore && opensource license 自动生成的网站
    lua-resty-qless-web UI 界面运行
    自定义pip 包开发简单说明
    ethr 微软开源的tcp udp http 网络性能测试工具
    openresty 集成lua-resty-mail +smtp2http 扩展灵活的mail 服务
    masterlab 敏捷项目管理工具
    luarocks 自定义包发布试用
    vorpal 又一个方便的cli 开发包
    gogs wekan 集成试用
  • 原文地址:https://www.cnblogs.com/hellen-li/p/3220036.html
Copyright © 2011-2022 走看看