好用的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 ...
随机推荐
- demo__image_loader
环境 webpack4.x 文件结构 │ package.json │ webpack.config.js │ yarn.lock │ ├─dist │ 1f871aa58.png │ bundle. ...
- php第十八节课
PDO 对不同的数据库连接使用 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "ht ...
- Huawei-R&S-网络工程师实验笔记20190607-STP生成树协议(基本配置、桥优先级、根桥选举、根端口、路径开销、边缘端口)
>Huawei-R&S-网络工程师实验笔记20190607-STP生成树协议(基本配置.桥优先级.根桥选举.根端口.路径开销.边缘端口) >>实验开始,先上拓扑图参考: &l ...
- JavaSE 学习笔记之Import 导入(十二)
Import - 导入:类名称变长,写起来很麻烦.为了简化,使用了一个关键字:import,可以使用这个关键字导入指定包中的类.记住:实际开发时,到的哪个类就导入哪个类,不建议使用*. import ...
- input的disabled和readonly区别
<input name=”country” id=”country” size=12 value=”disabled提交时得不到该值" disabled=”disabled” > ...
- 通过urllib2抓取网页内容(1)
一.urllib2发送请求 import urllib2 url = 'http://www.baidu.com' req = urllib2.Request(url) response = urll ...
- Maven中的dependency的scope作用域详解
1.test范围指的是测试范围有效,在编译和打包时都不会使用这个依赖 2.compile范围指的是编译范围有效,在编译和打包时都会将依赖存储进去 3.provided依赖:在编译和测试的过程有效,最后 ...
- Linq查询datatable的记录集合
通过linq查询datatable数据集合满足条件的数据集 1.首先定义查询字段的变量,比方深度 string strDepth=查询深度的值: var dataRows = from datarow ...
- swift 雨燕 新手教程
Apple Swift编程语言新手教程 chox 2014-06-03 文件夹 简单介绍 入门 简单值 控制流 函数与闭包 对象与类 枚举与结构 1 简单介绍 今天凌晨Apple刚刚公布了Swif ...
- [Python]通过websocket与jsclient通信
站点大多使用HTTP协议通信.而HTTP是无连接的协议.仅仅有client请求时,server端才干发出对应的应答.HTTP请求的包也比較大,假设仅仅是非常小的数据通信.开销过大.于是,我们能够使用w ...