博客
关于我
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/

    你可能感兴趣的文章
    python和c混合编程 github_在pycharm中使用git版本管理以及同步github的方法
    查看>>
    python命名空间更改_Python模块xml.etree.ElementTree自动修改xml命名空间键
    查看>>
    Python命名中尾随下划线有什么好处?
    查看>>
    Python启动脚本
    查看>>
    Python听写汇总结果
    查看>>
    python吊打Excel?屁!那是你不会用!
    查看>>
    Python合并两张图片 | 先叠透明度再合并 (附Demo)
    查看>>
    python各进制的表述与转换
    查看>>
    python各种库安装
    查看>>
    python可视化matplotlib_如何使用Python中最强大的可视化工具Matplotlib?
    查看>>
    python可维护性_使用这7大神器,让你的Python 代码更容易于维护
    查看>>
    python变量的详细教程_Python零基础入门教程之语法入门[变量](第二期)
    查看>>
    Python变量命名方法-ChatGPT4o作答
    查看>>
    Python变量与运算符
    查看>>
    Python变量/运算符/函数/模块/string
    查看>>
    python发送邮件的时候出现 error (535, b‘5.7.3 Authentication unsuccessful‘) 解决方法
    查看>>
    python发送notes邮件_使用python在Lotus Notes中发送邮件
    查看>>
    Python双版本下创建一个Scrapy(西瓜皮)项目
    查看>>
    Python双版本下No module named 'requests'
    查看>>
    python及pycharm2018软件安装教程
    查看>>