c#自带缓存使用方法 c#移除清理缓存
程序员文章站
2023-12-20 23:44:40
复制代码 代码如下:/// /// 获取数据缓存/// ///
/// <summary>
/// 获取数据缓存
/// </summary>
/// <param name="cachekey">键</param>
public static object getcache(string cachekey)
{
system.web.caching.cache objcache = httpruntime.cache;
return objcache[cachekey];
}
/// <summary>
/// 设置数据缓存
/// </summary>
public static void setcache(string cachekey, object objobject)
{
system.web.caching.cache objcache = httpruntime.cache;
objcache.insert(cachekey, objobject);
}
/// <summary>
/// 设置数据缓存
/// </summary>
public static void setcache(string cachekey, object objobject, timespan timeout)
{
system.web.caching.cache objcache = httpruntime.cache;
objcache.insert(cachekey, objobject, null, datetime.maxvalue, timeout, system.web.caching.cacheitempriority.notremovable, null);
}
/// <summary>
/// 设置数据缓存
/// </summary>
public static void setcache(string cachekey, object objobject, datetime absoluteexpiration, timespan slidingexpiration)
{
system.web.caching.cache objcache = httpruntime.cache;
objcache.insert(cachekey, objobject, null, absoluteexpiration, slidingexpiration);
}
/// <summary>
/// 移除指定数据缓存
/// </summary>
public static void removeallcache(string cachekey)
{
system.web.caching.cache _cache = httpruntime.cache;
_cache.remove(cachekey);
}
/// <summary>
/// 移除全部缓存
/// </summary>
public static void removeallcache()
{
system.web.caching.cache _cache = httpruntime.cache;
idictionaryenumerator cacheenum = _cache.getenumerator();
while (cacheenum.movenext())
{
_cache.remove(cacheenum.key.tostring());
}
}
复制代码 代码如下:
/// <summary>
/// 获取数据缓存
/// </summary>
/// <param name="cachekey">键</param>
public static object getcache(string cachekey)
{
system.web.caching.cache objcache = httpruntime.cache;
return objcache[cachekey];
}
/// <summary>
/// 设置数据缓存
/// </summary>
public static void setcache(string cachekey, object objobject)
{
system.web.caching.cache objcache = httpruntime.cache;
objcache.insert(cachekey, objobject);
}
/// <summary>
/// 设置数据缓存
/// </summary>
public static void setcache(string cachekey, object objobject, timespan timeout)
{
system.web.caching.cache objcache = httpruntime.cache;
objcache.insert(cachekey, objobject, null, datetime.maxvalue, timeout, system.web.caching.cacheitempriority.notremovable, null);
}
/// <summary>
/// 设置数据缓存
/// </summary>
public static void setcache(string cachekey, object objobject, datetime absoluteexpiration, timespan slidingexpiration)
{
system.web.caching.cache objcache = httpruntime.cache;
objcache.insert(cachekey, objobject, null, absoluteexpiration, slidingexpiration);
}
/// <summary>
/// 移除指定数据缓存
/// </summary>
public static void removeallcache(string cachekey)
{
system.web.caching.cache _cache = httpruntime.cache;
_cache.remove(cachekey);
}
/// <summary>
/// 移除全部缓存
/// </summary>
public static void removeallcache()
{
system.web.caching.cache _cache = httpruntime.cache;
idictionaryenumerator cacheenum = _cache.getenumerator();
while (cacheenum.movenext())
{
_cache.remove(cacheenum.key.tostring());
}
}