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了,只是要启用 ...
随机推荐
- struts2+hibernate整合-实现登录功能
最近一直学习struts2+hibernate框架,于是想把两个框架整合到一起,做一个小的登录项目.其他不多说,直接看例子. 1).Struts2 和hibernate的环境配置 包括jar包.web ...
- java中transient关键字的作用
Java有个特点就是序列化,简单地来说就是可以将这个类存储在物理空间(当然还是以文件的形式存在),那么当你从本地还原这个文件时,你可以将它转换为它本身.这可以极大地方便网络上的一些操作,但同时,因为涉 ...
- Warning: strftime(): It is not safe to rely on the system's timezone settings.
当运行程序时,会出现如下警告: Warning: strftime(): It is not safe to rely on the system's timezone settings. You a ...
- Batis-iBatis基本操作(增删改查)
Batis-iBatis基本操作(增删改查) 时间 2014-04-10 17:55:20 CSDN博客 原文 http://blog.csdn.net/mazhaojuan/article/de ...
- spring.net 框架分析(三)ContextRegistry.GetContext()
我们通过ContextRegistry.GetContext()建立了一个IApplicationContext得实例,那么这个实例具体是怎么建立的了. 我们来分析一下容器实例建立的过程: 我们在配置 ...
- [LeetCode] Find All Duplicates in an Array 找出数组中所有重复项
Given an array of integers, 1 ≤ a[i] ≤ n (n = size of array), some elements appear twice and others ...
- [LeetCode] ZigZag Converesion 之字型转换字符串
The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like ...
- C#进阶系列——WebApi 异常处理解决方案
前言:上篇C#进阶系列——WebApi接口传参不再困惑:传参详解介绍了WebApi参数的传递,这篇来看看WebApi里面异常的处理.关于异常处理,作为程序员的我们肯定不陌生,记得在介绍 AOP 的时候 ...
- 常用DOS命令
1.查询端口占用情况:netstat -aon |findstr "8080"; 查看端口进程号: 2.查看进程号信息: tasklist |findstr "999 ...
- ActiveMQ入门实例Demo
前面我们已经搭建和配置好了ActiveMQ,下面来看一个Demo,体验一下MQ. JMS 消息模型 JMS消息服务应用程序结构支持两种模型:点对点模型,发布者/订阅者模型. (1)点对点模型(Queu ...