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

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

为了实现缓存管理功能,我们创建了一个名为 CacheManager 的类,该类旨在简化缓存操作并提供统一的接口。该类继承自 IHttpStorageObject 抽象类,确保与系统内其他组件保持一致性。为了方便调用,我们采用单例模式设计 CacheManager,这意味着只有一个实例可以被多次使用。

主要功能

  • 单例模式实例获取

    使用 GetInstance 方法可以获得 CacheManager 实例。该方法通过锁机制确保单次初始化,保证多线程环境下也能正常工作。

  • 缓存值获取与设置

    • this[key] 方法用于通过键获取缓存值。
    • Get 方法提供更直接的值获取方式。
    • Add 方法用于将键值对添加到缓存中,支持默认超时(20分钟)和自定义超时。
    • Remove 方法用于删除指定键的缓存项。
    • RemoveAll 方法用于清除所有缓存项,支持按键值表达式筛选删除。
  • 缓存项管理

    • ContainsKey 方法用于检查指定键是否存在于缓存中。
    • GetAllKey 方法允许获取所有缓存键,方便管理和清理缓存。
  • 代码示例

    using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Web;using System.Web.Caching;namespace SyntacticSugar{    public class CacheManager
    : IHttpStorageObject
    { private static readonly CacheManager
    _instance; private static readonly object _instanceLock = new object(); private CacheManager() { Minutes = 60; } public static CacheManager
    GetInstance() { lock (_instanceLock) { if (_instance == null) _instance = new CacheManager
    (); } return _instance; } public override T this[string key] { get { return (T)HttpRuntime.Cache[CreateKey(key)]; } } public override bool ContainsKey(string key) { return HttpRuntime.Cache[CreateKey(key)] != null; } public override T Get(string key) { return (T)HttpRuntime.Cache.Get(CreateKey(key)); } public override void Add(string key, T value) { Add(key, value, 20 * Minutes); } public override void Add(string key, T value, int cacheDurationInSeconds) { Add(key, value, cacheDurationInSeconds, CacheItemPriority.Default); } public override void Add(string key, T value, int cacheDurationInSeconds, CacheItemPriority priority) { string keyString = CreateKey(key); HttpRuntime.Cache.Insert(keyString, value, null, DateTime.Now.AddSeconds(cacheDurationInSeconds), Cache.NoSlidingExpiration, priority, null); } public override void Remove(string key) { HttpRuntime.Cache.Remove(CreateKey(key)); } public override void RemoveAll() { var cache = HttpRuntime.Cache; var keys = new List
    (); var enumerator = cache.GetEnumerator(); while (enumerator.MoveNext()) keys.Add((string)enumerator.Key); foreach (var key in keys) cache.Remove(key); } public override void RemoveAll(Func
    removeExpression) { var cache = HttpRuntime.Cache; var keys = cache.GetAllKey().Where(removeExpression).ToList(); foreach (var key in keys) Remove(key); } public override IEnumerable
    GetAllKey() { var enumerator = HttpRuntime.Cache.GetEnumerator(); while (enumerator.MoveNext()) yield return enumerator.Key.ToString(); } private string CreateKey(string key) { return "http_cache_" + key; } }}

    抽象类定义

    using System;using System.Collections.Generic;using System.Linq;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, T value); public abstract bool ContainsKey(string key); public abstract T Get(string key); public abstract IEnumerable
    GetAllKey(); public abstract void Remove(string key); public abstract void RemoveAll(); public abstract void RemoveAll(Func
    removeExpression); public abstract T this[string key] { get; } }}

    本实现通过简化缓存操作和统一接口,为开发者提供了一个高效的缓存管理工具,同时确保了代码的可维护性和可扩展性。

    转载地址:http://feofz.baihongyu.com/

    你可能感兴趣的文章
    php-laravel框架用户验证(Auth)模块解析(一)
    查看>>
    php-laravel框架用户验证(Auth)模块解析(三)登录模块
    查看>>
    php-laravel框架用户验证(Auth)模块解析(二)注册模块
    查看>>
    php-laravel框架用户验证(Auth)模块解析(四)忘记密码
    查看>>
    php-redis中文参考手册_Ping_echo_set_get_setex_psetex_...
    查看>>
    PHP-Shopify-API-Wrapper 使用教程
    查看>>
    php-兔子问题,斐波那契数列
    查看>>
    PHP-希尔排序
    查看>>
    php-数据结构-二叉树的构建、前序遍历,中序遍历,后序遍历,查找,打印
    查看>>
    php-有序数组合并后仍有序
    查看>>
    redis使用
    查看>>
    Redis以及Redis的php扩展安装
    查看>>
    PHP-算法-最少比较次数获取最大值最小值
    查看>>
    php-约瑟夫问题
    查看>>
    Redis从库不能同步报Can’t save in background: fork: Cannot allocate memory错误
    查看>>
    Redis从入门到精通|干货篇
    查看>>
    php.ini maxfileuploads,细说PHP高洛峰文件上传类源文件
    查看>>
    php.ini中常见的配置信息选项
    查看>>
    php.ini配置中有10处设置不当,会使网站存在安全问题
    查看>>
    php/jsp/asp的区别
    查看>>