HttpCache缓存扩展方法
using System;
using System.Collections;
using System.Configuration;
using System.Web;
using System.Web.Caching;
namespace Meb.Common.Extensions
{
//缓存写入
//验证缓存是否存在
//缓存读取 按缓存索引读取相应缓存值
public static class CacheExtension
{
//private static int CacheMinute = int.Parse(ConfigurationManager.AppSettings["CacheMinute"] ?? "30");
/// <summary>
/// 获取当前应用程序指定CacheKey的Cache值
/// </summary>
/// <param name="cacheKey"></param>
/// <returns></returns>
public static object GetCache(string cacheKey)
{
var objCache = HttpRuntime.Cache;
return objCache[cacheKey];
}
/// <summary>
/// 查询指定CacheKey的Cache值是否存在
/// </summary>
/// <param name="cacheKey"></param>
/// <returns></returns>
public static bool IsExistsCache(string cacheKey)
{
var objCache = HttpRuntime.Cache;
return objCache[cacheKey] != null;
}
/// <summary>
/// 设置当前应用程序指定CacheKey的Cache值
/// </summary>
/// <param name="cacheKey"></param>
/// <param name="objObject"></param>
public static void SetCache(string cacheKey, object objObject)
{
var objCache = HttpRuntime.Cache;
objCache.Insert(cacheKey, objObject);
}
/// <summary>
/// 设置当前应用程序指定CacheKey的Cache值
/// </summary>
/// <param name="cacheKey"></param>
/// <param name="objObject"></param>
/// <param name="absoluteExpiration"></param>
/// <param name="slidingExpiration"></param>
public static void SetCache(string cacheKey, object objObject, DateTime absoluteExpiration, TimeSpan slidingExpiration)
{
var objCache = HttpRuntime.Cache;
objCache.Insert(cacheKey, objObject, null, absoluteExpiration, slidingExpiration);
}
/// <summary>
/// 设置数据缓存
/// </summary>
public static void SetCache(string cacheKey, object objObject, TimeSpan timeout)
{
var objCache = HttpRuntime.Cache;
objCache.Insert(cacheKey, objObject, null, DateTime.MaxValue, timeout, CacheItemPriority.NotRemovable, null);
}
/// <summary>
/// 移除指定数据缓存
/// </summary>
public static void RemoveAllCache(string cacheKey)
{
var cache = HttpRuntime.Cache;
cache.Remove(cacheKey);
}
/// <summary>
/// 移除全部缓存
/// </summary>
public static void RemoveAllCache()
{
var cache = HttpRuntime.Cache;
var cacheEnum = cache.GetEnumerator();
while (cacheEnum.MoveNext())
{
cache.Remove(cacheEnum.Key.ToString());
}
}
private static int CacheMinute = int.Parse(ConfigurationManager.AppSettings["CacheMinute"] ?? "30");
private const string ApplicationCacheKeyFormat = "Application_{0}";
private const string UserInfoCacheKeyFormat = "UserInfo_{0}";
private const string RolePopedomCacheKeyFormat = "RolePopedom_{0}";
public static void InsertForSlidingExpiration(this Cache cache, string key, object value)
{
cache.Insert(key, value, null, Cache.NoAbsoluteExpiration, TimeSpan.FromMinutes(CacheMinute));
}
public static T Get<T>(this Cache cache, object dataCacheKey, Func<object[], T> getService) where T : class
{
var cacheKey = string.Format("{0}_{1}", typeof(T).FullName, dataCacheKey);
var data = cache.Get(cacheKey) as T;
if (data != null)
{
return data;
}
data = getService(new object[] { dataCacheKey });
if (data != null)
{
cache.InsertForSlidingExpiration(cacheKey, data);
return data;
}
return null;
}
public static void Remove<T>(this Cache cache, object dataCacheKey)
{
var cacheKey = string.Format("{0}_{1}", typeof(T).FullName, dataCacheKey);
cache.Remove(cacheKey);
}
}
}
HttpCache缓存扩展方法的更多相关文章
- AbpVnext使用分布式IDistributedCache Redis缓存(自定义扩展方法)
AbpVnext使用分布式IDistributedCache缓存from Redis(带自定义扩展方法) 我的依赖包的主要版本以及Redis依赖如下 1:添加依赖 <PackageReferen ...
- MVC缓存03,扩展方法实现视图缓存
关于缓存,先前尝试了: ● 在"MVC缓存01,使用控制器缓存或数据层缓存"中,分别在控制器和Data Access Layer实现了缓存 ● 在"MVC缓存02,使用数 ...
- 【开源】OSharp框架解说系列(3):扩展方法
OSharp是什么? OSharp是个快速开发框架,但不是一个大而全的包罗万象的框架,严格的说,OSharp中什么都没有实现.与其他大而全的框架最大的不同点,就是OSharp只做抽象封装,不做实现.依 ...
- c# 扩展方法奇思妙用基础篇八:Distinct 扩展(转载)
转载地址:http://www.cnblogs.com/ldp615/archive/2011/08/01/distinct-entension.html 刚看了篇文章 <Linq的Distin ...
- PHP 缓存扩展opcache
opcache (全程 zend opcache): 从php5.5开始,默认提供的php脚本缓存扩展,编译php5.5时加上参数--enable-opcache就可以编译opcache了,只是要启用 ...
- linux下清除Squid缓存的方法记录
在日常运维工作中,只要用到squid缓存服务,就会常常被要求清理squid缓存.比如公司领导要求删一篇新闻,新闻是生成的静态.运维人员把服务器上静态的新闻页面删除了后,不料代理服务器上缓存还有.缓存服 ...
- SmartWiki开发日记之Laravel缓存扩展
SmartWiki简介请阅读: http://www.cnblogs.com/lifeil/p/6113323.html 因为SmartWiki的演示站点部署在阿里云上,阿里云有一个128M免费的Me ...
- 一个利用扩展方法的实例:AttachDataExtensions
扩展方法是C# 3.0(老赵对VB不熟)中最简单,也是最常用的语言特性之一.这是老赵自以为的一个简单却不失经典的实例: [AttributeUsage(AttributeTargets.All, Al ...
- php的opcache缓存扩展
opcache (全程 zend opcache): 从php5.5开始,默认提供的php脚本缓存扩展,编译php5.5时加上参数--enable-opcache就可以编译opcache了,只是要启用 ...
随机推荐
- Windows Server 2008 R2 NTP服务器
Server 1.查看服务器信息 w32tm /query /status 2.设置同步地址 w32tm /config /manualpeerlist:time.windows.com /syncf ...
- CANopen学习——感性认知
看不懂的知识硬着头皮也要看.读了当时虽然不理解,但脑子里对其相关名词.概念有印象,继续看下去,多读几遍,一定会在某个地方顿悟. CAN总线只是定义了物理层和数据链路层,并没有定义应用层.这么优秀的总 ...
- html之select标签
循环select标签 <select name="group_id"> {% for row in group_list %} <option value={{r ...
- [LeetCode] Contains Duplicate III 包含重复值之三
Given an array of integers, find out whether there are two distinct indices i and j in the array suc ...
- [LeetCode] Palindrome Partitioning II 拆分回文串之二
Given a string s, partition s such that every substring of the partition is a palindrome. Return the ...
- 在SpringMVC中使用@SessionAttributes和@ModelAttribute将数据存储在session域中
今天在我的springMVC项目--图书管理系统中,希望在登录时将登录的Users存在session中,开始是准备在controller中使用Servlet API中的对象,可是一直无法引用,不知道为 ...
- KMP算法实现
链接:http://blog.csdn.net/joylnwang/article/details/6778316 KMP算法是一种很经典的字符串匹配算法,链接中的讲解已经是很明确得了,自己按照其讲解 ...
- Mysql两个引擎对比
Mysql两个引擎对比 MyIsam 优点: 1.支持B-Tree检索和文本全文检索 2.性能消耗方面相对较低 3.支持全表(table)锁 缺点: ...
- BZOJ-2127-happiness(最小割)
2127: happiness(题解) Time Limit: 51 Sec Memory Limit: 259 MBSubmit: 1806 Solved: 875 Description 高一 ...
- spring架构源码:
p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px "Helvetica Neue"; color: #454545 } p. ...