C# Memcached緩存用法實例詳解。本站提示廣大學習愛好者:(C# Memcached緩存用法實例詳解)文章只能為提供參考,不一定能成為您想要的結果。以下是C# Memcached緩存用法實例詳解正文
本文實例講述了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#法式設計有所贊助。