好用的Cache辅助工具类
话不多说,直接上代码
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辅助工具类的更多相关文章
- 类型转换辅助工具类TypeCaseHelper
package org.sakaiproject.util; import java.math.BigDecimal; import java.sql.Date; import java.sql.Ti ...
- java在文本处理中的相关辅助工具类
1,java分词 package com.bobo.util; import ICTCLAS.I3S.AC.ICTCLAS50; public class Cutwords { public stat ...
- JUC——线程同步辅助工具类(Semaphore,CountDownLatch,CyclicBarrier)
锁的机制从整体的运行转态来讲核心就是:阻塞,解除阻塞,但是如果仅仅是这点功能,那么JUC并不能称为一个优秀的线程开发框架,然而是因为在juc里面提供了大量方便的同步工具辅助类. Semaphore信号 ...
- JUC——线程同步辅助工具类(Exchanger,CompletableFuture)
Exchanger交换空间 如果现在有两个线程,一个线程负责生产数据,另外一个线程负责消费数据,那么这个两个线程之间一定会存在一个公共的区域,那么这个区域的实现在JUC包之中称为Exchanger. ...
- 制作ado开发辅助工具类SqlHelper
public static class SqlHelper { //通过配置文件获取连接字符创 private static readonly string constr = Configuratio ...
- Web层辅助工具类
Java web开发中经常用到的一些方法: import java.io.BufferedReader; import java.net.InetAddress; import java.net.Un ...
- Redis源代码分析(二十四)--- tool工具类(2)
在上篇文章中初步的分析了一下,Redis工具类文件里的一些使用方法,包含2个随机算法和循环冗余校验算法,今天,继续学习Redis中的其它的一些辅助工具类的使用方法.包含里面的大小端转换算法,sha算法 ...
- [19/03/27-星期三] 容器_Iterator(迭代器)之遍历容器元素(List/Set/Map)&Collections工具类
一.概念 迭代器为我们提供了统一的遍历容器的方式 /* *迭代器遍历 * */ package cn.sxt.collection; import java.security.KeyStore.Ent ...
- JUC 常用4大并发工具类
什么是JUC? JUC就是java.util.concurrent包,这个包俗称JUC,里面都是解决并发问题的一些东西 该包的位置位于java下面的rt.jar包下面 4大常用并发工具类: Count ...
随机推荐
- PAT_A1003#Emergency
Source: PAT A1003 Emergency (25 分) Description: As an emergency rescue team leader of a city, you ar ...
- Linux之iptables(三、命令--->单主机)
iptables命令规则格式: iptables [-t table] SUBCOMMAND chain [-m matchname[per-match-options]] -j targetname ...
- 实验十二 团队作业8:软件测试与Alpha冲刺 第三天
项目 内容 这个作业属于哪个课程 老师链接 这个作业的要求在哪里 作业链接地址 团队名称 always run 作业学习目标 (1)掌握软件测试基础技术.(2)学习迭代式增量软件开发过程(Scrum) ...
- 【codeforces 527A】Playing with Paper
[题目链接]:http://codeforces.com/contest/527/problem/A [题意] 让你每次从一个长方形里面截出一个边长为长方形的较短边的正方形; 然后留下的部分重复上述步 ...
- CodeForces - 284C - Cows and Sequence
先上题目: C. Cows and Sequence time limit per test 3 seconds memory limit per test 256 megabytes input s ...
- Configuration must specify a spooling directory
启动spooling源时报错: 原因:spooling配置文件有误 a1.sources.r1.type = spooldir a1.sources.r1.spooldir = /usr/local/ ...
- nyoj_38_布线问题_201403121753
布线问题 时间限制:1000 ms | 内存限制:65535 KB 难度:4 描述 南阳理工学院要进行用电线路改造,现在校长要求设计师设计出一种布线方式,该布线方式需要满足以下条件:1.把所有 ...
- 【ACM】nyoj_14_会场安排问题_201308151955
会场安排问题时间限制:3000 ms | 内存限制:65535 KB 难度:4描述 学校的小礼堂每天都会有许多活动,有时间这些活动的计划时间会发生冲突,需要选择出一些活动进行举办.小刘的工作就是安 ...
- Skia图片解码模块流程分析
我在在PPAPI插件中使用Skia画图中说能够在PPAPI插件内使用Skia来画图.这里面会有一个与色彩空间(像素格式)相关的问题.在那篇文章里我们在PPAPI中使用PPB_ImageData创建2D ...
- 关于重置IOS App请求推送的授权请求
项目要加入推送通知.測试完本地通知后.发现測不了远程通知.于是想重置授权请求. 下面是重置授权请求的方法: 方法一: 通用->还原->抹掉全部内容和设置 可是第一种方法非常费时,抹掉内容预 ...