配置文件

<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. 利用WinHEX,重构狂牛加密视频1.0.0.1【只适合RIFF(AVI)】

    幸亏是视频部分没有进行加密 1.用 WinHEX 打开狂牛加密视频, 查找 [RIFF] 字符串 2.光标放在 RIFF的 [R]上面, 按 CTRL+SHIFT+END 3.把选择的块写入新文件 H ...

  2. hdu 1022 Train Problem I 解题报告

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1022 又是一道栈的练习,这次也是没有用到STL中的栈来实现.用来保存操作过程的数组(process[] ...

  3. Androidi性能优化之高效使用内存

    应用生存期的绝大多数时间都在用于处理内存中的数据 性能主要取决于以下三个因素: a:CPU如何操作特定的数据类型 b: 数据和指令需要占用多少存储空间 c: 数据在内存中的布局 访问内存: 因为访问内 ...

  4. stm32学习笔记----双串口同时打开时的printf()问题

    stm32学习笔记----双串口同时打开时的printf()问题 最近因为要使用串口2外接PN532芯片实现通信,另一方面,要使用串口1来将一些提示信息输出到上位机,于是重定义了printf(),使其 ...

  5. (七)STM32的RTC简单操作

    简单说明: /********************************************************************************************* ...

  6. Android仿微信界面

    效果图 原理介绍 1.先绘制一个颜色(例如:粉红) 2.设置Mode=DST_IN 3.绘制我们这个可爱的小机器人 回答我,显示什么,是不是显示交集,交集是什么?交集是我们的小机器人的非透明区域,也就 ...

  7. tomcat The file is absent or does not have execute permission

    [root@centos02 bin]# ./startup.sh Cannot find ./catalina.sh The file is absent or does not have exec ...

  8. 微信jsapi接口测试

    微信jsapi接口测试 <?php require_once 'lib.inc.php'; $wx = new WxApi(); if(!isset($_GET['code'])){ heade ...

  9. hdu 1215 筛法

    求小于n的n的因子之和 Sample Input 3 2 10 20Sample Output 1 8 22 #include<cstdio> #include<iostream&g ...

  10. Ubuntu 登录命令和赋值命令

    一. Ubuntu 设定root权限 进入 Terminal,输入命令su 如果没有给su赋root权限,则输入  sudo passwd root 赋值输入密码. 截图如下 二.以root身份进入l ...