System.Web.Caching简单封装类:

 using System;
using System.Collections.Generic;
using System.Web.Caching;
using System.Web;
using System.Collections; namespace APP.HttpCache
{
public class CacheHelper
{
private static Cache cacheObj = HttpRuntime.Cache; /// <summary>
/// 简单key,vlaue写入
/// </summary>
/// <param name="key"></param>
/// <param name="value"></param>
public static void Insert(string key, object value)
{
cacheObj.Insert(key, value);
} /// <summary>
/// 设置绝对过期时间
/// </summary>
/// <param name="key"></param>
/// <param name="value"></param>
/// <param name="absoluteExpiration"></param>
/// <param name="slidingExpiration"></param>
public static void Insert(string key, object value, DateTime absoluteExpiration)
{
cacheObj.Insert(key, value, null, absoluteExpiration, Cache.NoSlidingExpiration);
} /// <summary>
/// 设置平滑过期
/// </summary>
/// <param name="key"></param>
/// <param name="value"></param>
/// <param name="slidingExpiration"></param>
public static void Insert(string key, object value, TimeSpan slidingExpiration)
{
cacheObj.Insert(key, value, null, Cache.NoAbsoluteExpiration, slidingExpiration);
} /// <summary>
/// 得到vlaue
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
public static object Get(string key)
{
return cacheObj.Get(key);
} /// <summary>
/// 得到vlaue
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
public static T Get<T>(string key)
{
var v = cacheObj.Get(key);
return v == null ? default(T) : (T)Convert.ChangeType(v, typeof(T));
} /// <summary>
/// 移除key
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
public static void Delete(string key)
{
cacheObj.Remove(key);
} /// <summary>
/// 移除key
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
public static object Remove(string key)
{
return cacheObj.Remove(key);
} /// <summary>
/// 移除key
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
public static T Remove<T>(string key)
{
var v = cacheObj.Remove(key);
return v == null ? default(T) : (T)Convert.ChangeType(v, typeof(T));
} /// <summary>
/// 缓存key数量
/// </summary>
public static int KeyCount
{
get
{
return cacheObj.Count;
}
} /// <summary>
/// 所有key
/// </summary>
public static ArrayList KeyAll()
{
var arr = new ArrayList();
var item = cacheObj.GetEnumerator();
while (item.MoveNext())
{
arr.Add(item.Key);
}
return arr;
} /// <summary>
/// 清空所有缓存
/// </summary>
public static void DeleteAll()
{
var item = cacheObj.GetEnumerator();
while (item.MoveNext())
{
cacheObj.Remove(item.Key.ToString());
}
}
}
}

System.Web.Caching的更多相关文章

  1. System.Web.Caching.Cache类 缓存

    1.文件缓存依赖 public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender ...

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

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

  3. System.Web.Caching.Cache 方法汇总

    在做后台的时候,想着把所有栏目放到缓存里,就这里了一个类.必然是有缺陷,暂时没有实现滑动缓存 using System; using System.Collections; using System. ...

  4. 清除 System.Web.Caching.Cache 以"xxx"开头的缓存

    public static void ClearStartCache(string keyStart) { List<string> cacheKeys = new List<str ...

  5. 第一节:从程序集的角度分析System.Web.Caching.Cache ,并完成基本封装。

    一. 揭开迷雾 1. 程序集准备 a.  需要给项目添加 System.Web 程序集. b.  需要给使用的地方添加两个引用. 2. 程序集探究      在对应的类中输入关键字 Cache,选中点 ...

  6. 深入System.Web.Caching命名空间 教你Hold住缓存管理

    一,System .Web.Caching与缓存工作机制简介 System.Web.Caching是用来管理缓存的命名空间,其父级空间是System.Web,由此可见,缓存通常用于Web网站的开发,包 ...

  7. System.Web.Caching.Cache类 缓存 各种缓存依赖(转)

    转自:http://www.cnblogs.com/kissdodog/archive/2013/05/07/3064895.html Cache类,是一个用于缓存常用信息的类.HttpRuntime ...

  8. System.Web.Caching.Cache类 Asp.Net缓存 各种缓存依赖

    Cache类,是一个用于缓存常用信息的类.HttpRuntime.Cache以及HttpContext.Current.Cache都是该类的实例. 一.属性 属性 说明 Count 获取存储在缓存中的 ...

  9. Cache管理机制(System.Web.Caching)

    一,System .Web.Caching与缓存工作机制简介 System.Web.Caching是用来管理缓存的命名空间,其父级空间是System.Web,由此可见,缓存通常用于Web网站的开发,包 ...

随机推荐

  1. WideCharToMultiByte和MultiByteToWideChar函数的用法(转载)

    出处:http://www.cnblogs.com/gakusei/articles/1585211.html 为了支持Unicode编码,需要多字节与宽字节之间的相互转换.这两个系统函数在使用时需要 ...

  2. 用Python为iOS和Android写跨平台的应用

    首先保证安装了最新的python(当前安装的是python3.6) 一.安装Kivy :python -m pip install --upgrade pip wheel setuptools pyt ...

  3. C# 获取SQL Server所有的数据库名称

    参考文章:http://www.cnblogs.com/Abel_cn/archive/2008/12/09/1351425.html http://blog.csdn.net/friendan/ar ...

  4. java.io.InvalidClassException

    java.io.InvalidClassException public class PetroleumCoke implements Serializable { private static fi ...

  5. [JVM] - 不就是JVM么 JVM的继续探究

    前面几章跟着作者的脚步实现了使用Go语言查看java的.class文件源码(16进制) 复习一下: 相比Java语言,Go的访问控制非常简单,只有公开和私有两种. 所有首字母大写的类型, 结构体,字段 ...

  6. UVa 12545 比特变换器

    https://vjudge.net/problem/UVA-12545 题意:输入两个等长的串S和T,其中S包含字符0,1,?,但T只包含0和1. 用尽量少的步数把S变成T.每步有3种操作: ①把S ...

  7. nginx configuration

    Now that you know how to manage the service itself, you should take a few minutes to familiarize you ...

  8. vue router菜单 存在点哪个但还是会显示active

    <router-link to="/" exact>Home</router-link> <router-link to="/add&quo ...

  9. python3.6 ImportWarning: can't resolve package from __spec__ or __package__, falling back on __name__ and __path__

    Cython emulates Python 2-style implicit relative imports on Python 3 Cython的锅(也就是绝大多数下载安装的python)新的i ...

  10. 【Golang】Debug :decoding dwarf section info at offset 0x0: too short

    解决方法 通过下面的方式升级dlv 来解决这个问题: go get -u github.com/derekparker/delve/cmd/dlv 下面是我记录的定位问题的过程 问题描述 博主升级到了 ...