mirror of
http://git.coreshop.cn/jianweie/coreshoppro.git
synced 2025-12-06 17:23:26 +08:00
添加项目文件。
This commit is contained in:
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);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user