话不多说,直接上代码

 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. WinForm窗体中窗口控件的生成

    1:button控件的生成方式 Button button = new Button(); button.Size = new Size(80, 80); button.Location = new ...

  2. iic通讯 FPGA实现 mpu6050为例

    IIC最常用的通讯协议,但普遍用于单片机.arm这些,用FPGA实现大材小用,但对于菜鸡水平练练手很不错,考验串并转换和时序的控制.今天我就以mpu6050陀螺仪为例,实现FPGA的iic通信. 1. ...

  3. (C/C++学习)5.C++中的虚继承-虚函数-多态解析

    说明:在C++学习的过程中,虚继承-虚函数经常是初学者容易产生误解的两个概念,它们与C++中多态形成的关系,也是很多初学者经常产生困惑的地方,这篇文章将依次分别对三者进行解析,并讲述其之间的联系与不同 ...

  4. LA 4329 Ping pong

    #include <iostream> #include <cstring> #include <cstdio> using namespace std; ; ; ...

  5. redis学习五,redis集群搭建及添加主从节点

    redis集群 java架构师项目实战,高并发集群分布式,大数据高可用,视频教程 在redis3.0之前,出现了sentinel工具来监控各个Master的状态(可以看上一篇博客).如果Master异 ...

  6. Openfire:解决乱码问题

    当部署openfire后,创建用户和发送离线消息时会出现中文字符乱码的问题.要解决这个问题需要同时配置openfire和mysql两端. 首先openfire端,在安装页面中指定odbc连接串中需要带 ...

  7. Git 主要的工作流程

    Git使用个进制字符的SHA- Hash来唯一标识对象 如:e98757d0598ab6eeaf1df0d87dd00826048bd80b git 有种对象 1.blob 表示文本文件,二进制文件或 ...

  8. 【Android开发VR实战】三.开发一个寻宝类VR游戏TreasureHunt

    转载请注明出处:http://blog.csdn.net/linglongxin24/article/details/53939303 本文出自[DylanAndroid的博客] [Android开发 ...

  9. JSP 获取Request 经常使用參数

    <input type="hidden" id="a" value="<%=request.getScheme()%>" ...

  10. 2014百度之星第二题Disk Schedule(双调欧几里得旅行商问题+DP)

    Disk Schedule Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) To ...