C# 实现 CacheHelper:
using System;
using System.Web;
using System.Web.Caching;
public class CacheHelper
{
/// <summary>
/// 创建缓存
/// </summary>
/// <param name="key">缓存Key</param>
/// <param name="obj">object对象</param>
public static void Insert(string key, object obj)
{
//创建缓存
HttpContext.Current.Cache.Insert(key, obj);
}
/// <summary>
/// 移除缓存
/// </summary>
/// <param name="key">缓存Key</param>
public static void Remove(string key)
{
//创建缓存
HttpContext.Current.Cache.Remove(key);
}
/// <summary>
/// 创建缓存项的文件依赖
/// </summary>
/// <param name="key">缓存Key</param>
/// <param name="obj">object对象</param>
/// <param name="fileName">文件绝对路径</param>
public static void Insert(string key, object obj, string fileName)
{
//创建缓存依赖项
CacheDependency dep = new CacheDependency(fileName);
//创建缓存
HttpContext.Current.Cache.Insert(key, obj, dep);
}
/// <summary>
/// 创建缓存项过期
/// </summary>
/// <param name="key">缓存Key</param>
/// <param name="obj">object对象</param>
/// <param name="expires">过期时间(分钟)</param>
public static void Insert(string key, object obj, int expires)
{
HttpContext.Current.Cache.Insert(key, obj, null, Cache.NoAbsoluteExpiration, new TimeSpan(0, expires, 0));
}
/// <summary>
/// 获取缓存对象
/// </summary>
/// <param name="key">缓存Key</param>
/// <returns>object对象</returns>
public static object Get(string key)
{
if (string.IsNullOrEmpty(key))
{
return null;
}
return HttpContext.Current.Cache.Get(key);
}
/// <summary>
/// 获取缓存对象
/// </summary>
/// <typeparam name="T">T对象</typeparam>
/// <param name="key">缓存Key</param>
/// <returns></returns>
public static T Get<T>(string key)
{
object obj = Get(key);
return obj == null ? default(T) : (T)obj;
}
/// <summary>
/// 获取数据缓存
/// </summary>
/// <param name="cacheKey">键</param>
public static object GetCache(string cacheKey)
{
var objCache = HttpRuntime.Cache.Get(cacheKey);
return objCache;
}
/// <summary>
/// 设置数据缓存
/// </summary>
public static void SetCache(string cacheKey, object objObject)
{
var objCache = HttpRuntime.Cache;
objCache.Insert(cacheKey, objObject);
}
/// <summary>
/// 设置数据缓存
/// </summary>
public static void SetCache(string cacheKey, object objObject, int timeout = 7200)
{
try
{
if (objObject == null) return;
var objCache = HttpRuntime.Cache;
//相对过期
//objCache.Insert(cacheKey, objObject, null, DateTime.MaxValue, timeout, CacheItemPriority.NotRemovable, null);
//绝对过期时间
objCache.Insert(cacheKey, objObject, null, DateTime.Now.AddSeconds(timeout), TimeSpan.Zero, CacheItemPriority.High, null);
}
catch (Exception)
{
//throw;
}
}
/// <summary>
/// 移除指定数据缓存
/// </summary>
public static void RemoveAllCache(string cacheKey)
{
var cache = HttpRuntime.Cache;
cache.Remove(cacheKey);
}
/// <summary>
/// 移除全部缓存
/// </summary>
public static void RemoveAllCache()
{
var cache = HttpRuntime.Cache;
var cacheEnum = cache.GetEnumerator();
while (cacheEnum.MoveNext())
{
cache.Remove(cacheEnum.Key.ToString());
}
}
}
相关阅读 >>
C#开发实例-订制屏幕截图工具(十)在截图中包含鼠标指针形状
详细介绍C#批量生成随机密码必须包含数字和字母并用加密算法加密的代码案例
C#中static void main(string[] args) 参数的示例代码详解
更多相关阅读请进入《C#》频道 >>
C#高级编程(第11版) C# 7 & .NET Core 2.0(.NET开发经典名著)
作者:[美]克里斯琴·内格尔(Christian Nagel)著。出版时间:2019年3月。