添加项目文件。
This commit is contained in:
468
HT.Cloud.Code/Cache/CacheHelper.cs
Normal file
468
HT.Cloud.Code/Cache/CacheHelper.cs
Normal file
@ -0,0 +1,468 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace HT.Cloud.Code
|
||||
{
|
||||
public abstract class BaseHelper : RedisHelper<BaseHelper>
|
||||
{ }
|
||||
|
||||
public abstract class HandleLogHelper : RedisHelper<HandleLogHelper>
|
||||
{ }
|
||||
|
||||
public class CacheHelper
|
||||
{
|
||||
private static string cacheProvider = GlobalContext.SystemConfig.CacheProvider;
|
||||
|
||||
/// <summary>
|
||||
/// 添加缓存
|
||||
/// </summary>
|
||||
/// <param name="key">缓存Key</param>
|
||||
/// <param name="value">缓存Value</param>
|
||||
/// <param name="expiresIn">缓存时长h</param>
|
||||
/// <param name="isSliding">是否滑动过期(如果在过期时间内有操作,则以当前时间点延长过期时间)</param>
|
||||
/// <returns></returns>
|
||||
public static async Task<bool> SetAsync(string key, object value, int expiresIn = -1, bool isSliding = true)
|
||||
{
|
||||
return await SetBySecondAsync(key, value, expiresIn * 3600, isSliding);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 添加缓存
|
||||
/// </summary>
|
||||
/// <param name="key">缓存Key</param>
|
||||
/// <param name="value">缓存Value</param>
|
||||
/// <param name="expiresIn">缓存时长秒</param>
|
||||
/// <param name="isSliding">是否滑动过期(如果在过期时间内有操作,则以当前时间点延长过期时间)</param>
|
||||
/// <returns></returns>
|
||||
public static async Task<bool> SetBySecondAsync(string key, object value, int expiresIn = -1, bool isSliding = true)
|
||||
{
|
||||
if (string.IsNullOrEmpty(key))
|
||||
throw new ArgumentNullException(nameof(key));
|
||||
if (value == null)
|
||||
throw new ArgumentNullException(nameof(value));
|
||||
switch (cacheProvider)
|
||||
{
|
||||
case Define.CACHEPROVIDER_REDIS:
|
||||
if (expiresIn > 0)
|
||||
{
|
||||
await BaseHelper.SetAsync(key, value, expiresIn);
|
||||
}
|
||||
else
|
||||
{
|
||||
await BaseHelper.SetAsync(key, value);
|
||||
}
|
||||
return await ExistsAsync(key);
|
||||
|
||||
case Define.CACHEPROVIDER_MEMORY:
|
||||
if (expiresIn > 0)
|
||||
{
|
||||
MemoryCacheHelper.Set(key, value, TimeSpan.FromSeconds(expiresIn), isSliding);
|
||||
}
|
||||
else
|
||||
{
|
||||
MemoryCacheHelper.Set(key, value);
|
||||
}
|
||||
return await ExistsAsync(key);
|
||||
|
||||
default:
|
||||
if (expiresIn > 0)
|
||||
{
|
||||
MemoryCacheHelper.Set(key, value, TimeSpan.FromSeconds(expiresIn), isSliding);
|
||||
}
|
||||
else
|
||||
{
|
||||
MemoryCacheHelper.Set(key, value);
|
||||
}
|
||||
return await ExistsAsync(key);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 添加缓存
|
||||
/// </summary>
|
||||
/// <param name="key">缓存Key</param>
|
||||
/// <param name="value">缓存Value</param>
|
||||
/// <param name="expiresIn">缓存时长秒</param>
|
||||
/// <param name="isSliding">是否滑动过期(如果在过期时间内有操作,则以当前时间点延长过期时间)</param>
|
||||
/// <returns></returns>
|
||||
public static bool SetBySecond(string key, object value, int expiresIn = -1, bool isSliding = true)
|
||||
{
|
||||
if (string.IsNullOrEmpty(key))
|
||||
throw new ArgumentNullException(nameof(key));
|
||||
if (value == null)
|
||||
throw new ArgumentNullException(nameof(value));
|
||||
switch (cacheProvider)
|
||||
{
|
||||
case Define.CACHEPROVIDER_REDIS:
|
||||
if (expiresIn > 0)
|
||||
{
|
||||
BaseHelper.Set(key, value, expiresIn);
|
||||
}
|
||||
else
|
||||
{
|
||||
BaseHelper.Set(key, value);
|
||||
}
|
||||
return Exists(key);
|
||||
|
||||
case Define.CACHEPROVIDER_MEMORY:
|
||||
if (expiresIn > 0)
|
||||
{
|
||||
MemoryCacheHelper.Set(key, value, TimeSpan.FromSeconds(expiresIn), isSliding);
|
||||
}
|
||||
else
|
||||
{
|
||||
MemoryCacheHelper.Set(key, value);
|
||||
}
|
||||
return Exists(key);
|
||||
|
||||
default:
|
||||
if (expiresIn > 0)
|
||||
{
|
||||
MemoryCacheHelper.Set(key, value, TimeSpan.FromSeconds(expiresIn), isSliding);
|
||||
}
|
||||
else
|
||||
{
|
||||
MemoryCacheHelper.Set(key, value);
|
||||
}
|
||||
return Exists(key);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取缓存
|
||||
/// </summary>
|
||||
/// <param name="key">缓存Key</param>
|
||||
/// <returns></returns>
|
||||
public static async Task<T> GetAsync<T>(string key)
|
||||
{
|
||||
if (string.IsNullOrEmpty(key))
|
||||
throw new ArgumentNullException(nameof(key));
|
||||
switch (cacheProvider)
|
||||
{
|
||||
case Define.CACHEPROVIDER_REDIS:
|
||||
return await BaseHelper.GetAsync<T>(key);
|
||||
|
||||
case Define.CACHEPROVIDER_MEMORY:
|
||||
return MemoryCacheHelper.Get<T>(key);
|
||||
|
||||
default:
|
||||
return MemoryCacheHelper.Get<T>(key);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取缓存
|
||||
/// </summary>
|
||||
/// <param name="key">缓存Key</param>
|
||||
/// <returns></returns>
|
||||
public static T Get<T>(string key)
|
||||
{
|
||||
if (string.IsNullOrEmpty(key))
|
||||
throw new ArgumentNullException(nameof(key));
|
||||
switch (cacheProvider)
|
||||
{
|
||||
case Define.CACHEPROVIDER_REDIS:
|
||||
return BaseHelper.Get<T>(key);
|
||||
|
||||
case Define.CACHEPROVIDER_MEMORY:
|
||||
return MemoryCacheHelper.Get<T>(key);
|
||||
|
||||
default:
|
||||
return MemoryCacheHelper.Get<T>(key);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 删除缓存
|
||||
/// </summary>
|
||||
/// <param name="key">缓存Key</param>
|
||||
/// <returns></returns>
|
||||
public static async Task RemoveAsync(string key)
|
||||
{
|
||||
if (string.IsNullOrEmpty(key))
|
||||
throw new ArgumentNullException(nameof(key));
|
||||
switch (cacheProvider)
|
||||
{
|
||||
case Define.CACHEPROVIDER_REDIS:
|
||||
await BaseHelper.DelAsync(key);
|
||||
break;
|
||||
|
||||
case Define.CACHEPROVIDER_MEMORY:
|
||||
MemoryCacheHelper.Remove(key);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 删除缓存
|
||||
/// </summary>
|
||||
/// <param name="key">缓存Key</param>
|
||||
/// <returns></returns>
|
||||
public static void Remove(string key)
|
||||
{
|
||||
if (string.IsNullOrEmpty(key))
|
||||
throw new ArgumentNullException(nameof(key));
|
||||
switch (cacheProvider)
|
||||
{
|
||||
case Define.CACHEPROVIDER_REDIS:
|
||||
BaseHelper.Del(key);
|
||||
break;
|
||||
|
||||
case Define.CACHEPROVIDER_MEMORY:
|
||||
MemoryCacheHelper.Remove(key);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 验证缓存项是否存在
|
||||
/// </summary>
|
||||
/// <param name="key">缓存Key</param>
|
||||
/// <returns></returns>
|
||||
public static async Task<bool> ExistsAsync(string key)
|
||||
{
|
||||
if (string.IsNullOrEmpty(key))
|
||||
throw new ArgumentNullException(nameof(key));
|
||||
switch (cacheProvider)
|
||||
{
|
||||
case Define.CACHEPROVIDER_REDIS:
|
||||
return await BaseHelper.ExistsAsync(key);
|
||||
|
||||
case Define.CACHEPROVIDER_MEMORY:
|
||||
return MemoryCacheHelper.Exists(key);
|
||||
|
||||
default:
|
||||
return MemoryCacheHelper.Exists(key);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 验证缓存项是否存在
|
||||
/// </summary>
|
||||
/// <param name="key">缓存Key</param>
|
||||
/// <returns></returns>
|
||||
public static bool Exists(string key)
|
||||
{
|
||||
if (string.IsNullOrEmpty(key))
|
||||
throw new ArgumentNullException(nameof(key));
|
||||
switch (cacheProvider)
|
||||
{
|
||||
case Define.CACHEPROVIDER_REDIS:
|
||||
return BaseHelper.Exists(key);
|
||||
|
||||
case Define.CACHEPROVIDER_MEMORY:
|
||||
return MemoryCacheHelper.Exists(key);
|
||||
|
||||
default:
|
||||
return MemoryCacheHelper.Exists(key);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 缓存续期
|
||||
/// </summary>
|
||||
/// <param name="key">缓存Key</param>
|
||||
/// <param name="hour">时间小时</param>
|
||||
/// <returns></returns>
|
||||
public static async Task ExpireAsync(string key, int hour)
|
||||
{
|
||||
switch (cacheProvider)
|
||||
{
|
||||
case Define.CACHEPROVIDER_REDIS:
|
||||
await BaseHelper.ExpireAtAsync(key, DateTime.Now.AddHours(hour));
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 缓存续期
|
||||
/// </summary>
|
||||
/// <param name="key">缓存Key</param>
|
||||
/// <param name="hour">时间小时</param>
|
||||
/// <returns></returns>
|
||||
public static void Expire(string key, int hour)
|
||||
{
|
||||
switch (cacheProvider)
|
||||
{
|
||||
case Define.CACHEPROVIDER_REDIS:
|
||||
BaseHelper.ExpireAt(key, DateTime.Now.AddHours(hour));
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 清空缓存
|
||||
/// </summary>
|
||||
/// <param name="key">缓存Key</param>
|
||||
/// <returns></returns>
|
||||
public static async Task FlushAllAsync()
|
||||
{
|
||||
switch (cacheProvider)
|
||||
{
|
||||
case Define.CACHEPROVIDER_REDIS:
|
||||
await BaseHelper.NodesServerManager.FlushDbAsync();
|
||||
break;
|
||||
|
||||
case Define.CACHEPROVIDER_MEMORY:
|
||||
MemoryCacheHelper.RemoveCacheAll();
|
||||
break;
|
||||
|
||||
default:
|
||||
MemoryCacheHelper.RemoveCacheAll();
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 清空缓存
|
||||
/// </summary>
|
||||
/// <param name="key">缓存Key</param>
|
||||
/// <returns></returns>
|
||||
public static void FlushAll1()
|
||||
{
|
||||
switch (cacheProvider)
|
||||
{
|
||||
case Define.CACHEPROVIDER_REDIS:
|
||||
BaseHelper.NodesServerManager.FlushDb();
|
||||
break;
|
||||
|
||||
case Define.CACHEPROVIDER_MEMORY:
|
||||
MemoryCacheHelper.RemoveCacheAll();
|
||||
break;
|
||||
|
||||
default:
|
||||
MemoryCacheHelper.RemoveCacheAll();
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 不存在就插入
|
||||
/// </summary>
|
||||
/// <param name="key">缓存Key</param>
|
||||
/// <param name="value">缓存Value</param>
|
||||
/// <param name="second">过期时间</param>
|
||||
/// <returns></returns>
|
||||
public static async Task<bool> SetNxAsync(string key, object value, int second = 10)
|
||||
{
|
||||
bool result = false;
|
||||
switch (cacheProvider)
|
||||
{
|
||||
case Define.CACHEPROVIDER_REDIS:
|
||||
result = await BaseHelper.SetNxAsync(key, value);
|
||||
await BaseHelper.ExpireAtAsync(key, DateTime.Now.AddSeconds(second));
|
||||
break;
|
||||
|
||||
case Define.CACHEPROVIDER_MEMORY:
|
||||
if (MemoryCacheHelper.Exists(key))
|
||||
{
|
||||
result = false;
|
||||
MemoryCacheHelper.Get(key);
|
||||
}
|
||||
else
|
||||
{
|
||||
result = true;
|
||||
MemoryCacheHelper.Set(key, value, TimeSpan.FromSeconds(second), true);
|
||||
}
|
||||
break;
|
||||
|
||||
default:
|
||||
if (MemoryCacheHelper.Exists(key))
|
||||
{
|
||||
result = false;
|
||||
MemoryCacheHelper.Get(key);
|
||||
}
|
||||
else
|
||||
{
|
||||
result = true;
|
||||
MemoryCacheHelper.Set(key, value, TimeSpan.FromSeconds(second), true);
|
||||
}
|
||||
break;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 不存在就插入
|
||||
/// </summary>
|
||||
/// <param name="key">缓存Key</param>
|
||||
/// <param name="value">缓存Value</param>
|
||||
/// <param name="second">过期时间</param>
|
||||
/// <returns></returns>
|
||||
public static bool SetNx(string key, object value, int second = 10)
|
||||
{
|
||||
bool result = false;
|
||||
switch (cacheProvider)
|
||||
{
|
||||
case Define.CACHEPROVIDER_REDIS:
|
||||
result = BaseHelper.SetNx(key, value);
|
||||
BaseHelper.ExpireAt(key, DateTime.Now.AddSeconds(second));
|
||||
break;
|
||||
|
||||
case Define.CACHEPROVIDER_MEMORY:
|
||||
if (MemoryCacheHelper.Exists(key))
|
||||
{
|
||||
result = false;
|
||||
MemoryCacheHelper.Get(key);
|
||||
}
|
||||
else
|
||||
{
|
||||
result = true;
|
||||
MemoryCacheHelper.Set(key, value, TimeSpan.FromSeconds(second), true);
|
||||
}
|
||||
break;
|
||||
|
||||
default:
|
||||
if (MemoryCacheHelper.Exists(key))
|
||||
{
|
||||
result = false;
|
||||
MemoryCacheHelper.Get(key);
|
||||
}
|
||||
else
|
||||
{
|
||||
result = true;
|
||||
MemoryCacheHelper.Set(key, value, TimeSpan.FromSeconds(second), true);
|
||||
}
|
||||
break;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
public static async Task<IEnumerable<string>> GetAllKeyAsync<V>()
|
||||
{
|
||||
switch (cacheProvider)
|
||||
{
|
||||
case Define.CACHEPROVIDER_REDIS:
|
||||
return await BaseHelper.KeysAsync("SqlSugarDataCache.*");
|
||||
|
||||
case Define.CACHEPROVIDER_MEMORY:
|
||||
return MemoryCacheHelper.GetCacheKeys();
|
||||
|
||||
default:
|
||||
return MemoryCacheHelper.GetCacheKeys();
|
||||
}
|
||||
}
|
||||
|
||||
public static IEnumerable<string> GetAllKey<V>()
|
||||
{
|
||||
switch (cacheProvider)
|
||||
{
|
||||
case Define.CACHEPROVIDER_REDIS:
|
||||
return BaseHelper.Keys("SqlSugarDataCache.*");
|
||||
|
||||
case Define.CACHEPROVIDER_MEMORY:
|
||||
return MemoryCacheHelper.GetCacheKeys();
|
||||
|
||||
default:
|
||||
return MemoryCacheHelper.GetCacheKeys();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
227
HT.Cloud.Code/Cache/MemoryCacheHelper.cs
Normal file
227
HT.Cloud.Code/Cache/MemoryCacheHelper.cs
Normal file
@ -0,0 +1,227 @@
|
||||
using Microsoft.Extensions.Caching.Memory;
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using System.Text.RegularExpressions;
|
||||
|
||||
namespace HT.Cloud.Code
|
||||
{
|
||||
/// <summary>
|
||||
/// 缓存帮助类
|
||||
/// </summary>
|
||||
public class MemoryCacheHelper
|
||||
{
|
||||
private static readonly MemoryCache Cache = new MemoryCache(new MemoryCacheOptions());
|
||||
|
||||
/// <summary>
|
||||
/// 验证缓存项是否存在
|
||||
/// </summary>
|
||||
/// <param name="key">缓存Key</param>
|
||||
/// <returns></returns>
|
||||
public static bool Exists(string key)
|
||||
{
|
||||
if (key == null)
|
||||
throw new ArgumentNullException(nameof(key));
|
||||
return Cache.TryGetValue(key, out _);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 添加缓存
|
||||
/// </summary>
|
||||
/// <param name="key">缓存Key</param>
|
||||
/// <param name="value">缓存Value</param>
|
||||
/// <param name="expiresSliding">滑动过期时长(如果在过期时间内有操作,则以当前时间点延长过期时间)</param>
|
||||
/// <param name="expiressAbsoulte">绝对过期时长</param>
|
||||
/// <returns></returns>
|
||||
public static bool Set(string key, object value, TimeSpan expiresSliding, TimeSpan expiressAbsoulte)
|
||||
{
|
||||
if (key == null)
|
||||
throw new ArgumentNullException(nameof(key));
|
||||
if (value == null)
|
||||
throw new ArgumentNullException(nameof(value));
|
||||
|
||||
Cache.Set(key, value.ToJson(),
|
||||
new MemoryCacheEntryOptions().SetSlidingExpiration(expiresSliding)
|
||||
.SetAbsoluteExpiration(expiressAbsoulte));
|
||||
return Exists(key);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 添加缓存
|
||||
/// </summary>
|
||||
/// <param name="key">缓存Key</param>
|
||||
/// <param name="value">缓存Value</param>
|
||||
/// <param name="expiresIn">缓存时长</param>
|
||||
/// <param name="isSliding">是否滑动过期(如果在过期时间内有操作,则以当前时间点延长过期时间)</param>
|
||||
/// <returns></returns>
|
||||
public static bool Set(string key, object value, TimeSpan expiresIn, bool isSliding = false)
|
||||
{
|
||||
if (key == null)
|
||||
throw new ArgumentNullException(nameof(key));
|
||||
if (value == null)
|
||||
throw new ArgumentNullException(nameof(value));
|
||||
Cache.Set(key, value.ToJson(),
|
||||
isSliding
|
||||
? new MemoryCacheEntryOptions().SetSlidingExpiration(expiresIn)
|
||||
: new MemoryCacheEntryOptions().SetAbsoluteExpiration(expiresIn));
|
||||
|
||||
return Exists(key);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 添加缓存
|
||||
/// </summary>
|
||||
/// <param name="key">缓存Key</param>
|
||||
/// <param name="value">缓存Value</param>
|
||||
/// <returns></returns>
|
||||
public static bool Set(string key, object value)
|
||||
{
|
||||
if (key == null)
|
||||
throw new ArgumentNullException(nameof(key));
|
||||
if (value == null)
|
||||
throw new ArgumentNullException(nameof(value));
|
||||
Cache.Set(key, value.ToJson());
|
||||
|
||||
return Exists(key);
|
||||
}
|
||||
|
||||
#region 删除缓存
|
||||
|
||||
/// <summary>
|
||||
/// 删除缓存
|
||||
/// </summary>
|
||||
/// <param name="key">缓存Key</param>
|
||||
/// <returns></returns>
|
||||
public static void Remove(string key)
|
||||
{
|
||||
if (key == null)
|
||||
throw new ArgumentNullException(nameof(key));
|
||||
|
||||
Cache.Remove(key);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 批量删除缓存
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public static void RemoveAll(IEnumerable<string> keys)
|
||||
{
|
||||
if (keys == null)
|
||||
throw new ArgumentNullException(nameof(keys));
|
||||
|
||||
keys.ToList().ForEach(item => Cache.Remove(item));
|
||||
}
|
||||
|
||||
#endregion 删除缓存
|
||||
|
||||
#region 获取缓存
|
||||
|
||||
/// <summary>
|
||||
/// 获取缓存
|
||||
/// </summary>
|
||||
/// <param name="key">缓存Key</param>
|
||||
/// <returns></returns>
|
||||
public static T Get<T>(string key)
|
||||
{
|
||||
if (key == null)
|
||||
throw new ArgumentNullException(nameof(key));
|
||||
object temp;
|
||||
if (Cache.TryGetValue(key, out temp))
|
||||
{
|
||||
return temp.ToString().ToObject<T>();
|
||||
}
|
||||
return default(T);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取缓存
|
||||
/// </summary>
|
||||
/// <param name="key">缓存Key</param>
|
||||
/// <returns></returns>
|
||||
public static string Get(string key)
|
||||
{
|
||||
if (key == null)
|
||||
throw new ArgumentNullException(nameof(key));
|
||||
if (Cache.Get(key) == null)
|
||||
{
|
||||
return string.Empty;
|
||||
}
|
||||
return Cache.Get(key).ToString();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取缓存集合
|
||||
/// </summary>
|
||||
/// <param name="keys">缓存Key集合</param>
|
||||
/// <returns></returns>
|
||||
public static IDictionary<string, object> GetAll(IEnumerable<string> keys)
|
||||
{
|
||||
if (keys == null)
|
||||
throw new ArgumentNullException(nameof(keys));
|
||||
|
||||
var dict = new Dictionary<string, object>();
|
||||
keys.ToList().ForEach(item => dict.Add(item, Cache.Get(item)));
|
||||
return dict;
|
||||
}
|
||||
|
||||
#endregion 获取缓存
|
||||
|
||||
/// <summary>
|
||||
/// 删除所有缓存
|
||||
/// </summary>
|
||||
public static void RemoveCacheAll()
|
||||
{
|
||||
var l = GetCacheKeys();
|
||||
foreach (var s in l)
|
||||
{
|
||||
Remove(s);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 删除匹配到的缓存
|
||||
/// </summary>
|
||||
/// <param name="pattern"></param>
|
||||
/// <returns></returns>
|
||||
public static void RemoveCacheRegex(string pattern)
|
||||
{
|
||||
IEnumerable<string> l = SearchCacheRegex(pattern);
|
||||
foreach (var s in l)
|
||||
{
|
||||
Remove(s);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 搜索 匹配到的缓存
|
||||
/// </summary>
|
||||
/// <param name="pattern"></param>
|
||||
/// <returns></returns>
|
||||
public static IEnumerable<string> SearchCacheRegex(string pattern)
|
||||
{
|
||||
var cacheKeys = GetCacheKeys();
|
||||
var l = cacheKeys.Where(k => Regex.IsMatch(k, pattern)).ToList();
|
||||
return l.AsReadOnly();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取所有缓存键
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public static List<string> GetCacheKeys()
|
||||
{
|
||||
const BindingFlags flags = BindingFlags.Instance | BindingFlags.NonPublic;
|
||||
var entries = Cache.GetType().GetField("_entries", flags).GetValue(Cache);
|
||||
var cacheItems = entries as IDictionary;
|
||||
var keys = new List<string>();
|
||||
if (cacheItems == null) return keys;
|
||||
foreach (DictionaryEntry cacheItem in cacheItems)
|
||||
{
|
||||
keys.Add(cacheItem.Key.ToString());
|
||||
}
|
||||
return keys;
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user