博客
关于我
C#语法糖之 cache操作类 asp.net
阅读量:457 次
发布时间:2019-03-06

本文共 5921 字,大约阅读时间需要 19 分钟。

因为考虑到我下面我将写session cookies 等 操作类 ,与cache具有共性。 所以都统一继承了IHttpStorageObject  abstract class 来保函数风格的统一 ,但是又为了调用方便,抽象中又使用了单例来简化调用。

 

 

使用方法很简单:

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Web;using System.Web.Caching;using System.Collections;using System.Linq.Expressions;namespace SyntacticSugar{    ///     /// ** 描述:缓存操作类    /// ** 创始时间:2015-6-9    /// ** 修改时间:-    /// ** 作者:sunkaixuan    /// ** 使用说明:http://www.cnblogs.com/sunkaixuan/p/4563462.html    ///     /// 
///
public class CacheManager
: IHttpStorageObject
{ #region 全局变量 private static CacheManager
_instance = null; private static readonly object _instanceLock = new object(); #endregion #region 构造函数 private CacheManager() { } #endregion #region 属性 ///
///根据key获取value /// ///
public override V this[string key] { get { return (V)HttpRuntime.Cache[CreateKey(key)]; } } #endregion #region 公共函数 ///
/// key是否存在 /// ///
key ///
/// 存在
true
不存在
false
. /// ///
public override bool ContainsKey(string key) { return HttpRuntime.Cache[CreateKey(key)] != null; } ///
/// 获取缓存值 /// ///
key ///
public override V Get(string key) { return (V)HttpRuntime.Cache.Get(CreateKey(key)); } ///
/// 获取实例 (单例模式) /// ///
public static CacheManager
GetInstance() { if (_instance == null) lock (_instanceLock) if (_instance == null) _instance = new CacheManager
(); return _instance; } ///
/// 插入缓存(默认20分钟) /// ///
key ///
value public override void Add(string key, V value) { Add(key, value, Minutes*20); } ///
/// 插入缓存 /// ///
key ///
value ///
过期时间单位秒 public void Add(string key, V value, int cacheDurationInSeconds) { Add(key, value, cacheDurationInSeconds, CacheItemPriority.Default); } ///
/// 插入缓存. /// ///
key ///
value ///
过期时间单位秒 ///
缓存项属性 public void Add(string key, V value, int cacheDurationInSeconds, CacheItemPriority priority) { string keyString = CreateKey(key); HttpRuntime.Cache.Insert(keyString, value, null, DateTime.Now.AddSeconds(cacheDurationInSeconds), Cache.NoSlidingExpiration, priority, null); } ///
/// 插入缓存. /// ///
key ///
value ///
过期时间单位秒 ///
缓存项属性 public void Add(string key, V value, int cacheDurationInSeconds, CacheDependency dependency, CacheItemPriority priority) { string keyString = CreateKey(key); HttpRuntime.Cache.Insert(keyString, value, dependency, DateTime.Now.AddSeconds(cacheDurationInSeconds), Cache.NoSlidingExpiration, priority, null); } ///
/// 删除缓存 /// ///
key public override void Remove(string key) { HttpRuntime.Cache.Remove(CreateKey(key)); } ///
/// 清除所有缓存 /// public override void RemoveAll() { 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); } } ///
/// 清除所有包含关键字的缓存 /// ///
关键字 public override void RemoveAll(Func
removeExpression) { System.Web.Caching.Cache _cache = HttpRuntime.Cache; var allKeyList = GetAllKey(); var delKeyList = allKeyList.Where(removeExpression).ToList(); foreach (var key in delKeyList) { Remove(key); } } ///
/// 获取所有缓存key /// ///
public override IEnumerable
GetAllKey() { IDictionaryEnumerator CacheEnum = HttpRuntime.Cache.GetEnumerator(); while (CacheEnum.MoveNext()) { yield return CacheEnum.Key.ToString(); } } #endregion #region 私有函数 ///
///创建KEY /// ///
Key ///
private string CreateKey(string key) { return "http_cache_"+key.ToString(); } #endregion }}

  

using System;namespace SyntacticSugar{    public abstract class IHttpStorageObject
{ public int Minutes = 60; public int Hour = 60 * 60; public int Day = 60 * 60 * 24; public System.Web.HttpContext context = System.Web.HttpContext.Current; public abstract void Add(string key, V value); public abstract bool ContainsKey(string key); public abstract V Get(string key); public abstract global::System.Collections.Generic.IEnumerable
GetAllKey(); public abstract void Remove(string key); public abstract void RemoveAll(); public abstract void RemoveAll(Func
removeExpression); public abstract V this[string key] { get; } }}

  

你可能感兴趣的文章
MTTR、MTBF、MTTF的大白话理解
查看>>
mt_rand
查看>>
mysql -存储过程
查看>>
mysql /*! 50100 ... */ 条件编译
查看>>
mysql 1045解决方法
查看>>
mudbox卸载/完美解决安装失败/如何彻底卸载清除干净mudbox各种残留注册表和文件的方法...
查看>>
mysql 1264_关于mysql 出现 1264 Out of range value for column 错误的解决办法
查看>>
mysql 1593_Linux高可用(HA)之MySQL主从复制中出现1593错误码的低级错误
查看>>
mysql 5.6 修改端口_mysql5.6.24怎么修改端口号
查看>>
mui折叠面板点击事件跳转
查看>>
MySQL 8 公用表表达式(CTE)—— WITH关键字深入用法
查看>>
mysql 8 远程方位_mysql 8 远程连接注意事项
查看>>
MUI框架里的ajax的三种方法
查看>>
MySQL 8.0 恢复孤立文件每表ibd文件
查看>>
Mysql 8.0 新特性
查看>>
MultCloud – 支持数据互传的网盘管理
查看>>
MySQL 8.0.23中复制架构从节点自动故障转移
查看>>
MySQL 8.0开始Group by不再排序
查看>>
mysql ansi nulls_SET ANSI_NULLS ON SET QUOTED_IDENTIFIER ON 什么意思
查看>>
multi swiper bug solution
查看>>