C# CacheHelper
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using System.Threading;
using System.Web; namespace Utils
{
/// <summary>
/// 自定义字典缓存帮助类
/// </summary>
/// <typeparam name="TKey"></typeparam>
/// <typeparam name="TValue"></typeparam>
public class SynchronisedDictionary<TKey, TValue> : IDictionary<TKey, TValue>
{
private Dictionary<TKey, TValue> innerDict; // 缓存内容
private ReaderWriterLockSlim readWriteLock; // 读写锁 /// <summary>
/// 初始化构造函数
/// </summary>
/// <param name="dic"></param>
public SynchronisedDictionary(Dictionary<TKey, TValue> dic)
{
this.readWriteLock = new ReaderWriterLockSlim();
this.innerDict = dic ?? new Dictionary<TKey, TValue>();
} /// <summary>
/// 使用lambda初始化构造函数
/// </summary>
/// <param name="getKey"></param>
/// <param name="list"></param>
public SynchronisedDictionary(Func<TValue, TKey> getKey, List<TValue> list)
{
this.readWriteLock = new ReaderWriterLockSlim();
this.innerDict = new Dictionary<TKey, TValue>(); if (list != null && list.Count > )
{
foreach (var item in list)
{
var key = getKey(item);
if (this.innerDict.ContainsKey(key))
{
this.innerDict[key] = item;
}
else
{
this.innerDict.Add(getKey(item), item);
}
}
}
} /// <summary>
/// 同步缓存
/// </summary>
/// <param name="key">缓存键</param>
/// <param name="value">缓存值</param>
/// <param name="del">是否删除</param>
/// <remarks>
/// 新增:SyncCache(Id,Value,false)
/// 修改:SyncCache(Id,Value,false)
/// 删除:SyncCache(Id,null,true)
/// </remarks>
public void SyncCache(TKey key, TValue value, bool del = false)
{
if (del)
{
Remove(key);
}
else
{
this[key] = value;
}
} /// <summary>
/// 通过KeyValuePair新增缓存(建议使用SyncCache方法)
/// </summary>
/// <param name="item">KeyValuePair键值对</param>
public void Add(KeyValuePair<TKey, TValue> item)
{
using (new AcquireWriteLock(this.readWriteLock))
{
this.innerDict[item.Key] = item.Value;
}
} /// <summary>
/// 根据Key,Value新增缓存(建议使用SyncCache方法)
/// </summary>
/// <param name="key">缓存键</param>
/// <param name="value">缓存值</param>
public void Add(TKey key, TValue value)
{
using (new AcquireWriteLock(this.readWriteLock))
{
this.innerDict[key] = value;
}
} /// <summary>
/// 移除缓存(建议使用SyncCache方法)
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
public bool Remove(TKey key)
{
bool isRemoved;
using (new AcquireWriteLock(this.readWriteLock))
{
isRemoved = this.innerDict.Remove(key);
}
return isRemoved;
} /// <summary>
/// 移除缓存(建议使用SyncCache方法)
/// </summary>
/// <param name="item"></param>
/// <returns></returns>
public bool Remove(KeyValuePair<TKey, TValue> item)
{
using (new AcquireWriteLock(this.readWriteLock))
{
return this.innerDict.Remove(item.Key);
}
} /// <summary>
/// 清空所有缓存
/// </summary>
public void Clear()
{
using (new AcquireWriteLock(this.readWriteLock))
{
this.innerDict.Clear();
}
} /// <summary>
/// 是否包含指定元素
/// </summary>
/// <param name="item">KeyValuePair键值对</param>
/// <returns></returns>
public bool Contains(KeyValuePair<TKey, TValue> item)
{
using (new AcquireReadLock(this.readWriteLock))
{
return this.innerDict.Contains<KeyValuePair<TKey, TValue>>(item);
}
} /// <summary>
/// 是否包含指定的键
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
public bool ContainsKey(TKey key)
{
using (new AcquireReadLock(this.readWriteLock))
{
return this.innerDict.ContainsKey(key);
}
} /// <summary>
/// copy到指定Array中
/// </summary>
/// <param name="array"></param>
/// <param name="arrayIndex"></param>
public void CopyTo(KeyValuePair<TKey, TValue>[] array, int arrayIndex)
{
using (new AcquireReadLock(this.readWriteLock))
{
this.innerDict.ToArray<KeyValuePair<TKey, TValue>>().CopyTo(array, arrayIndex);
}
} /// <summary>
/// 获取枚举数
/// 可用foreach
/// </summary>
/// <returns></returns>
public IEnumerator GetEnumerator()
{
using (new AcquireReadLock(this.readWriteLock))
{
return this.innerDict.GetEnumerator();
}
} IEnumerator<KeyValuePair<TKey, TValue>> IEnumerable<KeyValuePair<TKey, TValue>>.GetEnumerator()
{
using (new AcquireReadLock(this.readWriteLock))
{
return this.innerDict.GetEnumerator();
}
} /// <summary>
/// 获取缓存
/// </summary>
/// <param name="key">缓存键</param>
/// <param name="value">缓存值</param>
/// <returns></returns>
public bool TryGetValue(TKey key, out TValue value)
{
using (new AcquireReadLock(this.readWriteLock))
{
return this.innerDict.TryGetValue(key, out value);
}
} /// <summary>
/// 获取缓存的所有KEY
/// </summary>
public ICollection<TKey> Keys
{
get
{
using (new AcquireReadLock(this.readWriteLock))
{
return this.innerDict.Keys;
}
}
} /// <summary>
/// 获取缓存的所有VALUE
/// </summary>
public ICollection<TValue> Values
{
get
{
using (new AcquireReadLock(this.readWriteLock))
{
return this.innerDict.Values;
}
}
} /// <summary>
/// 获取缓存长度
/// </summary>
public int Count
{
get
{
using (new AcquireReadLock(this.readWriteLock))
{
return this.innerDict.Count;
}
}
} /// <summary>
/// TValue属性读写
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
public TValue this[TKey key]
{
get
{
using (new AcquireReadLock(this.readWriteLock))
{
return this.innerDict[key];
}
}
set
{
using (new AcquireWriteLock(this.readWriteLock))
{
this.innerDict[key] = value;
}
}
} /// <summary>
/// 是否只读
/// </summary>
public bool IsReadOnly
{
get
{
return false;
}
}
} /// <summary>
/// 自定义列表缓存帮助类
/// </summary>
/// <typeparam name="TValue"></typeparam>
public class SynchronisedList<TValue> : IList<TValue>
{
private List<TValue> innerList; // 缓存内容
private ReaderWriterLockSlim readWriteLock; // 读写锁 /// <summary>
/// 初始化构造函数
/// </summary>
/// <param name="list"></param>
public SynchronisedList(IEnumerable<TValue> list)
{
innerList = new List<TValue>();
readWriteLock = new ReaderWriterLockSlim();
if (list != null && list.Count() > )
{
this.innerList.AddRange(list);
}
} /// <summary>
/// 获取缓存内容列表
/// </summary>
/// <returns></returns>
public List<TValue> GetList()
{
return this.innerList;
} /// <summary>
/// 获取索引
/// </summary>
/// <param name="item"></param>
/// <returns></returns>
public int IndexOf(TValue item)
{
using (new AcquireReadLock(this.readWriteLock))
{
return innerList.IndexOf(item);
}
} /// <summary>
/// 根据索引插入值
/// </summary>
/// <param name="index"></param>
/// <param name="item"></param>
public void Insert(int index, TValue item)
{
using (new AcquireWriteLock(this.readWriteLock))
{
innerList.Insert(index, item);
}
} /// <summary>
/// 根据索引移除值
/// </summary>
/// <param name="index"></param>
public void RemoveAt(int index)
{
using (new AcquireWriteLock(this.readWriteLock))
{
innerList.RemoveAt(index);
}
} /// <summary>
/// TValue属性读写
/// </summary>
/// <param name="index"></param>
/// <returns></returns>
public TValue this[int index]
{
get
{
using (new AcquireReadLock(this.readWriteLock))
{
return innerList[index];
}
}
set
{
using (new AcquireWriteLock(this.readWriteLock))
{
innerList[index] = value;
}
}
} /// <summary>
/// 插入值
/// </summary>
/// <param name="item"></param>
public void Add(TValue item)
{
using (new AcquireWriteLock(this.readWriteLock))
{
innerList.Add(item);
}
} /// <summary>
/// 清除缓存
/// </summary>
public void Clear()
{
using (new AcquireWriteLock(this.readWriteLock))
{
innerList.Clear();
}
} /// <summary>
/// 是否包含
/// </summary>
/// <param name="item"></param>
/// <returns></returns>
public bool Contains(TValue item)
{
using (new AcquireReadLock(this.readWriteLock))
{
return innerList.Contains(item);
}
} /// <summary>
/// copy到指定Array中
/// </summary>
/// <param name="array"></param>
/// <param name="arrayIndex"></param>
public void CopyTo(TValue[] array, int arrayIndex)
{
using (new AcquireReadLock(this.readWriteLock))
{
innerList.CopyTo(array, arrayIndex);
}
} /// <summary>
/// 获取缓存长度
/// </summary>
public int Count
{
get
{
using (new AcquireReadLock(this.readWriteLock))
{
return innerList.Count;
}
}
} /// <summary>
/// 只读属性
/// </summary>
public bool IsReadOnly
{
get
{
return false;
}
} /// <summary>
/// 移除值
/// </summary>
/// <param name="item"></param>
/// <returns></returns>
public bool Remove(TValue item)
{
using (new AcquireWriteLock(this.readWriteLock))
{
return innerList.Remove(item);
}
} /// <summary>
/// 获取枚举数
/// 可用foreach
/// </summary>
/// <returns></returns>
public IEnumerator<TValue> GetEnumerator()
{
using (new AcquireReadLock(this.readWriteLock))
{
return innerList.GetEnumerator();
}
} IEnumerator IEnumerable.GetEnumerator()
{
using (new AcquireReadLock(this.readWriteLock))
{
return innerList.GetEnumerator();
}
}
} class AcquireReadLock : IDisposable
{
private ReaderWriterLockSlim rwLock;
private bool disposedValue; public AcquireReadLock(ReaderWriterLockSlim rwLock)
{
this.rwLock = new ReaderWriterLockSlim();
this.disposedValue = false;
this.rwLock = rwLock;
this.rwLock.EnterReadLock();
} public void Dispose()
{
this.Dispose(true);
GC.SuppressFinalize(this);
} protected virtual void Dispose(bool disposing)
{
if (!this.disposedValue && disposing)
{
this.rwLock.ExitReadLock();
}
this.disposedValue = true;
}
} class AcquireWriteLock : IDisposable
{
private ReaderWriterLockSlim rwLock;
private bool disposedValue; public AcquireWriteLock(ReaderWriterLockSlim rwLock)
{
this.rwLock = new ReaderWriterLockSlim();
this.disposedValue = false;
this.rwLock = rwLock;
this.rwLock.EnterWriteLock();
} public void Dispose()
{
this.Dispose(true);
GC.SuppressFinalize(this);
} protected virtual void Dispose(bool disposing)
{
if (!this.disposedValue && disposing)
{
this.rwLock.ExitWriteLock();
}
this.disposedValue = true;
}
} public class WebCacheHelper
{
System.Web.Caching.Cache Cache = HttpRuntime.Cache; public void Set(string key, object data)
{
Cache.Insert(key, data);
}
public void Set(string key, object data, DateTime absoluteExpiration, TimeSpan slidingExpiration)
{
Cache.Insert(key, data, null, absoluteExpiration, slidingExpiration);
} public object Get(string Key)
{
return Cache[Key];
} public T Get<T>(string key)
{
return (T)Cache[key];
} public bool IsSet(string key)
{
return Cache[key] != null;
} public void Remove(string Key)
{
if (Cache[Key] != null)
{
Cache.Remove(Key);
}
} public void RemoveByPattern(string pattern)
{
IDictionaryEnumerator enumerator = Cache.GetEnumerator();
Regex rgx = new Regex(pattern, (RegexOptions.Singleline | (RegexOptions.Compiled | RegexOptions.IgnoreCase)));
while (enumerator.MoveNext())
{
if (rgx.IsMatch(enumerator.Key.ToString()))
{
Cache.Remove(enumerator.Key.ToString());
}
}
} public void Clear()
{
IDictionaryEnumerator enumerator = Cache.GetEnumerator();
while (enumerator.MoveNext())
{
Cache.Remove(enumerator.Key.ToString());
}
} }
}
C# CacheHelper的更多相关文章
- 缓存工具类CacheHelper
代码: using System; using System.Collections.Generic; using System.Linq; using System.Text; using Syst ...
- MySqlHelper、CacheHelper
MySqlHelper代码: using System; using System.Collections; using System.Collections.Generic; using Syste ...
- [Cache] C#操作缓存--CacheHelper缓存帮助类 [复制链接]
using System;using System.Web;using System.Collections; namespace DotNet.Utilities{ public class Cac ...
- Asp.net Core CacheHelper 通用缓存帮助类
using System; using Microsoft.Extensions.Caching.Memory; using System.Runtime; namespace UFX.Tools { ...
- [Cache] C#操作缓存--CacheHelper缓存帮助类 (转载)
点击下载 CacheHelper.zip CacheHelper 缓存帮助类 C#怎么操作缓存 怎么设置和取缓存数据,都在这个类里面呢 下面看一下代码吧 /// <summary> /// ...
- CacheHelper工具类的使用
package com.bbcmart.util; import net.sf.ehcache.Cache; import net.sf.ehcache.CacheManager; import ne ...
- C#操作缓存--CacheHelper缓存帮助类
/// <summary>/// 类说明:Assistant/// 联系方式:361983679 /// 更新网站:<a href=\"http://www.cckan. ...
- C#缓存-依赖 CacheHelper
缓存依赖文件或文件夹 //创建缓存依赖项 CacheDependency dep = new CacheDependency(fileName);//Server.MapPath("&quo ...
- C# WebHelper-CookieHelper,CacheHelper,SessionHelper
常用web操作工具类,记录一下,本文记录的工具类,都要求引用 System.Web 1.CookieHelper /// <summary> /// Cookie工具类 /// </ ...
- WebHelper-SessionHelper、CookieHelper、CacheHelper、Tree
ylbtech-Unitity: cs-WebHelper-SessionHelper.CookieHelper.CacheHelper.Tree SessionHelper.cs CookieHel ...
随机推荐
- c#、sql、asp.net、js、ajax、jquery大学知识点笔记
<table cellSpacing="0" cellPadding="0" width="609" height="470 ...
- switch与java,c#的异同
<script type="text/javascript" language="javascript"> //JavaScript控制语句基本和以 ...
- c#事件求解
闲来无聊对于clr一书又重新温习了下,但是看到事件这张后还是有很多的困惑,对于事件能力CLR是这样描述,通知其它对象发生特定的事情. 1.其它对象:是指对于事件的关注者 2.特定的事件:对于满足事件交 ...
- linux终端下文件不同颜色的含义
偶然注意到在终端下花花绿绿的目录显示效果,开始以为只是些特效,后来研究了一下,原来其中有些规律性的东西,总结如下: 蓝色表示目录:
- CSS3(transform/transition/animation) 基础 总结
1.CSS3新增的样式(常用) //颜色透明表示rgba(0,0,0,.5) //圆角(定义角半径)border-radius: 5px 10px 15px 20px; //文字/盒子阴影text-s ...
- ASP.NET WEB API构建基于REST风格
使用ASP.NET WEB API构建基于REST风格的服务实战系列教程[开篇] 最近发现web api很火,园内也有各种大神已经在研究,本人在asp.net官网上看到一个系列教程,原文地址:http ...
- .NET/ASP.NETMVC Model元数据、HtmlHelper、自定义模板、模板的装饰者模式(一)
.NET/ASP.NETMVC Model元数据.HtmlHelper.自定义模板.模板的装饰者模式(一) 阅读目录: 1.开篇介绍 2.Model与View的使用关系(数据上下文DataContex ...
- MongoDB应用介绍之前
MongoDb企业应用实战(一) 写在MongoDB应用介绍之前 故事背景: 本人有幸,经老友( 现为x知名快递公司技术总监 ) 推荐进入中国前三大民营快递公司之一工作,在此非常感谢他,在此也非常 ...
- tcp连接以及网络I/O的几个问题
这段时间在做一些web方面开发的事情,用的Nginx+fast-cgi,计划深入看一下Nginx的内部实现和架构,以方便理解和调优.后面准备写一篇有关Nginx介绍和深度解析的文章,要深入理解web服 ...
- ios开发实践之UIDatePicker(已对之前无法解决的问题做了解答)
需求:要做一个生日选择的控件,但除了选择之外还需要自定义几个控件,跟生日选择控件组合一起. 做法:自定义了一个UIImageView,并且作为背景.在这个背景view上再添加其他button和时间选择 ...