C#.NET Velocity共享缓存管理
using System;
using System.Collections;
using System.Text.RegularExpressions;
using System.Web;
using Microsoft.Data.Caching;
using System.Collections.Generic;
using System.Linq;
namespace Pub.Class
{
/// <summary>
/// Velocity缓存管理
/// </summary>
public class VelocityCache {
#region 私有静态字段
private static readonly DataCache _cache;
private static readonly DataCacheFactory _factory;
private static string RegionName = "PubClassRegion";
/// <summary>
/// 缓存因子
/// </summary>
private static int Factor = 5;
#endregion
#region 构造器
static VelocityCache() {
Cache2.VelocityCacheName = string.IsNullOrEmpty(Cache2.VelocityCacheName) ? WebConfig.GetApp("VelocityCacheName") ?? string.Empty : Cache2.VelocityCacheName;
if (Cache2.VelocityCacheName.Length>0) {
_factory = new DataCacheFactory();
_cache = _factory.GetCache(Cache2.VelocityCacheName);
try { _cache.CreateRegion(RegionName, false); } catch {}
}
}
#endregion
#region 公有靜态字段
/// <summary>
/// 日缓存因子
/// </summary>
public static readonly int DayFactor = 17280;
/// <summary>
/// 小时缓存因子
/// </summary>
public static readonly int HourFactor = 720;
/// <summary>
/// 分缓存因子
/// </summary>
public static readonly int MinuteFactor = 12;
/// <summary>
/// 秒缓存因子
/// </summary>
public static readonly double SecondFactor = 0.2;
#endregion
#region 静态方法
public static IList<CachedItem> GetList(){
IList<CachedItem> list = new List<CachedItem>();
IList<KeyValuePair<string, object>> result = _cache.GetObjectsInRegion(RegionName).ToList();
foreach(var info in result) {
list.Add(new CachedItem(info.Key, info.Value.GetType().ToString()));
}
return list;
}
/// <summary>
/// 清空所有缓存项目
/// </summary>
public static void Clear() {
_cache.ClearRegion(RegionName);
}
/// <summary>
/// 删除缓存
/// </summary>
/// <param name="pattern">缓存键正则匹配模式</param>
public static void RemoveByPattern(string pattern) {
IList<KeyValuePair<string, object>> result = _cache.GetObjectsInRegion(RegionName).ToList();
Regex regex = new Regex(pattern, RegexOptions.IgnoreCase | RegexOptions.Singleline | RegexOptions.Compiled);
foreach(var info in result) {
if (regex.IsMatch(info.Key)) _cache.Remove(info.Key, RegionName);
}
}
/// <summary>
/// 删除缓存
/// </summary>
/// <param name="key">缓存键名</param>
public static void Remove(string key) { if (Get(key) != null) _cache.Remove(key, RegionName); }
/// <summary>
/// 增加缓存项目
/// </summary>
/// <param name="key">缓存键名</param>
/// <param name="obj">缓存对象</param>
public static void Insert(string key, object obj) { _cache.Put(key, obj, DateTime.Now.GetTimeSpan(DateTime.Now.AddSeconds(Factor)), RegionName); }
public static void Add(string key, object obj) { _cache.Add(key, obj, DateTime.Now.GetTimeSpan(DateTime.Now.AddSeconds(Factor)), RegionName); }
/// <summary>
/// 增加缓存项目
/// </summary>
/// <param name="key">缓存键名</param>
/// <param name="obj">缓存对象</param>
/// <param name="seconds">缓存秒数</param>
public static void Insert(string key, object obj, int seconds) {
if (obj != null) _cache.Put(key, obj, DateTime.Now.GetTimeSpan(DateTime.Now.AddSeconds(Factor * seconds)), RegionName);
}
public static void Add(string key, object obj, int seconds) {
if (obj != null) _cache.Add(key, obj, DateTime.Now.GetTimeSpan(DateTime.Now.AddSeconds(Factor * seconds)), RegionName);
}
/// <summary>
/// 获取缓存对象
/// </summary>
/// <param name="key">缓存键名</param>
/// <returns>返回缓存对象</returns>
public static object Get(string key) { return _cache.Get(key, RegionName); }
#endregion
}
}
开源:
https://github.com/hcxiong 欢迎收藏:)