mirror of
http://git.coreshop.cn/jianweie/coreshoppro.git
synced 2025-12-06 17:13:26 +08:00
添加项目文件。
This commit is contained in:
88
CoreCms.Net.Caching/Manual/IManualCacheManager.cs
Normal file
88
CoreCms.Net.Caching/Manual/IManualCacheManager.cs
Normal file
@@ -0,0 +1,88 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace CoreCms.Net.Caching.Manual
|
||||
{
|
||||
/// <summary>
|
||||
/// 手动缓存操作接口
|
||||
/// </summary>
|
||||
public interface IManualCacheManager
|
||||
{
|
||||
|
||||
/// <summary>
|
||||
/// 验证缓存项是否存在
|
||||
/// </summary>
|
||||
/// <param name="key">缓存Key</param>
|
||||
/// <returns></returns>
|
||||
bool Exists(string key);
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 添加缓存
|
||||
/// </summary>
|
||||
/// <param name="key">缓存Key</param>
|
||||
/// <param name="value">缓存Value</param>
|
||||
/// <param name="expiresIn">缓存时长(分钟)</param>
|
||||
/// <returns></returns>
|
||||
bool Set(string key, object value, int expiresIn = 0);
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 删除缓存
|
||||
/// </summary>
|
||||
/// <param name="key">缓存Key</param>
|
||||
/// <returns></returns>
|
||||
void Remove(string key);
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 批量删除缓存
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
void RemoveAll(IEnumerable<string> keys);
|
||||
|
||||
/// <summary>
|
||||
/// 获取缓存
|
||||
/// </summary>
|
||||
/// <param name="key">缓存Key</param>
|
||||
/// <returns></returns>
|
||||
T Get<T>(string key);
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 获取缓存
|
||||
/// </summary>
|
||||
/// <param name="key">缓存Key</param>
|
||||
/// <returns></returns>
|
||||
object Get(string key);
|
||||
|
||||
/// <summary>
|
||||
/// 获取缓存集合
|
||||
/// </summary>
|
||||
/// <param name="keys">缓存Key集合</param>
|
||||
/// <returns></returns>
|
||||
IDictionary<string, object> GetAll(IEnumerable<string> keys);
|
||||
|
||||
/// <summary>
|
||||
/// 删除所有缓存
|
||||
/// </summary>
|
||||
void RemoveCacheAll();
|
||||
|
||||
/// <summary>
|
||||
/// 删除匹配到的缓存
|
||||
/// </summary>
|
||||
/// <param name="pattern"></param>
|
||||
/// <returns></returns>
|
||||
void RemoveCacheRegex(string pattern);
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 搜索 匹配到的缓存
|
||||
/// </summary>
|
||||
/// <param name="pattern"></param>
|
||||
/// <returns></returns>
|
||||
IList<string> SearchCacheRegex(string pattern);
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
51
CoreCms.Net.Caching/Manual/ManualDataCache.cs
Normal file
51
CoreCms.Net.Caching/Manual/ManualDataCache.cs
Normal file
@@ -0,0 +1,51 @@
|
||||
/***********************************************************************
|
||||
* Project: CoreCms.Net *
|
||||
* Web: https://CoreCms.Net *
|
||||
* ProjectName: 核心内容管理系统 *
|
||||
* Author: 大灰灰 *
|
||||
* Email: JianWeie@163.com *
|
||||
* Versions: 1.0 *
|
||||
* CreateTime: 2020-02-02 14:43:16
|
||||
* NameSpace: CoreCms.Net.Framework.Caching
|
||||
* FileName: DataCache
|
||||
* ClassDescription:
|
||||
***********************************************************************/
|
||||
|
||||
|
||||
using CoreCms.Net.Caching.MemoryCache;
|
||||
using CoreCms.Net.Caching.Redis;
|
||||
using CoreCms.Net.Configuration;
|
||||
using CoreCms.Net.Utility.Extensions;
|
||||
|
||||
namespace CoreCms.Net.Caching.Manual
|
||||
{
|
||||
/// <summary>
|
||||
/// 手动缓存调用
|
||||
/// </summary>
|
||||
public static partial class ManualDataCache
|
||||
{
|
||||
private static IManualCacheManager _instance = null;
|
||||
/// <summary>
|
||||
/// 静态实例,外部可直接调用
|
||||
/// </summary>
|
||||
public static IManualCacheManager Instance
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_instance == null)
|
||||
{
|
||||
if (AppSettingsConstVars.RedisUseCache)
|
||||
{
|
||||
_instance = new RedisCacheManager();
|
||||
}
|
||||
else
|
||||
{
|
||||
_instance = new MemoryCacheManager();
|
||||
|
||||
}
|
||||
}
|
||||
return _instance;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
181
CoreCms.Net.Caching/Manual/MemoryCacheManager.cs
Normal file
181
CoreCms.Net.Caching/Manual/MemoryCacheManager.cs
Normal file
@@ -0,0 +1,181 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using System.Text.RegularExpressions;
|
||||
using CoreCms.Net.Caching.Manual;
|
||||
using Microsoft.Extensions.Caching.Memory;
|
||||
using Microsoft.Extensions.Options;
|
||||
|
||||
namespace CoreCms.Net.Caching.MemoryCache
|
||||
{
|
||||
public class MemoryCacheManager : IManualCacheManager
|
||||
{
|
||||
private static volatile Microsoft.Extensions.Caching.Memory.MemoryCache _memoryCache = new Microsoft.Extensions.Caching.Memory.MemoryCache((IOptions<MemoryCacheOptions>)new MemoryCacheOptions());
|
||||
|
||||
/// <summary>
|
||||
/// 验证缓存项是否存在
|
||||
/// </summary>
|
||||
/// <param name="key">缓存Key</param>
|
||||
/// <returns></returns>
|
||||
public bool Exists(string key)
|
||||
{
|
||||
if (key == null)
|
||||
throw new ArgumentNullException(nameof(key));
|
||||
return _memoryCache.TryGetValue(key, out _);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 添加缓存
|
||||
/// </summary>
|
||||
/// <param name="key">缓存Key</param>
|
||||
/// <param name="value">缓存Value</param>
|
||||
/// <param name="expiresIn">缓存时间</param>
|
||||
/// <returns></returns>
|
||||
public bool Set(string key, object value, int expiresIn = 0)
|
||||
{
|
||||
if (key == null)
|
||||
throw new ArgumentNullException(nameof(key));
|
||||
if (value == null)
|
||||
throw new ArgumentNullException(nameof(value));
|
||||
if (expiresIn > 0)
|
||||
{
|
||||
_memoryCache.Set(key, value,
|
||||
new MemoryCacheEntryOptions().SetSlidingExpiration(TimeSpan.FromSeconds(120))
|
||||
.SetAbsoluteExpiration(TimeSpan.FromMinutes(expiresIn)));
|
||||
}
|
||||
else
|
||||
{
|
||||
_memoryCache.Set(key, value,
|
||||
new MemoryCacheEntryOptions().SetSlidingExpiration(TimeSpan.FromSeconds(120))
|
||||
.SetAbsoluteExpiration(TimeSpan.FromMinutes(1440)));
|
||||
}
|
||||
|
||||
return Exists(key);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 删除缓存
|
||||
/// </summary>
|
||||
/// <param name="key">缓存Key</param>
|
||||
/// <returns></returns>
|
||||
public void Remove(string key)
|
||||
{
|
||||
if (key == null)
|
||||
throw new ArgumentNullException(nameof(key));
|
||||
|
||||
_memoryCache.Remove(key);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 批量删除缓存
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public void RemoveAll(IEnumerable<string> keys)
|
||||
{
|
||||
if (keys == null)
|
||||
throw new ArgumentNullException(nameof(keys));
|
||||
|
||||
keys.ToList().ForEach(item => _memoryCache.Remove(item));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取缓存对象
|
||||
/// </summary>
|
||||
/// <param name="key">缓存Key</param>
|
||||
/// <returns></returns>
|
||||
public T Get<T>(string key)
|
||||
{
|
||||
if (key == null)
|
||||
throw new ArgumentNullException(nameof(key));
|
||||
|
||||
return _memoryCache.Get<T>(key);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取缓存
|
||||
/// </summary>
|
||||
/// <param name="key">缓存Key</param>
|
||||
/// <returns></returns>
|
||||
public object Get(string key)
|
||||
{
|
||||
if (key == null)
|
||||
throw new ArgumentNullException(nameof(key));
|
||||
|
||||
return _memoryCache.Get(key);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取缓存集合
|
||||
/// </summary>
|
||||
/// <param name="keys">缓存Key集合</param>
|
||||
/// <returns></returns>
|
||||
public 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, _memoryCache.Get(item)));
|
||||
return dict;
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 删除所有缓存
|
||||
/// </summary>
|
||||
public void RemoveCacheAll()
|
||||
{
|
||||
var l = GetCacheKeys();
|
||||
foreach (var s in l)
|
||||
{
|
||||
Remove(s);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 删除匹配到的缓存
|
||||
/// </summary>
|
||||
/// <param name="pattern"></param>
|
||||
/// <returns></returns>
|
||||
public void RemoveCacheRegex(string pattern)
|
||||
{
|
||||
IList<string> l = SearchCacheRegex(pattern);
|
||||
foreach (var s in l)
|
||||
{
|
||||
Remove(s);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 搜索 匹配到的缓存
|
||||
/// </summary>
|
||||
/// <param name="pattern"></param>
|
||||
/// <returns></returns>
|
||||
public IList<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 List<string> GetCacheKeys()
|
||||
{
|
||||
const BindingFlags flags = BindingFlags.Instance | BindingFlags.NonPublic;
|
||||
var entries = _memoryCache.GetType().GetField("_entries", flags).GetValue(_memoryCache);
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
209
CoreCms.Net.Caching/Manual/RedisCacheManager.cs
Normal file
209
CoreCms.Net.Caching/Manual/RedisCacheManager.cs
Normal file
@@ -0,0 +1,209 @@
|
||||
/***********************************************************************
|
||||
* Project: CoreCms.Net *
|
||||
* Web: https://CoreCms.Net *
|
||||
* ProjectName: 核心内容管理系统 *
|
||||
* Author: 大灰灰 *
|
||||
* Email: JianWeie@163.com *
|
||||
* Versions: 1.0 *
|
||||
* CreateTime: 2020-02-02 14:09:33
|
||||
* ClassDescription:
|
||||
***********************************************************************/
|
||||
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using CoreCms.Net.Caching.Manual;
|
||||
using CoreCms.Net.Configuration;
|
||||
using CoreCms.Net.Utility.Extensions;
|
||||
using StackExchange.Redis;
|
||||
|
||||
namespace CoreCms.Net.Caching.Redis
|
||||
{
|
||||
public class RedisCacheManager : IManualCacheManager
|
||||
{
|
||||
private readonly string _redisConnenctionString;
|
||||
|
||||
public volatile ConnectionMultiplexer RedisConnection;
|
||||
|
||||
private readonly object _redisConnectionLock = new object();
|
||||
|
||||
public RedisCacheManager()
|
||||
{
|
||||
string redisConfiguration = AppSettingsConstVars.RedisConfigConnectionString;//获取连接字符串
|
||||
|
||||
if (string.IsNullOrWhiteSpace(redisConfiguration))
|
||||
{
|
||||
throw new ArgumentException("redis config is empty", nameof(redisConfiguration));
|
||||
}
|
||||
_redisConnenctionString = redisConfiguration;
|
||||
|
||||
RedisConnection = GetRedisConnection();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 核心代码,获取连接实例
|
||||
/// 通过双if 夹lock的方式,实现单例模式
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
private ConnectionMultiplexer GetRedisConnection()
|
||||
{
|
||||
//如果已经连接实例,直接返回
|
||||
if (RedisConnection != null && RedisConnection.IsConnected)
|
||||
{
|
||||
return RedisConnection;
|
||||
}
|
||||
//加锁,防止异步编程中,出现单例无效的问题
|
||||
lock (_redisConnectionLock)
|
||||
{
|
||||
if (RedisConnection != null)
|
||||
{
|
||||
//释放redis连接
|
||||
RedisConnection.Dispose();
|
||||
}
|
||||
try
|
||||
{
|
||||
RedisConnection = ConnectionMultiplexer.Connect(_redisConnenctionString);
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
throw new Exception("Redis服务未启用,请开启该服务,并且请注意端口号,Redis默认使用6379端口号。");
|
||||
}
|
||||
}
|
||||
return RedisConnection;
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 判断key是否存在
|
||||
/// </summary>
|
||||
/// <param name="key"></param>
|
||||
/// <returns></returns>
|
||||
public bool Exists(string key)
|
||||
{
|
||||
return RedisConnection.GetDatabase().KeyExists(key);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 添加缓存
|
||||
/// </summary>
|
||||
/// <param name="key">缓存Key</param>
|
||||
/// <param name="value">缓存Value</param>
|
||||
/// <param name="expiresIn">缓存时间</param>
|
||||
/// <returns></returns>
|
||||
public bool Set(string key, object value, int expiresIn = 0)
|
||||
{
|
||||
if (value != null)
|
||||
{
|
||||
//序列化,将object值生成RedisValue
|
||||
if (expiresIn > 0)
|
||||
{
|
||||
return RedisConnection.GetDatabase().StringSet(key, SerializeExtensions.Serialize(value), TimeSpan.FromMinutes(expiresIn));
|
||||
}
|
||||
else
|
||||
{
|
||||
return RedisConnection.GetDatabase().StringSet(key, SerializeExtensions.Serialize(value));
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 删除缓存
|
||||
/// </summary>
|
||||
/// <param name="key">缓存Key</param>
|
||||
/// <returns></returns>
|
||||
public void Remove(string key)
|
||||
{
|
||||
RedisConnection.GetDatabase().KeyDelete(key);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 批量删除缓存
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public void RemoveAll(IEnumerable<string> keys)
|
||||
{
|
||||
foreach (var key in keys)
|
||||
{
|
||||
RedisConnection.GetDatabase().KeyDelete(key);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取缓存对象
|
||||
/// </summary>
|
||||
/// <param name="key">缓存Key</param>
|
||||
/// <returns></returns>
|
||||
public T Get<T>(string key)
|
||||
{
|
||||
var value = RedisConnection.GetDatabase().StringGet(key);
|
||||
if (value.HasValue)
|
||||
{
|
||||
//需要用的反序列化,将Redis存储的Byte[],进行反序列化
|
||||
return SerializeExtensions.Deserialize<T>(value);
|
||||
}
|
||||
|
||||
return default;
|
||||
}
|
||||
|
||||
public object Get(string key)
|
||||
{
|
||||
return RedisConnection.GetDatabase().StringGet(key);
|
||||
}
|
||||
|
||||
public 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, RedisConnection.GetDatabase().StringGet(item)));
|
||||
return dict;
|
||||
|
||||
}
|
||||
|
||||
public void RemoveCacheAll()
|
||||
{
|
||||
foreach (var endPoint in GetRedisConnection().GetEndPoints())
|
||||
{
|
||||
var server = GetRedisConnection().GetServer(endPoint);
|
||||
foreach (var key in server.Keys())
|
||||
{
|
||||
RedisConnection.GetDatabase().KeyDelete(key);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void RemoveCacheRegex(string pattern)
|
||||
{
|
||||
var script = "return redis.call('keys',@pattern)";
|
||||
var prepared = LuaScript.Prepare(script);
|
||||
var redisResult = RedisConnection.GetDatabase().ScriptEvaluate(prepared, new { pattern });
|
||||
if (!redisResult.IsNull)
|
||||
{
|
||||
RedisConnection.GetDatabase().KeyDelete((RedisKey[])redisResult); //删除一组key
|
||||
}
|
||||
}
|
||||
|
||||
public IList<string> SearchCacheRegex(string pattern)
|
||||
{
|
||||
var list = new List<String>();
|
||||
var script = "return redis.call('keys',@pattern)";
|
||||
var prepared = LuaScript.Prepare(script);
|
||||
var redisResult = RedisConnection.GetDatabase().ScriptEvaluate(prepared, new { pattern });
|
||||
if (!redisResult.IsNull)
|
||||
{
|
||||
foreach (var key in (RedisKey[])redisResult)
|
||||
{
|
||||
list.Add(RedisConnection.GetDatabase().StringGet(key));
|
||||
}
|
||||
}
|
||||
return list;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user