时间:2021-05-20
本文实例讲述了C#中Memcached缓存的用法,分享给大家供大家参考。具体方法如下:
ICacheStrategy.cs文件如下:
复制代码 代码如下:public interface ICacheStrategy
{
/// <summary>
/// 添加数据到缓存
/// </summary>
/// <param name="objId">缓存名称</param>
/// <param name="o">缓存内容</param>
/// <param name="second">缓存时间(秒)</param>
void Add(string objId, object o, int second);
/// <summary>
/// 添加数据到缓存 (依赖其它缓存)
/// </summary>
/// <param name="objId">缓存名称</param>
/// <param name="o">缓存内容</param>
/// <param name="second">缓存时间(秒)</param>
/// <param name="dependKey">依赖缓存名称数组</param>
void AddCacheDepend(string objId, object o, int second, string[] dependKey);
/// <summary>
/// 添加数据到缓存 (依赖文件)
/// </summary>
/// <param name="objId">缓存名称</param>
/// <param name="o">缓存内容</param>
/// <param name="second">缓存时间(秒)</param>
/// <param name="files">依赖缓存名称文件名数组</param>
void AddFileDepend(string objId, object o, int second, string[] files);
/// <summary>
/// 删除指定缓存
/// </summary>
/// <param name="objId"></param>
void RemoveCache(string objId);
/// <summary>
/// 删除所有缓存
/// </summary>
void RemoveCacheAll();
/// <summary>
/// 删除匹配到的缓存
/// </summary>
/// <param name="pattern"></param>
/// <returns></returns>
void RemoveCacheRegex(string pattern);
/// <summary>
/// 获取所有缓存键
/// </summary>
/// <returns></returns>
IList<string> GetCacheKeys();
/// <summary>
/// 搜索 匹配到的缓存
/// </summary>
/// <param name="pattern"></param>
/// <returns></returns>
IList<string> SearchCacheRegex(string pattern);
/// <summary>
/// 获得缓存数据
/// </summary>
/// <param name="objId"></param>
/// <returns></returns>
object GetCacheData(string objId);
/// <summary>
/// 判断此缓存是否有效
/// </summary>
/// <param name="objID"></param>
/// <returns></returns>
bool HasCache(string objID);
}
MemcachedStrategy.cs文件如下:
复制代码 代码如下:public class MemcachedStrategy : ICacheStrategy
{
#region ICacheStrategy 成员
/// <summary>
/// 添加数据到缓存
/// </summary>
/// <param name="objId">缓存名称</param>
/// <param name="o">缓存内容</param>
/// <param name="second">缓存时间(秒)</param>
public void Add(string objId, object o, int second)
{
if (second > 0)
{
MemcachedManager.CacheClient.Set(objId, o, DateTime.Now.AddSeconds(second));
}
else
{
MemcachedManager.CacheClient.Set(objId, o);
}
}
#region 缓存依赖没有用到memcached
/// <summary>
/// 添加数据到缓存 (依赖其它缓存)
/// </summary>
/// <param name="objId">缓存名称</param>
/// <param name="o">缓存内容</param>
/// <param name="second">缓存时间(秒)</param>
/// <param name="dependKey">依赖缓存名称数组</param>
public void AddCacheDepend(string objId, object o, int second, string[] dependKey)
{
CacheDependency dependencies = new CacheDependency(null, dependKey, DateTime.Now);
HttpRuntime.Cache.Insert(objId, o, dependencies, DateTime.Now.AddSeconds((double)second), Cache.NoSlidingExpiration, CacheItemPriority.Default, null);
}
/// <summary>
/// 添加数据到缓存 (依赖文件)
/// </summary>
/// <param name="objId">缓存名称</param>
/// <param name="o">缓存内容</param>
/// <param name="second">缓存时间(秒)</param>
/// <param name="files">依赖缓存名称文件名数组</param>
public void AddFileDepend(string objId, object o, int second, string[] files)
{
CacheDependency dependencies = new CacheDependency(files, DateTime.Now);
HttpRuntime.Cache.Insert(objId, o, dependencies, DateTime.Now.AddSeconds((double)second), Cache.NoSlidingExpiration, CacheItemPriority.Default, null);
}
#endregion
/// <summary>
/// 删除指定缓存
/// </summary>
/// <param name="objId"></param>
public void RemoveCache(string objId)
{
if (MemcachedManager.CacheClient.KeyExists(objId))
MemcachedManager.CacheClient.Delete(objId);
}
/// <summary>
/// 删除所有缓存
/// </summary>
public void RemoveCacheAll()
{
MemcachedManager.CacheClient.FlushAll();
}
/// <summary>
/// 删除匹配到的缓存
/// </summary>
/// <param name="pattern"></param>
public void RemoveCacheRegex(string pattern)
{
IList<string> list = SearchCacheRegex(pattern);
foreach (var item in list)
{
MemcachedManager.CacheClient.Delete(item);
}
}
/// <summary>
/// 获取所有缓存键
/// </summary>
/// <returns></returns>
public IList<string> GetCacheKeys()
{
return MemcachedManager.GetAllKeys();
}
/// <summary>
/// 搜索 匹配到的缓存
/// </summary>
/// <param name="pattern"></param>
/// <returns></returns>
public IList<string> SearchCacheRegex(string pattern)
{
List<string> l = new List<string>();
IList<string> cacheKeys = MemcachedManager.GetAllKeys();
foreach (var item in cacheKeys)
{
if (Regex.IsMatch(item, pattern))
{
l.Add(item);
}
}
return l.AsReadOnly();
}
/// <summary>
/// 获得缓存数据
/// </summary>
/// <param name="objId"></param>
/// <returns></returns>
public object GetCacheData(string objId)
{
return MemcachedManager.CacheClient.Get(objId);
}
/// <summary>
/// 判断此缓存是否有效
/// </summary>
/// <param name="objID"></param>
/// <returns></returns>
public bool HasCache(string objID)
{
return MemcachedManager.CacheClient.KeyExists(objID);
}
#endregion
}
希望本文所述对大家的C#程序设计有所帮助。
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
本文实例讲述了C#自定义缓存封装类。分享给大家供大家参考。具体如下:这个自定义的C#类封装了部分常用的缓存操作,包括写入缓存,读取缓存,设置缓存过期时间等等,简
C#动态创建button按钮的方法实例详解C#编程中经常需要动态创建,本文主要介绍C#动态创建button按钮的方法,涉及C#按钮属性动态设置的相关技巧,以供借
C#中Array和ArrayList详解及区别一、Array的用法type[]typename=newtype[size];或者type[]typename=n
本文初步探讨了C#缓存的原理及应用,并以实例加以分析总结,这些对C#初学者来说是很有必要熟练掌握的内容。具体如下:一、概述:缓存应用目的:缓存主要是为了提高数据
本文实例汇总了C#中@的用法,对C#程序设计来说有不错的借鉴价值。具体如下:一字符串中的用法1.学过C#的人都知道C#中字符串常量可以以@开头声名,这样的优点是