using EntrustSettle.Common; using Microsoft.Extensions.Logging; using Newtonsoft.Json; using StackExchange.Redis; using System; using System.Collections.Generic; using System.ComponentModel; using System.Linq; using System.Threading.Tasks; namespace EntrustSettle.Extensions { [Description("普通缓存考虑直接使用ICaching,如果要使用Redis队列等还是使用此类")] public class RedisBasketRepository : IRedisBasketRepository { private readonly ILogger _logger; private readonly ConnectionMultiplexer _redis; private readonly IDatabase _database; public RedisBasketRepository(ILogger 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 Exist(string key) { return await _database.KeyExistsAsync(key); } public async Task GetValue(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) { if (value is string cacheValue) { // 字符串无需序列化 await _database.StringSetAsync(key, cacheValue, cacheTime); } else { //序列化,将object值生成RedisValue await _database.StringSetAsync(key, SerializeHelper.Serialize(value), cacheTime); } } } public async Task Get(string key) { var value = await _database.StringGetAsync(key); if (value.HasValue) { //需要用的反序列化,将Redis存储的Byte[],进行反序列化 return SerializeHelper.Deserialize(value); } else { return default(TEntity); } } /// /// 根据key获取RedisValue /// /// /// /// public async Task ListRangeAsync(string redisKey) { return await _database.ListRangeAsync(redisKey); } /// /// 在列表头部插入值。如果键不存在,先创建再插入值 /// /// /// /// public async Task ListLeftPushAsync(string redisKey, string redisValue, int db = -1) { return await _database.ListLeftPushAsync(redisKey, redisValue); } /// /// 在列表尾部插入值。如果键不存在,先创建再插入值 /// /// /// /// public async Task ListRightPushAsync(string redisKey, string redisValue, int db = -1) { return await _database.ListRightPushAsync(redisKey, redisValue); } /// /// 在列表尾部插入数组集合。如果键不存在,先创建再插入值 /// /// /// /// public async Task ListRightPushAsync(string redisKey, IEnumerable redisValue, int db = -1) { var redislist = new List(); foreach (var item in redisValue) { redislist.Add(item); } return await _database.ListRightPushAsync(redisKey, redislist.ToArray()); } /// /// 移除并返回存储在该键列表的第一个元素 反序列化 /// /// /// public async Task ListLeftPopAsync(string redisKey, int db = -1) where T : class { return JsonConvert.DeserializeObject(await _database.ListLeftPopAsync(redisKey)); } /// /// 移除并返回存储在该键列表的最后一个元素 反序列化 /// 只能是对象集合 /// /// /// public async Task ListRightPopAsync(string redisKey, int db = -1) where T : class { return JsonConvert.DeserializeObject(await _database.ListRightPopAsync(redisKey)); } /// /// 移除并返回存储在该键列表的第一个元素 /// /// /// /// public async Task ListLeftPopAsync(string redisKey, int db = -1) { return await _database.ListLeftPopAsync(redisKey); } /// /// 移除并返回存储在该键列表的最后一个元素 /// /// /// /// /// public async Task ListRightPopAsync(string redisKey, int db = -1) { return await _database.ListRightPopAsync(redisKey); } /// /// 列表长度 /// /// /// /// public async Task ListLengthAsync(string redisKey, int db = -1) { return await _database.ListLengthAsync(redisKey); } /// /// 返回在该列表上键所对应的元素 /// /// /// public async Task> ListRangeAsync(string redisKey, int db = -1) { var result = await _database.ListRangeAsync(redisKey); return result.Select(o => o.ToString()); } /// /// 根据索引获取指定位置数据 /// /// /// /// /// /// public async Task> ListRangeAsync(string redisKey, int start, int stop, int db = -1) { var result = await _database.ListRangeAsync(redisKey, start, stop); return result.Select(o => o.ToString()); } /// /// 删除List中的元素 并返回删除的个数 /// /// key /// 元素 /// 大于零 : 从表头开始向表尾搜索,小于零 : 从表尾开始向表头搜索,等于零:移除表中所有与 VALUE 相等的值 /// /// public async Task ListDelRangeAsync(string redisKey, string redisValue, long type = 0, int db = -1) { return await _database.ListRemoveAsync(redisKey, redisValue, type); } /// /// 清空List /// /// /// public async Task ListClearAsync(string redisKey, int db = -1) { await _database.ListTrimAsync(redisKey, 1, 0); } } }