话不多说,直接上代码

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Web;
using System.Web.Caching; namespace Tools
{
/// <summary>
/// 缓存辅助类
/// </summary>
public static class CacheHelper
{
#region // 绝对缓存过期时间
public static DateTime Absolute_Minute_1
{
get { return DateTime.Now.AddMinutes(); }
} public static DateTime Absolute_Minute_10
{
get { return DateTime.Now.AddMinutes(); }
} public static DateTime Absolute_Minute_30
{
get { return DateTime.Now.AddMinutes(); }
} public static DateTime Absolute_Hour_1
{
get { return DateTime.Now.AddHours(); }
} public static DateTime Absolute_Hour_2
{
get { return DateTime.Now.AddHours(); }
} public static DateTime Absolute_Hour_5
{
get { return DateTime.Now.AddHours(); }
} public static DateTime Absolute_Hour_12
{
get { return DateTime.Now.AddHours(); }
} public static DateTime Absolute_Day_1
{
get { return DateTime.Now.AddDays(); }
} public static DateTime Absolute_Day_7
{
get { return DateTime.Now.AddDays(); }
} public static DateTime Absolute_Day_14
{
get { return DateTime.Now.AddDays(); }
} public static DateTime Absolute_Day_15
{
get { return DateTime.Now.AddDays(); }
} public static DateTime Absolute_Month_1
{
get { return DateTime.Now.AddMonths(); }
}
#endregion #region // 滑动缓存过期时间
public static TimeSpan Sliding_Minute_1
{
get { return new TimeSpan(TimeSpan.TicksPerMinute); }
} public static TimeSpan Sliding_Minute_10
{
get { return new TimeSpan(TimeSpan.TicksPerMinute * ); }
} public static TimeSpan Sliding_Minute_30
{
get { return new TimeSpan(TimeSpan.TicksPerMinute * ); }
} public static TimeSpan Sliding_Hour_1
{
get { return new TimeSpan(TimeSpan.TicksPerHour); }
} public static TimeSpan Sliding_Hour_2
{
get { return new TimeSpan(TimeSpan.TicksPerHour * ); }
} public static TimeSpan Sliding_Hour_5
{
get { return new TimeSpan(TimeSpan.TicksPerHour * ); }
} public static TimeSpan Sliding_Hour_12
{
get { return new TimeSpan(TimeSpan.TicksPerHour * ); }
} public static TimeSpan Sliding_Day_1
{
get { return new TimeSpan(TimeSpan.TicksPerDay); }
}
#endregion /// <summary>
/// 缓存
/// </summary>
private static Cache cache = HttpRuntime.Cache; /// <summary>
/// 根据键获取缓存数据
/// </summary>
/// <param name="cacheKey">缓存的键</param>
/// <returns></returns>
private static object GetCache(string cacheKey)
{
return cache.Get(cacheKey);
} /// <summary>
/// 设置缓存
/// </summary>
/// <param name="cacheKey">缓存的键</param>
/// <param name="objValue">缓存的值</param>
private static void SetCache(string cacheKey, object objValue)
{
cache.Insert(cacheKey, objValue);
} /// <summary>
/// 设置缓存
/// </summary>
/// <param name="cacheKey">缓存的键</param>
/// <param name="objValue">缓存的值</param>
/// <param name="slidingExpiration">滑动过期时间</param>
private static void SetCache(string cacheKey, object objValue, TimeSpan slidingExpiration)
{
cache.Insert(cacheKey, objValue, null, Cache.NoAbsoluteExpiration, slidingExpiration);
} /// <summary>
/// 设置缓存
/// </summary>
/// <param name="cacheKey">缓存的键</param>
/// <param name="objValue">缓存的值</param>
/// <param name="absoluteExpiration">绝对过期时间</param>
private static void SetCache(string cacheKey, object objValue, DateTime absoluteExpiration)
{
cache.Insert(cacheKey, objValue, null, absoluteExpiration, Cache.NoSlidingExpiration);
} /// <summary>
/// 设置缓存
/// </summary>
/// <param name="cacheKey">缓存的键</param>
/// <param name="objValue">缓存的值</param>
/// <param name="dependency">文件依赖</param>
private static void SetCache(string cacheKey, object objValue, CacheDependency dependency)
{
cache.Insert(cacheKey, objValue, dependency, Cache.NoAbsoluteExpiration, Cache.NoSlidingExpiration);
} /// <summary>
/// 移除指定的缓存
/// </summary>
/// <param name="cacheKey">缓存的键</param>
public static void Remove(string cacheKey)
{
cache.Remove(cacheKey);
} /// <summary>
/// 移除全部缓存
/// </summary>
public static void Remove()
{
IDictionaryEnumerator CacheEnum = cache.GetEnumerator();
while (CacheEnum.MoveNext())
{
Remove(CacheEnum.Key.ToString());
}
} /// <summary>
/// 删除以cacheKeyPrefix为前缀的缓存Key的缓存
/// </summary>
/// <param name="cacheKeyPrefix">缓存键前缀</param>
public static void RemoveByKeyStartsWith(string cacheKeyPrefix)
{
IDictionaryEnumerator CacheEnum = cache.GetEnumerator();
while (CacheEnum.MoveNext())
{
var key = CacheEnum.Key.ToString();
if (key != null && key.StartsWith(cacheKeyPrefix))
{
Remove(key);
}
}
} /// <summary>
/// 从缓存中获取数据。缓存中不存在的时候,从回调方法getDate中获取,并设置进缓存。
/// </summary>
/// <typeparam name="T">缓存的数据类型</typeparam>
/// <param name="cacheKey">缓存的键</param>
/// <param name="getData">回调方法</param>
/// <returns>缓存中的数据</returns>
public static T Get<T>(string cacheKey, Func<T> getData)
{
var data = GetCache(cacheKey);
if (data == null)
{
data = getData();
SetCache(cacheKey, data);
}
return (T)data;
} /// <summary>
/// 从缓存中获取数据。缓存中不存在的时候,从回调方法getDate中获取,并设置进缓存。
/// </summary>
/// <typeparam name="T">缓存的数据类型</typeparam>
/// <param name="cacheKey">缓存的键</param>
/// <param name="slidingExpiration">滑动过期时间</param>
/// <param name="getData">回调方法</param>
/// <returns>缓存中的数据</returns>
public static T Get<T>(string cacheKey, TimeSpan slidingExpiration, Func<T> getData)
{
var data = GetCache(cacheKey);
if (data == null)
{
data = getData();
SetCache(cacheKey, data, slidingExpiration);
}
return (T)data;
} /// <summary>
/// 从缓存中获取数据。缓存中不存在的时候,从回调方法getDate中获取,并设置进缓存。
/// </summary>
/// <typeparam name="T">缓存的数据类型</typeparam>
/// <param name="cacheKey">缓存的键</param>
/// <param name="absoluteExpiration">绝对过期时间</param>
/// <param name="getData">回调方法</param>
/// <returns>缓存中的数据</returns>
public static T Get<T>(string cacheKey, DateTime absoluteExpiration, Func<T> getData)
{
var data = GetCache(cacheKey);
if (data == null)
{
data = getData();
SetCache(cacheKey, data, absoluteExpiration);
}
return (T)data;
} /// <summary>
/// 从缓存中获取数据。缓存中不存在的时候,从回调方法getDate中获取,并设置进缓存。
/// </summary>
/// <typeparam name="T">缓存的数据类型</typeparam>
/// <param name="cacheKey">缓存的键</param>
/// <param name="dependency">文件依赖</param>
/// <param name="getData">回调方法</param>
/// <returns>缓存中的数据</returns>
public static T Get<T>(string cacheKey, CacheDependency dependency, Func<T> getData)
{
var data = GetCache(cacheKey);
if (data == null)
{
data = getData();
SetCache(cacheKey, data, dependency);
}
return (T)data;
} /// <summary>
/// 从缓存中获取数据。缓存中不存在的时候,从回调方法getDate中获取,并设置进缓存。
/// </summary>
/// <typeparam name="T">缓存的数据类型</typeparam>
/// <param name="cacheKey">缓存的键</param>
/// <param name="filename">依赖的文件路径</param>
/// <param name="getData">回调方法</param>
/// <returns>缓存中的数据</returns>
public static T Get<T>(string cacheKey, string filename, Func<T> getData)
{
return Get<T>(cacheKey, new CacheDependency(filename), getData);
} /// <summary>
/// 从缓存中获取数据。缓存中不存在的时候,从回调方法getDate中获取,并设置进缓存。
/// </summary>
/// <typeparam name="T">缓存的数据类型</typeparam>
/// <param name="cacheKey">缓存的键</param>
/// <param name="filenames">依赖的文件路径</param>
/// <param name="getData">回调方法</param>
/// <returns>缓存中的数据</returns>
public static T Get<T>(string cacheKey, string[] filenames, Func<T> getData)
{
return Get<T>(cacheKey, new CacheDependency(filenames), getData);
}
}
}

好用的Cache辅助工具类的更多相关文章

  1. 类型转换辅助工具类TypeCaseHelper

    package org.sakaiproject.util; import java.math.BigDecimal; import java.sql.Date; import java.sql.Ti ...

  2. java在文本处理中的相关辅助工具类

    1,java分词 package com.bobo.util; import ICTCLAS.I3S.AC.ICTCLAS50; public class Cutwords { public stat ...

  3. JUC——线程同步辅助工具类(Semaphore,CountDownLatch,CyclicBarrier)

    锁的机制从整体的运行转态来讲核心就是:阻塞,解除阻塞,但是如果仅仅是这点功能,那么JUC并不能称为一个优秀的线程开发框架,然而是因为在juc里面提供了大量方便的同步工具辅助类. Semaphore信号 ...

  4. JUC——线程同步辅助工具类(Exchanger,CompletableFuture)

    Exchanger交换空间 如果现在有两个线程,一个线程负责生产数据,另外一个线程负责消费数据,那么这个两个线程之间一定会存在一个公共的区域,那么这个区域的实现在JUC包之中称为Exchanger. ...

  5. 制作ado开发辅助工具类SqlHelper

    public static class SqlHelper { //通过配置文件获取连接字符创 private static readonly string constr = Configuratio ...

  6. Web层辅助工具类

    Java web开发中经常用到的一些方法: import java.io.BufferedReader; import java.net.InetAddress; import java.net.Un ...

  7. Redis源代码分析(二十四)--- tool工具类(2)

    在上篇文章中初步的分析了一下,Redis工具类文件里的一些使用方法,包含2个随机算法和循环冗余校验算法,今天,继续学习Redis中的其它的一些辅助工具类的使用方法.包含里面的大小端转换算法,sha算法 ...

  8. [19/03/27-星期三] 容器_Iterator(迭代器)之遍历容器元素(List/Set/Map)&Collections工具类

    一.概念 迭代器为我们提供了统一的遍历容器的方式 /* *迭代器遍历 * */ package cn.sxt.collection; import java.security.KeyStore.Ent ...

  9. JUC 常用4大并发工具类

    什么是JUC? JUC就是java.util.concurrent包,这个包俗称JUC,里面都是解决并发问题的一些东西 该包的位置位于java下面的rt.jar包下面 4大常用并发工具类: Count ...

随机推荐

  1. JDK5-8特性归纳

    jdk5新特性1.自动装箱和拆箱2.枚举3.静态导入4.可变参数5.內省   是Java语言对Bean类属性.事件的一种缺省处理方法.例如类A中有属性那么,那我们可以通过getName,setName ...

  2. SGC强制最低128位加密,公钥支持ECC加密算法的SSL证书

      Pro SSL证书,验证企业域名所有权和企业身份信息,采用SGC(服务器门控)技术强制128位以上至256位加密,属于企业OV验证级专业版(Pro) SSL证书:即使用户使用低版本浏览器(比如浏览 ...

  3. RaspberryPi3安装CentOS7教程

    1.准备 Centos 7 AMR版镜像下载地址: http://mirror.centos.org/altarch/7/isos/armhfp/ 下载得到:CentOS-Userland-7-arm ...

  4. RestEasy 用户指南----第7章 @HeaderParam

    转载说明出处:http://blog.csdn.net/nndtdx/article/details/6870391 原文地址 http://docs.jboss.org/resteasy/docs/ ...

  5. [bzoj3224]普通平衡树[Treap]

    Treap 的各种操作,模板题,要再写几遍 #include <iostream> #include <algorithm> #include <cstdio> # ...

  6. hdu_1859_最小长方形_201402282048

    最小长方形 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submi ...

  7. hdu_1038_Biker's Trip Odometer_201311021643

    Biker's Trip Odometer Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Ot ...

  8. 查看编译器的默认include 路径

    echo | gcc -v -x c++ -E - echo | g++ -v -x c++ -E - `gcc -print-prog-name=cc1plus` -v `g++ -print-pr ...

  9. 2.6-NAT

    2.6-NAT     网络地址转换协议NAT(Network Address Translation):     交换和远程都要用,先上什么就放在哪一块讲,具体来说NAT还是属于远程的.       ...

  10. LeetCode——Permutation Sequence

    The set [1,2,3,-,n] contains a total of n! unique permutations. By listing and labeling all of the p ...