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

    你可能感兴趣的文章
    Objective-C实现hamiltonianCycle哈密尔顿图算法(附完整源码)
    查看>>
    Objective-C实现hamming code汉明码算法(附完整源码)
    查看>>
    Objective-C实现hamming numbers汉明数算法(附完整源码)
    查看>>
    Objective-C实现hammingDistance汉明距离算法(附完整源码)
    查看>>
    Objective-C实现hanning 窗(附完整源码)
    查看>>
    Objective-C实现hanoiTower汉诺塔算法(附完整源码)
    查看>>
    Objective-C实现hardy ramanujana定理算法(附完整源码)
    查看>>
    Objective-C实现harmonic series调和级数算法(附完整源码)
    查看>>
    Objective-C实现harris算法(附完整源码)
    查看>>
    Objective-C实现HashTable哈希表算法(附完整源码)
    查看>>
    Objective-C实现haversine distance斜距算法(附完整源码)
    查看>>
    Objective-C实现heap sort堆排序算法(附完整源码)
    查看>>
    Objective-C实现heaps algorithm堆算法(附完整源码)
    查看>>
    Objective-C实现heap堆算法(附完整源码)
    查看>>
    Objective-C实现Heap堆算法(附完整源码)
    查看>>
    Objective-C实现hexagonal numbers六边形数算法(附完整源码)
    查看>>
    Objective-C实现hidden layers neural network浅层神经网络算法(附完整源码)
    查看>>
    Objective-C实现highest response ratio next高响应比优先调度算法(附完整源码)
    查看>>
    Objective-C实现hill climbing爬山法用来寻找函数的最大值算法(附完整源码)
    查看>>
    Objective-C实现Hill密码加解密算法(附完整源码)
    查看>>