C# Memcached缓存用法实例详解
本文实例讲述了c#中memcached缓存的用法,分享给大家供大家参考。具体方法如下:
icachestrategy.cs文件如下:
{
/// <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文件如下:
{
#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#程序设计有所帮助。