mirror of
http://git.coreshop.cn/jianweie/coreshoppro.git
synced 2025-12-06 16:23:26 +08:00
添加项目文件。
This commit is contained in:
@@ -0,0 +1,47 @@
|
||||
/***********************************************************************
|
||||
* Project: CoreCms
|
||||
* ProjectName: 核心内容管理系统
|
||||
* Web: https://www.corecms.net
|
||||
* Author: 大灰灰
|
||||
* Email: jianweie@163.com
|
||||
* CreateTime: 2021/7/13 21:58:04
|
||||
* Description: 暂无
|
||||
***********************************************************************/
|
||||
|
||||
|
||||
using System.Threading.Tasks;
|
||||
using CoreCms.Net.Caching.Manual;
|
||||
using CoreCms.Net.Configuration;
|
||||
using CoreCms.Net.Model.Entities;
|
||||
|
||||
namespace CoreCms.Net.Caching.AccressToken
|
||||
{
|
||||
/// <summary>
|
||||
/// 微信帮助类
|
||||
/// </summary>
|
||||
public static class WeChatCacheAccessTokenHelper
|
||||
{
|
||||
/// <summary>
|
||||
/// 获取微信小程序accessToken
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public static string GetWxOpenAccessToken()
|
||||
{
|
||||
//获取小程序AccessToken
|
||||
var cacheAccessToken = ManualDataCache.Instance.Get<WeChatAccessToken>(GlobalEnumVars.AccessTokenEnum.WxOpenAccessToken.ToString());
|
||||
return cacheAccessToken?.accessToken;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取微信公众号accessToken
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public static string GetWeChatAccessToken()
|
||||
{
|
||||
//获取微信AccessToken
|
||||
var cacheAccessToken = ManualDataCache.Instance.Get<WeChatAccessToken>(GlobalEnumVars.AccessTokenEnum.WeiXinAccessToken.ToString());
|
||||
return cacheAccessToken?.accessToken;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
12
CoreCms.Net.Caching/AutoMate/MemoryCache/ICachingProvider.cs
Normal file
12
CoreCms.Net.Caching/AutoMate/MemoryCache/ICachingProvider.cs
Normal file
@@ -0,0 +1,12 @@
|
||||
namespace CoreCms.Net.Caching.AutoMate.MemoryCache
|
||||
{
|
||||
/// <summary>
|
||||
/// 简单的缓存接口,只有查询和添加,以后会进行扩展
|
||||
/// </summary>
|
||||
public interface ICachingProvider
|
||||
{
|
||||
object Get(string cacheKey);
|
||||
|
||||
void Set(string cacheKey, object cacheValue, int timeSpan);
|
||||
}
|
||||
}
|
||||
30
CoreCms.Net.Caching/AutoMate/MemoryCache/MemoryCaching.cs
Normal file
30
CoreCms.Net.Caching/AutoMate/MemoryCache/MemoryCaching.cs
Normal file
@@ -0,0 +1,30 @@
|
||||
using Microsoft.Extensions.Caching.Memory;
|
||||
using System;
|
||||
|
||||
namespace CoreCms.Net.Caching.AutoMate.MemoryCache
|
||||
{
|
||||
/// <summary>
|
||||
/// 实例化缓存接口ICaching
|
||||
/// </summary>
|
||||
public class MemoryCaching : ICachingProvider
|
||||
{
|
||||
//引用Microsoft.Extensions.Caching.Memory;这个和.net 还是不一样,没有了Httpruntime了
|
||||
private readonly IMemoryCache _cache;
|
||||
//还是通过构造函数的方法,获取
|
||||
public MemoryCaching(IMemoryCache cache)
|
||||
{
|
||||
_cache = cache;
|
||||
}
|
||||
|
||||
public object Get(string cacheKey)
|
||||
{
|
||||
return _cache.Get(cacheKey);
|
||||
}
|
||||
|
||||
public void Set(string cacheKey, object cacheValue, int timeSpan)
|
||||
{
|
||||
_cache.Set(cacheKey, cacheValue, TimeSpan.FromSeconds(timeSpan * 60));
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,143 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
using StackExchange.Redis;
|
||||
|
||||
namespace CoreCms.Net.Caching.AutoMate.RedisCache
|
||||
{
|
||||
/// <summary>
|
||||
/// Redis缓存接口
|
||||
/// </summary>
|
||||
public interface IRedisOperationRepository
|
||||
{
|
||||
|
||||
//获取 Reids 缓存值
|
||||
Task<string> Get(string key);
|
||||
|
||||
//获取值,并序列化
|
||||
Task<TEntity> Get<TEntity>(string key);
|
||||
|
||||
//保存
|
||||
Task Set(string key, object value, TimeSpan cacheTime);
|
||||
|
||||
//判断是否存在
|
||||
Task<bool> Exist(string key);
|
||||
|
||||
//移除某一个缓存值
|
||||
Task Remove(string key);
|
||||
|
||||
//全部清除
|
||||
Task Clear();
|
||||
|
||||
/// <summary>
|
||||
/// 根据key获取RedisValue
|
||||
/// </summary>
|
||||
/// <param name="redisKey"></param>
|
||||
/// <returns></returns>
|
||||
Task<RedisValue[]> ListRangeAsync(string redisKey);
|
||||
|
||||
/// <summary>
|
||||
/// 在列表头部插入值。如果键不存在,先创建再插入值
|
||||
/// </summary>
|
||||
/// <param name="redisKey"></param>
|
||||
/// <param name="redisValue"></param>
|
||||
/// <returns></returns>
|
||||
Task<long> ListLeftPushAsync(string redisKey, string redisValue);
|
||||
|
||||
/// <summary>
|
||||
/// 在列表尾部插入值。如果键不存在,先创建再插入值
|
||||
/// </summary>
|
||||
/// <param name="redisKey"></param>
|
||||
/// <param name="redisValue"></param>
|
||||
/// <returns></returns>
|
||||
Task<long> ListRightPushAsync(string redisKey, string redisValue);
|
||||
|
||||
/// <summary>
|
||||
/// 在列表尾部插入数组集合。如果键不存在,先创建再插入值
|
||||
/// </summary>
|
||||
/// <param name="redisKey"></param>
|
||||
/// <param name="redisValue"></param>
|
||||
/// <returns></returns>
|
||||
Task<long> ListRightPushAsync(string redisKey, IEnumerable<string> redisValue);
|
||||
|
||||
/// <summary>
|
||||
/// 移除并返回存储在该键列表的第一个元素 反序列化
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <param name="redisKey"></param>
|
||||
/// <returns></returns>
|
||||
Task<T> ListLeftPopAsync<T>(string redisKey) where T : class;
|
||||
|
||||
/// <summary>
|
||||
/// 移除并返回存储在该键列表的最后一个元素 反序列化
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <param name="redisKey"></param>
|
||||
/// <returns></returns>
|
||||
Task<T> ListRightPopAsync<T>(string redisKey) where T : class;
|
||||
|
||||
/// <summary>
|
||||
/// 移除并返回存储在该键列表的第一个元素
|
||||
/// </summary>
|
||||
/// <param name="redisKey"></param>
|
||||
/// <returns></returns>
|
||||
Task<string> ListLeftPopAsync(string redisKey);
|
||||
|
||||
/// <summary>
|
||||
/// 移除并返回存储在该键列表的最后一个元素
|
||||
/// </summary>
|
||||
/// <param name="redisKey"></param>
|
||||
/// <returns></returns>
|
||||
Task<string> ListRightPopAsync(string redisKey);
|
||||
|
||||
/// <summary>
|
||||
/// 列表长度
|
||||
/// </summary>
|
||||
/// <param name="redisKey"></param>
|
||||
/// <returns></returns>
|
||||
Task<long> ListLengthAsync(string redisKey);
|
||||
|
||||
/// <summary>
|
||||
/// 返回在该列表上键所对应的元素
|
||||
/// </summary>
|
||||
/// <param name="redisKey"></param>
|
||||
/// <param name="db"></param>
|
||||
/// <returns></returns>
|
||||
Task<IEnumerable<string>> ListRangeAsync(string redisKey, int db = -1);
|
||||
|
||||
/// <summary>
|
||||
/// 根据索引获取指定位置数据
|
||||
/// </summary>
|
||||
/// <param name="redisKey"></param>
|
||||
/// <param name="start"></param>
|
||||
/// <param name="stop"></param>
|
||||
/// <returns></returns>
|
||||
Task<IEnumerable<string>> ListRangeAsync(string redisKey, int start, int stop);
|
||||
|
||||
/// <summary>
|
||||
/// 删除List中的元素 并返回删除的个数
|
||||
/// </summary>
|
||||
/// <param name="redisKey"></param>
|
||||
/// <param name="redisValue"></param>
|
||||
/// <param name="type"></param>
|
||||
/// <returns></returns>
|
||||
Task<long> ListDelRangeAsync(string redisKey, string redisValue, long type = 0);
|
||||
|
||||
/// <summary>
|
||||
/// 清空List
|
||||
/// </summary>
|
||||
/// <param name="redisKey"></param>
|
||||
/// <returns></returns>
|
||||
Task ListClearAsync(string redisKey);
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 有序集合/定时任务延迟队列用的多
|
||||
/// </summary>
|
||||
/// <param name="redisKey">key</param>
|
||||
/// <param name="redisValue">元素</param>
|
||||
/// <param name="score">分数</param>
|
||||
Task SortedSetAddAsync(string redisKey, string redisValue, double score);
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,243 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Newtonsoft.Json;
|
||||
using StackExchange.Redis;
|
||||
|
||||
namespace CoreCms.Net.Caching.AutoMate.RedisCache
|
||||
{
|
||||
public class RedisOperationRepository : IRedisOperationRepository
|
||||
{
|
||||
private readonly ILogger<RedisOperationRepository> _logger;
|
||||
private readonly ConnectionMultiplexer _redis;
|
||||
private readonly IDatabase _database;
|
||||
|
||||
public RedisOperationRepository(ILogger<RedisOperationRepository> logger, ConnectionMultiplexer redis)
|
||||
{
|
||||
_logger = logger;
|
||||
_redis = redis;
|
||||
_database = redis.GetDatabase();
|
||||
}
|
||||
|
||||
private IServer GetServer()
|
||||
{
|
||||
var endpoint = _redis.GetEndPoints();
|
||||
return _redis.GetServer(endpoint.First());
|
||||
}
|
||||
|
||||
public async Task Clear()
|
||||
{
|
||||
foreach (var endPoint in _redis.GetEndPoints())
|
||||
{
|
||||
var server = GetServer();
|
||||
foreach (var key in server.Keys())
|
||||
{
|
||||
await _database.KeyDeleteAsync(key);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<bool> Exist(string key)
|
||||
{
|
||||
return await _database.KeyExistsAsync(key);
|
||||
}
|
||||
|
||||
public async Task<string> Get(string key)
|
||||
{
|
||||
return await _database.StringGetAsync(key);
|
||||
}
|
||||
|
||||
public async Task Remove(string key)
|
||||
{
|
||||
await _database.KeyDeleteAsync(key);
|
||||
}
|
||||
|
||||
public async Task Set(string key, object value, TimeSpan cacheTime)
|
||||
{
|
||||
if (value != null)
|
||||
{
|
||||
//序列化,将object值生成RedisValue
|
||||
await _database.StringSetAsync(key, JsonConvert.SerializeObject(value), cacheTime);
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<TEntity> Get<TEntity>(string key)
|
||||
{
|
||||
var value = await _database.StringGetAsync(key);
|
||||
if (value.HasValue)
|
||||
{
|
||||
//需要用的反序列化,将Redis存储的Byte[],进行反序列化
|
||||
return JsonConvert.DeserializeObject<TEntity>(value);
|
||||
}
|
||||
else
|
||||
{
|
||||
return default;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 根据key获取RedisValue
|
||||
/// </summary>
|
||||
/// <param name="redisKey"></param>
|
||||
/// <returns></returns>
|
||||
public async Task<RedisValue[]> ListRangeAsync(string redisKey)
|
||||
{
|
||||
return await _database.ListRangeAsync(redisKey);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 在列表头部插入值。如果键不存在,先创建再插入值
|
||||
/// </summary>
|
||||
/// <param name="redisKey"></param>
|
||||
/// <param name="redisValue"></param>
|
||||
/// <returns></returns>
|
||||
public async Task<long> ListLeftPushAsync(string redisKey, string redisValue)
|
||||
{
|
||||
return await _database.ListLeftPushAsync(redisKey, redisValue);
|
||||
}
|
||||
/// <summary>
|
||||
/// 在列表尾部插入值。如果键不存在,先创建再插入值
|
||||
/// </summary>
|
||||
/// <param name="redisKey"></param>
|
||||
/// <param name="redisValue"></param>
|
||||
/// <returns></returns>
|
||||
public async Task<long> ListRightPushAsync(string redisKey, string redisValue)
|
||||
{
|
||||
return await _database.ListRightPushAsync(redisKey, redisValue);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 在列表尾部插入数组集合。如果键不存在,先创建再插入值
|
||||
/// </summary>
|
||||
/// <param name="redisKey"></param>
|
||||
/// <param name="redisValue"></param>
|
||||
/// <returns></returns>
|
||||
public async Task<long> ListRightPushAsync(string redisKey, IEnumerable<string> redisValue)
|
||||
{
|
||||
var redislist = new List<RedisValue>();
|
||||
foreach (var item in redisValue)
|
||||
{
|
||||
redislist.Add(item);
|
||||
}
|
||||
return await _database.ListRightPushAsync(redisKey, redislist.ToArray());
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 移除并返回存储在该键列表的第一个元素 反序列化
|
||||
/// </summary>
|
||||
/// <param name="redisKey"></param>
|
||||
/// <returns></returns>
|
||||
public async Task<T> ListLeftPopAsync<T>(string redisKey) where T : class
|
||||
{
|
||||
var cacheValue = await _database.ListLeftPopAsync(redisKey);
|
||||
if (string.IsNullOrEmpty(cacheValue)) return null;
|
||||
var res = JsonConvert.DeserializeObject<T>(cacheValue);
|
||||
return res;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 移除并返回存储在该键列表的最后一个元素 反序列化
|
||||
/// 只能是对象集合
|
||||
/// </summary>
|
||||
/// <param name="redisKey"></param>
|
||||
/// <returns></returns>
|
||||
public async Task<T> ListRightPopAsync<T>(string redisKey) where T : class
|
||||
{
|
||||
var cacheValue = await _database.ListRightPopAsync(redisKey);
|
||||
if (string.IsNullOrEmpty(cacheValue)) return null;
|
||||
var res = JsonConvert.DeserializeObject<T>(cacheValue);
|
||||
return res;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 移除并返回存储在该键列表的第一个元素
|
||||
/// </summary>
|
||||
/// <param name="redisKey"></param>
|
||||
/// <returns></returns>
|
||||
public async Task<string> ListLeftPopAsync(string redisKey)
|
||||
{
|
||||
return await _database.ListLeftPopAsync(redisKey);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 移除并返回存储在该键列表的最后一个元素
|
||||
/// </summary>
|
||||
/// <param name="redisKey"></param>
|
||||
/// <returns></returns>
|
||||
public async Task<string> ListRightPopAsync(string redisKey)
|
||||
{
|
||||
return await _database.ListRightPopAsync(redisKey);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 列表长度
|
||||
/// </summary>
|
||||
/// <param name="redisKey"></param>
|
||||
/// <returns></returns>
|
||||
public async Task<long> ListLengthAsync(string redisKey)
|
||||
{
|
||||
return await _database.ListLengthAsync(redisKey);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 返回在该列表上键所对应的元素
|
||||
/// </summary>
|
||||
/// <param name="redisKey"></param>
|
||||
/// <returns></returns>
|
||||
public async Task<IEnumerable<string>> ListRangeAsync(string redisKey, int db = -1)
|
||||
{
|
||||
var result = await _database.ListRangeAsync(redisKey);
|
||||
return result.Select(o => o.ToString());
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 根据索引获取指定位置数据
|
||||
/// </summary>
|
||||
/// <param name="redisKey"></param>
|
||||
/// <param name="start"></param>
|
||||
/// <param name="stop"></param>
|
||||
/// <returns></returns>
|
||||
public async Task<IEnumerable<string>> ListRangeAsync(string redisKey, int start, int stop)
|
||||
{
|
||||
var result = await _database.ListRangeAsync(redisKey, start, stop);
|
||||
return result.Select(o => o.ToString());
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 删除List中的元素 并返回删除的个数
|
||||
/// </summary>
|
||||
/// <param name="redisKey">key</param>
|
||||
/// <param name="redisValue">元素</param>
|
||||
/// <param name="type">大于零 : 从表头开始向表尾搜索,小于零 : 从表尾开始向表头搜索,等于零:移除表中所有与 VALUE 相等的值</param>
|
||||
/// <returns></returns>
|
||||
public async Task<long> ListDelRangeAsync(string redisKey, string redisValue, long type = 0)
|
||||
{
|
||||
return await _database.ListRemoveAsync(redisKey, redisValue, type);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 清空List
|
||||
/// </summary>
|
||||
/// <param name="redisKey"></param>
|
||||
public async Task ListClearAsync(string redisKey)
|
||||
{
|
||||
await _database.ListTrimAsync(redisKey, 1, 0);
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 有序集合/定时任务延迟队列用的多
|
||||
/// </summary>
|
||||
/// <param name="redisKey">key</param>
|
||||
/// <param name="redisValue">元素</param>
|
||||
/// <param name="score">分数</param>
|
||||
public async Task SortedSetAddAsync(string redisKey, string redisValue, double score)
|
||||
{
|
||||
await _database.SortedSetAddAsync(redisKey, redisValue, score);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
18
CoreCms.Net.Caching/CoreCms.Net.Caching.csproj
Normal file
18
CoreCms.Net.Caching/CoreCms.Net.Caching.csproj
Normal file
@@ -0,0 +1,18 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net5.0</TargetFramework>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.Extensions.Caching.Memory" Version="5.0.0" />
|
||||
<PackageReference Include="SKIT.FlurlHttpClient.Wechat.Api" Version="1.9.0" />
|
||||
<PackageReference Include="StackExchange.Redis" Version="2.2.50" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\CoreCms.Net.Configuration\CoreCms.Net.Configuration.csproj" />
|
||||
<ProjectReference Include="..\CoreCms.Net.Utility\CoreCms.Net.Utility.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
282
CoreCms.Net.Caching/SqlSugar/SqlSugarMemoryCache.cs
Normal file
282
CoreCms.Net.Caching/SqlSugar/SqlSugarMemoryCache.cs
Normal file
@@ -0,0 +1,282 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using System.Text;
|
||||
using System.Text.RegularExpressions;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.Extensions.Caching.Memory;
|
||||
using SqlSugar;
|
||||
|
||||
namespace CoreCms.Net.Caching.SqlSugar
|
||||
{
|
||||
public class SqlSugarMemoryCache : ICacheService
|
||||
{
|
||||
MemoryCacheHelper cache = new MemoryCacheHelper();
|
||||
public void Add<V>(string key, V value)
|
||||
{
|
||||
cache.Set(key, value);
|
||||
}
|
||||
|
||||
public void Add<V>(string key, V value, int cacheDurationInSeconds)
|
||||
{
|
||||
cache.Set(key, value, cacheDurationInSeconds);
|
||||
}
|
||||
|
||||
public bool ContainsKey<V>(string key)
|
||||
{
|
||||
return cache.Exists(key);
|
||||
}
|
||||
|
||||
public V Get<V>(string key)
|
||||
{
|
||||
return cache.Get<V>(key);
|
||||
}
|
||||
|
||||
public IEnumerable<string> GetAllKey<V>()
|
||||
{
|
||||
return cache.GetCacheKeys();
|
||||
}
|
||||
|
||||
public V GetOrCreate<V>(string cacheKey, Func<V> create, int cacheDurationInSeconds = int.MaxValue)
|
||||
{
|
||||
if (cache.Exists(cacheKey))
|
||||
{
|
||||
return cache.Get<V>(cacheKey);
|
||||
}
|
||||
else
|
||||
{
|
||||
var result = create();
|
||||
cache.Set(cacheKey, result, cacheDurationInSeconds);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
public void Remove<V>(string key)
|
||||
{
|
||||
cache.Remove(key);
|
||||
}
|
||||
}
|
||||
public class MemoryCacheHelper
|
||||
{
|
||||
private static readonly Microsoft.Extensions.Caching.Memory.MemoryCache Cache = new Microsoft.Extensions.Caching.Memory.MemoryCache(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 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 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,
|
||||
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 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,
|
||||
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 void Set(string key, object value)
|
||||
{
|
||||
Set(key, value, TimeSpan.FromDays(1));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 添加缓存
|
||||
/// </summary>
|
||||
/// <param name="key">缓存Key</param>
|
||||
/// <param name="value">缓存Value</param>
|
||||
/// <param name="ts"></param>
|
||||
/// <returns></returns>
|
||||
public void Set(string key, object value, TimeSpan ts)
|
||||
{
|
||||
Set(key, value, ts, false);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 添加缓存
|
||||
/// </summary>
|
||||
/// <param name="key">缓存Key</param>
|
||||
/// <param name="value">缓存Value</param>
|
||||
/// <param name="ts"></param>
|
||||
/// <returns></returns>
|
||||
public void Set(string key, object value, int seconds)
|
||||
{
|
||||
var ts = TimeSpan.FromSeconds(seconds);
|
||||
Set(key, value, ts, false);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 删除缓存
|
||||
/// </summary>
|
||||
/// <param name="key">缓存Key</param>
|
||||
/// <returns></returns>
|
||||
public void Remove(string key)
|
||||
{
|
||||
if (key == null)
|
||||
throw new ArgumentNullException(nameof(key));
|
||||
Cache.Remove(key);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 批量删除缓存
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public void RemoveAll(IEnumerable<string> keys)
|
||||
{
|
||||
if (keys == null)
|
||||
throw new ArgumentNullException(nameof(keys));
|
||||
|
||||
keys.ToList().ForEach(item => Cache.Remove(item));
|
||||
}
|
||||
|
||||
|
||||
#region 获取缓存
|
||||
|
||||
/// <summary>
|
||||
/// 获取缓存
|
||||
/// </summary>
|
||||
/// <param name="key">缓存Key</param>
|
||||
/// <returns></returns>
|
||||
public T Get<T>(string key)
|
||||
{
|
||||
if (key == null)
|
||||
throw new ArgumentNullException(nameof(key));
|
||||
|
||||
return Cache.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 Cache.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, Cache.Get(item)));
|
||||
return dict;
|
||||
}
|
||||
#endregion
|
||||
|
||||
/// <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 = 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
68
CoreCms.Net.Caching/SqlSugar/SqlSugarRedisCache.cs
Normal file
68
CoreCms.Net.Caching/SqlSugar/SqlSugarRedisCache.cs
Normal file
@@ -0,0 +1,68 @@
|
||||
using SqlSugar;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using CoreCms.Net.Caching.Redis;
|
||||
using StackExchange.Redis;
|
||||
using CoreCms.Net.Configuration;
|
||||
|
||||
namespace CoreCms.Net.Caching.SqlSugar
|
||||
{
|
||||
public class SqlSugarRedisCache : ICacheService
|
||||
{
|
||||
readonly RedisCacheManager _service = null;
|
||||
|
||||
public SqlSugarRedisCache()
|
||||
{
|
||||
_service = new RedisCacheManager(); ;
|
||||
}
|
||||
|
||||
public void Add<TV>(string key, TV value)
|
||||
{
|
||||
_service.Set(key, value);
|
||||
}
|
||||
|
||||
public void Add<TV>(string key, TV value, int cacheDurationInSeconds)
|
||||
{
|
||||
_service.Set(key, value, cacheDurationInSeconds);
|
||||
}
|
||||
|
||||
public bool ContainsKey<TV>(string key)
|
||||
{
|
||||
return _service.Exists(key);
|
||||
}
|
||||
|
||||
public TV Get<TV>(string key)
|
||||
{
|
||||
return _service.Get<TV>(key);
|
||||
}
|
||||
|
||||
public IEnumerable<string> GetAllKey<TV>()
|
||||
{
|
||||
|
||||
return _service.SearchCacheRegex("SqlSugarDataCache.*");
|
||||
}
|
||||
|
||||
public TV GetOrCreate<TV>(string cacheKey, Func<TV> create, int cacheDurationInSeconds = int.MaxValue)
|
||||
{
|
||||
if (this.ContainsKey<TV>(cacheKey))
|
||||
{
|
||||
return this.Get<TV>(cacheKey);
|
||||
}
|
||||
else
|
||||
{
|
||||
var result = create();
|
||||
this.Add(cacheKey, result, cacheDurationInSeconds);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
public void Remove<TV>(string key)
|
||||
{
|
||||
_service.Remove(key);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user