缓存工具类CacheHelper
代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web; namespace Common.Utils
{
/// <summary>
/// 缓存
/// </summary>
public static class CacheHelper
{
/// <summary>
/// 获取缓存
/// </summary>
public static object Get(string cacheKey)
{
return HttpRuntime.Cache[cacheKey];
} /// <summary>
/// 设置缓存
/// </summary>
public static void Set(string cacheKey, object value)
{
HttpRuntime.Cache.Insert(cacheKey, value);
} /// <summary>
/// 添加缓存
/// </summary>
public static void Set<T>(string cacheKey, object value)
{
Type type = typeof(T);
string tableName = type.Name;
if (HttpRuntime.Cache[tableName] == null)
{
Dictionary<string, object> dic = new Dictionary<string, object>();
dic.Add(cacheKey, value);
HttpRuntime.Cache.Insert(tableName, dic);
}
else
{
Dictionary<string, object> dic = (Dictionary<string, object>)HttpRuntime.Cache[tableName];
if (dic.Keys.Contains<string>(cacheKey))
{
dic[cacheKey] = value;
}
else
{
dic.Add(cacheKey, value);
}
HttpRuntime.Cache[tableName] = dic;
}
} /// <summary>
/// 获取缓存
/// </summary>
public static object Get<T>(string cacheKey)
{
Type type = typeof(T);
string tableName = type.Name;
if (HttpRuntime.Cache[tableName] != null)
{
Dictionary<string, object> dic = (Dictionary<string, object>)HttpRuntime.Cache[tableName];
if (dic.Keys.Contains<string>(cacheKey))
{
return dic[cacheKey];
}
}
return null;
} /// <summary>
/// 删除缓存
/// </summary>
public static void Remove<T>()
{
HttpRuntime.Cache.Remove(typeof(T).Name);
}
}
}
缓存工具类CacheHelper的更多相关文章
- Cache【硬盘缓存工具类(包含内存缓存LruCache和磁盘缓存DiskLruCache)】
版权声明:本文为HaiyuKing原创文章,转载请注明出处! 前言 内存缓存LruCache和磁盘缓存DiskLruCache的封装类,主要用于图片缓存. 效果图 代码分析 内存缓存LruCache和 ...
- 分享基于MemoryCache(内存缓存)的缓存工具类,C# B/S 、C/S项目均可以使用!
using System; using System.Collections.Generic; using System.Linq; using System.Runtime.Caching; usi ...
- php 缓存工具类 实现网页缓存
php 缓存工具类 实现网页缓存 php程序在抵抗大流量访问的时候动态网站往往都是难以招架,所以要引入缓存机制,一般情况下有两种类型缓存 一.文件缓存 二.数据查询结果缓存,使用内存来实现高速缓存 本 ...
- Go/Python/Erlang编程语言对比分析及示例 基于RabbitMQ.Client组件实现RabbitMQ可复用的 ConnectionPool(连接池) 封装一个基于NLog+NLog.Mongo的日志记录工具类LogUtil 分享基于MemoryCache(内存缓存)的缓存工具类,C# B/S 、C/S项目均可以使用!
Go/Python/Erlang编程语言对比分析及示例 本文主要是介绍Go,从语言对比分析的角度切入.之所以选择与Python.Erlang对比,是因为做为高级语言,它们语言特性上有较大的相似性, ...
- redis缓存工具类
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.redis ...
- redis缓存工具类,提供序列化接口
1.序列化工具类 package com.qicheshetuan.backend.util; import java.io.ByteArrayInputStream; import java.io. ...
- CookieUtils-浏览器缓存工具类
package cn.yonyong.myproject.commons.utils; import javax.servlet.http.Cookie; import javax.servlet.h ...
- 基于spring的redisTemplate的缓存工具类
pom.xml文件添加 <!-- config redis data and client jar --><dependency> <groupId>org.spr ...
- 可以随时拿取spring容器中Bean的工具类
前言 在Spring帮我们管理bean后,编写一些工具类的时候需要从容器中拿到一些对象来做一些操作,比如字典缓存工具类,在没有找到字典缓存时,需要dao对象从数据库load一次,再次存入缓存中.此时需 ...
随机推荐
- 网页提交中文到WEB容器的经历了些什么过程....
先准备一个网页 <html><meta http-equiv="Content-Type" content="text/html; charset=gb ...
- 数据库的快照隔离级别(Snapshot Isolation)
隔离级别定义事务处理数据读取操作的隔离程度,在SQL Server中,隔离级别只会影响读操作申请的共享锁(Shared Lock),而不会影响写操作申请的互斥锁(Exclusive Lock),隔离级 ...
- System.FormatException: GUID 应包含带 4 个短划线的 32 位数(xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx)。
在NHibernate数据库查询中出现了这个错误,由于是数据库是mysql的,当定义的字段为char(36)的时候就会出现这个错误. [解决方法] 将char(36) 改成varchar(40)就行了 ...
- [C#] 简单的 Helper 封装 -- SQLiteHelper
using System; using System.Data; using System.Data.SQLite; namespace SqliteConsoleApp { /// <summ ...
- Linux下的C Socket编程 -- server端的继续研究
Linux下的C Socket编程(四) 延长server的生命周期 在前面的一个个例子中,server在处理完一个连接后便会立即结束掉自己,然而这种server并不科学啊,server应该是能够一直 ...
- JS中关于字符串的几个常用又容易忘记的方法
1>.字符串连接:concat(): 左边字符串. concat(连接的字符串1,字符串2,....); 获取指定位置的字符:charAt(): 返回指定位置的字符: 字符串.charAt(i ...
- CentOS 6.5安装Node.js, npm
CentOS上可以通过下载*.tar.gz安装包的方式自己解压缩.编译的方式安装,同时还可以采用EPEL的方式安装: Node.js and npm are available from the Fe ...
- Xamarin.Android之SQLiteOpenHelper
一.前言 在手机中进行网络连接不仅是耗时也是耗电的,而耗电却是致命的.所以我们就需要数据库帮助我们存储离线数据,以便在用户未使用网络的情况下也可以能够使用应用的部分功能,而在需要网络连接的功能上采用提 ...
- 用lucene替代mysql读库的尝试
采用lucene对mysql中的表建索引,并替代全文检索操作. 备注:代码临时梳理很粗糙,后续修改. import java.io.File; import java.io.IOException; ...
- .NET基础拾遗(2)面向对象的实现和异常的处理基础
Index : (1)类型语法.内存管理和垃圾回收基础 (2)面向对象的实现和异常的处理基础 (3)字符串.集合与流 (4)委托.事件.反射与特性 (5)多线程开发基础 (6)ADO.NET与数据库开 ...