欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

c#自带缓存使用方法 c#移除清理缓存

程序员文章站 2024-02-24 23:24:34
复制代码 代码如下:/// /// 获取数据缓存/// ///

复制代码 代码如下:

/// <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());
    }
}