配置文件

<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

转载于:https://www.cnblogs.com/hellen-li/p/3220036.html

HttpRuntime.Cache的使用经验相关推荐

  1. ASP.NET 中HttpRuntime.Cache缓存数据

    最近在开始一个微信开发,发现微信的Access_Token获取每天次数是有限的,然后想到缓存,正好看到微信教程里面推荐HttpRuntime.Cache缓存就顺便看了下. 写了(Copy)了一个辅助类 ...

  2. HttpContext.Current.Cache vs. HttpRuntime.Cache

    .NET中Cache有两种调用方式:HttpContext.Current.Cache 和 HttpRuntime.Cache,这两种方式有什么区别呢?我们先看MSDN上的解释:       Http ...

  3. HttpContext.Current.Cache和HttpRuntime.Cache的区别,以及System.Runtime.Caching

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

  4. 缓存通用管理类 + 缓存 HttpContext.Current.Cache 和 HttpRuntime.Cache 的区别

    以前写asp.net时用HttpContext.Current.Cache存缓存很好用,今天写了一个windows服务程序,HttpContext.Current.Cache存缓存的时候还好,取的时候 ...

  5. HttpRuntime.Cache的用法

    HttpRuntime Cache用法及参数解释 System.Web.HttpRuntime.Cache的方法: Add,Insert,Get,Remove 存Cache方法: HttpRuntim ...

  6. System.Web.Caching.Cache类 缓存 各种缓存依赖

    原文:System.Web.Caching.Cache类 缓存 各种缓存依赖 Cache类,是一个用于缓存常用信息的类.HttpRuntime.Cache以及HttpContext.Current.C ...

  7. 利用Cache,asp.net 简单实现定时执行任务

    利用Cache,让asp.net 简单实现定时执行任务 代码 private static CacheItemRemovedCallback OnCacheRemove = null; protect ...

  8. ASP.NET 学习笔记_13 viewstate 和 cache

    1. (1.默认情况下ASP.Net是启用ViewState的,这样在页面中会生成冗长的隐藏字段,ViewState对于需要PostBack处理的页面才可能有用,对于新闻展示页面不需要交互完全没必要用 ...

  9. asp.net中缓存Cache类的使用案例(附源码)

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.C ...

最新文章

  1. 官方wdpc安装文档,推荐RPM包安装
  2. 大连海事学院计算机研究生多少分,大连海事大学计算机或软件工程研究生多少分能录取...
  3. nyoj 14 会场安排问题(贪心专题)
  4. mysql日期处理的一些实现
  5. 【黑金ZYNQ7000系列原创视频教程】06.ZYNQ来自FPGA的中断——按键中断实验
  6. 编译打包vue_Vue 源码分析( 一 )
  7. Python3——网络编程基础
  8. 数据仓库 Hive(内含大数据镜像下载)
  9. C#多线程技术总结(异步)
  10. CoordinatorLayout 使用综述系列(二)与AppBarLayout结合上下联动效果
  11. 表格图片预览_Mac预览工具使用技巧,Mac预览功能实用技巧大全
  12. Hive自定义函数UDF、UDAF、UDTF
  13. 【论文写作】文献资料的作用只是添砖加瓦
  14. POJ3264 Balanced Lineup【线段树】
  15. js先执行一个方法再往下执行_轻松理解JS中的面向对象,顺便搞懂prototype和__proto__...
  16. 数据库学习笔记1-事务 transaction
  17. matlab 中文注释乱码问题解决
  18. Contrastive learning的学习
  19. 另类推柿子 Crypto Lights
  20. MindManager Mac苹果版本教程激活码序列号秘钥下载详情

热门文章

  1. html设置json请求头,当我想在zf2客户端代码中使用“application/json”时,接受请求标头是“text/html,application/xhtm ...(etc)”...
  2. MyEclipse设置代码自动补全,及取消空格和‘=’补全
  3. mysql5.7存储json_MySQL5.7的json数据格式的问题
  4. vue 加载数据 影响seu_VUE常见面试题
  5. 使用帅气的cordic算法进行坐标系互转及log10的求解
  6. 解决JAVA_HOME nor the JRE_HOME environment variable is defined
  7. ubuntu16.04安装teamviewer12
  8. 挂载(mount)深入理解
  9. 使用jenkins进行Android的持续集成
  10. 移动开发js库Zepto.js使用中的一些注意点