C#——HTTP缓存

1、读取缓存项

public static object CacheReader(string cacheKey)
{
    return HttpRuntime.Cache[cacheKey];
}

2、写入缓存项

public static void CacheWriter(string cacheKey, object cacheValue, int cache_time = 0)
{
    HttpRuntime.Cache.Insert(cacheKey, cacheValue, null,
        DateTime.Now.AddMinutes(cache_time <= 0 ? cacheTime : cache_time),
        Cache.NoSlidingExpiration);
}

3、移除指定缓存项

public static void CacheRemove(string cacheName)
{
    HttpRuntime.Cache.Remove(cacheName);
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
C#对数据集缓存 /// <summary> /// 获取当前应用程序指定CacheKey的Cache值 /// </summary> /// <param name="CacheKey"></param> /// <returns></returns> public static object GetCache(string CacheKey) { System.Web.Caching.Cache objCache = HttpRuntime.Cache; return objCache[CacheKey]; } /// <summary> /// 设置当前应用程序指定CacheKey的Cache值 /// </summary> /// <param name="CacheKey"></param> /// <param name="objObject"></param> public static void SetCache(string CacheKey, object objObject) { System.Web.Caching.Cache objCache = HttpRuntime.Cache; objCache.Insert(CacheKey, objObject); } /// <summary> /// 设置当前应用程序指定CacheKey的Cache值 /// </summary> /// <param name="CacheKey"></param> /// <param name="objObject"></param> 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> /// 设置当前应用程序指定CacheKey的Cache值(可不设置绝对过期事件) /// </summary> /// <param name="CacheKey"></param> /// <param name="objObject"></param> /// <param name="slidingExpiration"></param> public static void SetCache(string CacheKey, object objObject, TimeSpan slidingExpiration) { System.Web.Caching.Cache objCache = HttpRuntime.Cache; objCache.Insert(CacheKey, objObject, null, Cache.NoAbsoluteExpiration, slidingExpiration); } public static void RemoveCache(string cacheKey) { System.Web.Caching.Cache objCache = HttpRuntime.Cache; if (objCache[cacheKey] != null) objCache.Remove(cacheKey); }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值