配置文件

<appSettings>  
<add key="EnableCache" value="true"/>  
<add key="CacheDurationSeconds" value=""/>
</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, );
        }
    }
    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(, , 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, ); }
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

HttpRuntime.Cache的使用经验的更多相关文章

  1. 缓存 HttpContext.Current.Cache和HttpRuntime.Cache的区别

    先看MSDN上的解释: HttpContext.Current.Cache:为当前 HTTP 请求获取Cache对象. HttpRuntime.Cache:获取当前应用程序的Cache.  我们再用. ...

  2. HttpContext.Current.Cache 和HttpRuntime.Cache的区别

    先看MSDN上的解释:      HttpContext.Current.Cache:为当前 HTTP 请求获取Cache对象.      HttpRuntime.Cache:获取当前应用程序的Cac ...

  3. ASP.NET HttpRuntime.Cache缓存类使用总结

    1.高性能文件缓存key-value存储—Redis 2.高性能文件缓存key-value存储—Memcached 备注:三篇博文结合阅读,简单理解并且使用,如果想深入学习,请多参考文章中给出的博文地 ...

  4. HttpRuntime.Cache 失效

    最近做一个报纸内容类网站,为了提高响应速度,将首页各栏目以及二级栏目中Part文献列表存储在HttpRuntime.Cache缓存中,发布后发现问题,刚插入的缓存很快就失效,本机调试没有问题. 由于H ...

  5. 服务端缓存HttpRuntime.Cache的使用

    HttpRuntime.Cache.Insert("缓存key", "缓存content", null, DateTime.Now.AddMinutes(3), ...

  6. HttpRuntime.Cache被清空的DataTable

    将一个DataTable存到Cache中后,另一个页面新建变量并获取,操作变量,Cache中的数据也被改动了? 页面a.aspx 初始化并赋值,输出当前缓存内DataTable内数据条数 Page_L ...

  7. HttpContext.Current.Cache 和 HttpRuntime.Cache 区别

    原文地址:http://blog.csdn.net/avon520/article/details/4872704 .NET中Cache有两种调用方式:HttpContext.Current.Cach ...

  8. 分布式缓存HttpRuntime.cache应用到单点登陆中_优化登陆

    以前的设计方案,是我们在数据库中放一个表,用作存储验证登陆成功的用户,并且生成用户TOKEN(令牌) 分布式缓存+集群的解决方案图: 相应的代码: DE层中配置文件: receiveTimeout=& ...

  9. Asp.net缓存技术(HttpRuntime.Cache)

    一.缓存: 5个等级的缓存 1级是网络级缓存,缓存在浏览器,CDN以及代理服务器中   (举个例子:每个帮助页面都进行了缓存,访问一个页面的代码非常简单) 2级是由.net框架 HttpRuntime ...

随机推荐

  1. 【Django】Django 直接执行原始SQL 如何防止SQL注入 ?

    代码示例: #错误--不要直接格式化字符串 query = 'SELECT * FROM myapp_person WHERE last_name = %s' % lname Person.objec ...

  2. 【Linux】为啥查某个进程的线程,查出来的所有线程的pid不一样啊

    楼上说的linux线程和进程是一样的,这个说法是错误的. 看了楼主的问题,感觉楼主是被PID给弄混了,线程进程都会有自己的ID,这个ID就叫做PID,PID是不特指进程ID,线程ID也可以叫做PID. ...

  3. canvas实践小实例二 —— 扇形

    俗话说:发图不留种,菊花万人捅!我这里想延伸一下:教学不给例,说你是傻逼!哎呀,还挺押韵,嘻嘻,开个玩笑! 我们都讲了四期API的知识了,估计大家看的也是枯燥的很啊,前面的小实例也是太简单,简直不解渴 ...

  4. Java for LeetCode 152 Maximum Product Subarray

    Find the contiguous subarray within an array (containing at least one number) which has the largest ...

  5. 猪八戒吃西瓜(wmelon)-排序-查找

    问题 A: 猪八戒吃西瓜(wmelon) 时间限制: 1 Sec  内存限制: 64 MB提交: 30  解决: 14[提交][状态][讨论版] 题目描述 有一天,贪吃的猪八戒来到了一个大果园,果园里 ...

  6. poj 1611 The Suspects 解题报告

    题目链接:http://poj.org/problem?id=1611 题意:给定n个人和m个群,接下来是m行,每行给出该群内的人数以及这些人所对应的编号.需要统计出跟编号0的人有直接或间接关系的人数 ...

  7. 一个程序中关于多个osgGA::GUIEventHandler同时存在的问题

    平时使用GUIEventHandler不太注意handle()函数的返回值,觉得返回true或者false都无所谓,其实不然. 我遇到的问题是程序中一个节点添加了GUIEventHandler对象pi ...

  8. sublime text 3 使用过程总结记录

    自定义的设置: "save_on_focus_lost": true //在文件失去焦点的时候自动保存

  9. Java 批量反编译class文件,并保持目录结构

    jad -o -r -d d:\src -s java C:\Users\spring\Desktop\egorder3.0\WEB-INF\classes\**\*.class -o - overw ...

  10. java类初始化优先级

    父类静态变量.父类静态代码块.子类静态变量.子类静态代码块.父类非静态变量.父类非静态代码块.父类构造函数.子类非静态变量.子类非静态代码块.子类构造函数