C# 缓存操作类
2024-09-28 12:32:09
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Caching; namespace Utility
{
/// <summary>
/// 缓存操作,默认缓存1分钟
/// </summary>
public static class CacheHelper
{
static int cacheTime = 1; /// <summary>
/// 读取缓存项
/// </summary>
/// <returns></returns>
public static object CacheReader(string cacheKey)
{
return HttpRuntime.Cache[cacheKey];
} /// <summary>
/// 写入缓存项
/// </summary>
public static void CacheWriter(string cacheKey, object cacheValue, int cache_time = 0)
{
HttpRuntime.Cache.Insert(cacheKey, cacheValue, null,
DateTime.Now.AddMinutes(cache_time <= 0 ? cacheTime : cache_time),
Cache.NoSlidingExpiration);
} /// <summary>
/// 移除指定缓存项
/// </summary>
public static void CacheRemove(string cacheName)
{
HttpRuntime.Cache.Remove(cacheName);
} /// <summary>
/// 缓存对象泛型实现
/// </summary>
public static T ObjectReader<T>(string cacheKey = null)
where T : class
{
string cachekey = typeof(T).GetHashCode() + StringHelper.ToString(cacheKey);
var obj = CacheReader(cachekey) as T;
return obj;
} /// <summary>
/// 缓存对象泛型实现
/// </summary>
public static void ObjectWriter<T>(T cacheValue, string cacheKey = null, int cache_time = 0)
where T : class
{
string cachekey = typeof (T).GetHashCode() + StringHelper.ToString(cacheKey);
CacheWriter(cachekey, cacheValue, cache_time);
}
}
}
最新文章
- Delphi Code Editor 之 编辑器选项
- jQuery $(document).ready()和JavaScript onload事件
- MongoDB 索引相关知识
- 如何解决cellIndex在IE下兼容性问题
- 漫谈刑事辩护 z
- .Net 4.0 Convert Object to XDocument
- python基础之 optparse.OptionParser
- cdn与http缓存
- PHP查询MYSQL表的主键
- 在WebBrowser控件使用js调用C#方法
- Django学习(4)表单,让数据库更美好
- 半小时入门Thrift
- linux普通帐号可以临时切换到root(添加用户到sudoers中)
- bootstrap datetimepicker
- jq 获取当前屏幕高度
- CCF认证201712-1最小差值
- java两年工作经验有什么经验
- MFC程序开始的执行过程详述
- 深入浅出Docker(三):Docker开源之路
- 在 LINQ to Entities 查询中无法构造实体或复杂类型&ldquo;Mvc_MusicShop_diy.Models.Order&rdquo;
热门文章
- 创建一个简单的WCF程序2——手动开启/关闭WCF服务与动态调用WCF地址
- 区块链区块的生成和链接,比特币btc的产生,UTXO的生成和消耗,比特币系统
- IPERF 网络性能测试
- web前端学习:JavaScript学习指南
- GoldenGate实时投递数据到大数据平台(3)- Apache Flume
- Kali linux apt-get update 失败,无release……(最有效)
- JOBDU 题目1131:合唱队形
- ELK学习笔记之F5-HTTP-requesting-logging logstash filter
- select2 AJAX获取数据
- ora-24550 signo=6 signo=11解决