Dictionary实现的缓存类Cache

 最近用到缓存,所以就自己动手写了一个简单的Cache.

命名空间JChen.Ext有这个方法:

        /// <summary>
        /// 是否为NULL或者空
        /// </summary> /// <param name="str"></param> /// <returns></returns> public static bool IsNullOrEmpty(this string str) { if (str == null || str.Trim() == "") { return true; } return false; }
IsNullOrEmpty

 

using System;
using System.Collections.Generic;
using JChen.Ext; namespace JChen.Caching { /// <summary> /// 缓存 /// </summary> /// <typeparam name="T"></typeparam> public class Cache<T> { /// <summary> ////// </summary> private static object lockObj = new object(); /// <summary> /// 默认Cache /// </summary> private static Cache<T> _default = new Cache<T>(true); /// <summary> /// 每个实例的缓存 /// </summary> private Dictionary<string, T> instanceCache = null; /// <summary> /// 默认的缓存 /// </summary> private static Dictionary<string, T> defaultCache = new Dictionary<string, T>(); /// <summary> /// 缓存 /// </summary> public Cache() { instanceCache = new Dictionary<string, T>(); } /// <summary> /// 私有构造,区别于Cache(),只是为了实例化_default; /// 对于默认的缓存,不需要且不能实例化instanceCache /// </summary> /// <param name="isDefualt"></param> private Cache(bool isDefualt) { } /// <summary> /// 相对于T的默认缓存实例 /// 特别注意:对于相同的类型T,Default是同一个对象 /// </summary> public static Cache<T> Default { get { return _default; } } /// <summary> /// 设置或获取缓存数据 /// </summary> /// <param name="key">键,不能为空</param> /// <returns>如果键为空,返回值为类型T的默认值</returns> public T this[string key] { get { if (instanceCache != null) { if (instanceCache.ContainsKey(key)) { return instanceCache[key]; } } else { if (defaultCache.ContainsKey(key)) { return defaultCache[key]; } } return default(T); } set { if (key.IsNullOrEmpty()) { throw new ArgumentNullException("key", "键不能为空!"); } if (instanceCache != null) { if (instanceCache.ContainsKey(key)) { instanceCache[key] = value; } else { lock (lockObj) { instanceCache.Add(key, value); } } } else { if (defaultCache.ContainsKey(key)) { defaultCache[key] = value; } else { lock (lockObj) { defaultCache.Add(key, value); } } } } } /// <summary> /// 缓存数量 /// </summary> public int Count { get { if (instanceCache != null) { return instanceCache.Count; } else { return defaultCache.Count; } } } /// <summary> /// 移除指定键的缓存值 /// </summary> /// <param name="key">键,不能为空</param> /// <returns></returns> public bool Remove(string key) { if (key.IsNullOrEmpty()) { return false; } if (instanceCache != null) { if (instanceCache.ContainsKey(key)) { return instanceCache.Remove(key); } } else { if (defaultCache.ContainsKey(key)) { return defaultCache.Remove(key); } } return false; } /// <summary> /// 清空缓存 /// </summary> public void Clear() { if (instanceCache != null) { instanceCache.Clear(); } else { defaultCache.Clear(); } } } }
Cache

 

转载于:https://www.cnblogs.com/jchen361/p/4016392.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值