好用的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 ...
随机推荐
- Lua之尾调函数的用法
Lua之尾调函数的用法 --当函数的最后返回结果调用另一个函数,称之为尾调函数 function f(x) return g(x) end --由于“尾调用”不会耗费栈空间,所以一个程序可以拥有无数嵌 ...
- Neo4j图数据库从入门到精通
目录 第一章:介绍 Neo4j是什么 Neo4j的特点 Neo4j的优点 第二章:安装 1.环境 2.下载 3.开启远程访问 4.启动 第三章:CQL 1.CQL简介 2.Neo4j CQL命令/条款 ...
- sql 语句实现可用户名、邮箱、手机号登录系统
select top 1 nid from Users where (userName collate Chinese_PRC_CS_AS=@userName or mobile collate Ch ...
- 【[Offer收割]编程练习赛13 C】 一人麻将
[题目链接]:http://hihocoder.com/problemset/problem/1503 [题意] [题解] 一直在纠结如果没胡的话要扔掉哪一个麻将; 但其实可不用扔的,全部存起来就好了 ...
- 混合了RBAC和ACL的权限系统(二) -- 基于RBAC的系统权限
http://fightplane.iteye.com/blog/1278464 1. 概念说明 A 系统级权限:从角色的角度出发,不特定于任何实际的资源的权限.比如“用户是否可以修改标题”这个权限, ...
- noip模拟赛 排序
分析:因为序列是不严格单调的,所以挪动一个数其实就相当于把这个数给删了.如果a[i] < a[i-1],那么可以删掉a[i],也可以删掉a[i-1](!如果没考虑到这一点就只有90分),删后判断 ...
- P - How many
Give you n ( n < 10000) necklaces ,the length of necklace will not large than 100,tell me How man ...
- RDS for MySQL查询缓存 (Query Cache) 的设置和使用
https://help.aliyun.com/knowledge_detail/41717.html?spm=5176.7841698.2.11.aCvOXJ RDS for MySQL查询缓存 ( ...
- spring mvc之applicationContext
1.ApplicationContext是在package org.springframework.context下,是spring的,spring context包下的. applicationCo ...
- Maven 的dependency 的 classifier的作用
直接看一个例子,maven中要引入json包,于是使用了 <dependency> <groupId>net.sf.json-lib</groupId> <a ...