You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

385 lines
13 KiB
C#

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

using Admin.Core.Common;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using StackExchange.Redis;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace Admin.Core.Extensions
{
public class RedisBasketRepository : IRedisBasketRepository
{
private readonly ILogger<RedisBasketRepository> _logger;
private readonly ConnectionMultiplexer _redis;
private readonly IDatabase _database;
public RedisBasketRepository(ILogger<RedisBasketRepository> logger, ConnectionMultiplexer redis)
{
_logger = logger;
_redis = redis;
_database = redis.GetDatabase();
}
private IServer GetServer()
{
var endpoint = _redis.GetEndPoints();
return _redis.GetServer(endpoint.First());
}
private IServer GetServer(System.Net.EndPoint endpoint)
{
return _redis.GetServer(endpoint);
}
/// <summary>
/// 获取服务基础信息
/// </summary>
public RedisBaseView GetBaseInfo()
{
var server = GetServer();
RedisBaseView view = new RedisBaseView()
{
redis_version = server.Version.ToString(),
redis_mode = server.ServerType.ToString(),
connected_clients = "",
uptime_in_days = "",
used_cpu_user_children = "",
maxmemory_human = "",
aof_enabled = "",
rdb_last_bgsave_status = "",
dbSize = "",
instantaneous_input_kbps = "",
instantaneous_output_kbps = "",
};
return view;
}
/// <summary>
/// 缓存监控
/// </summary>
public List<RedisKeyView> GetAllKes()
{
List<RedisKeyView> redisKeys = new List<RedisKeyView>();
foreach (var endPoint in _redis.GetEndPoints())
{
var server = _redis.GetServer(endPoint);
foreach (var key in server.Keys())
{
RedisKeyView redisKey = new RedisKeyView()
{
Server = server.ToString(),
Point = endPoint.ToString(),
Key = key
};
redisKeys.Add(redisKey);
}
}
return redisKeys;
}
/// <summary>
/// 节点状态
/// </summary>
/// <returns></returns>
public List<PointStatus> GetPointStatus()
{
//var ServerSelectionStrategy = _redis.OperationCount;
List<PointStatus> statuses = new List<PointStatus>();
var endpoint = _redis.GetEndPoints();
foreach (var item in endpoint)
{
PointStatus status = new PointStatus()
{
Point = item.ToString(),
Normal = GetServer(item).IsConnected
};
statuses.Add(status);
}
return statuses;
}
/// <summary>
/// 全部清除
/// </summary>
/// <returns></returns>
public async Task Clear()
{
foreach (var endPoint in _redis.GetEndPoints())
{
var server = GetServer();
foreach (var key in server.Keys())
{
await _database.KeyDeleteAsync(key);
}
}
}
/// <summary>
/// 判断是否存在
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
public async Task<bool> Exist(string key)
{
return await _database.KeyExistsAsync(key);
}
/// <summary>
/// 获取 Reids 缓存值
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
public async Task<string> GetValue(string key)
{
return await _database.StringGetAsync(key);
}
/// <summary>
/// 移除某一个缓存值
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
public async Task<bool> Remove(string key)
{
return await _database.KeyDeleteAsync(key);
}
/// <summary>
/// 保存-默认系统过期时间
/// </summary>
/// <param name="key"></param>
/// <param name="value"></param>
/// <param name="timeSpan">缓存过期时间,单位:分钟</param>
/// <returns></returns>
public async Task<bool> SetStr(string key, string value, int timeSpan = 0)
{
value = value.IsNotEmptyOrNull() ? value : string.Empty;
// 缓存过期时间:分钟
var cacheTimeOut = Appsettings.app(new string[] { "AppSettings", "CacheTimeOut" }).ObjToInt();
var cacheTime = TimeSpan.FromSeconds(timeSpan == 0 ? cacheTimeOut * 60 : timeSpan * 60);
//序列化将object值生成RedisValue
return await _database.StringSetAsync(key, value, cacheTime);
}
/// <summary>
/// 保存-永不过期
/// </summary>
/// <param name="key"></param>
/// <param name="value"></param>
/// <returns></returns>
public async Task<bool> SetStrL(string key, string value)
{
value = value.IsNotEmptyOrNull() ? value : string.Empty;
//序列化将object值生成RedisValue
return await _database.StringSetAsync(key, value);
}
/// <summary>
/// 保存-默认系统过期时间
/// </summary>
/// <param name="key"></param>
/// <param name="value"></param>
/// <param name="timeSpan">缓存过期时间,单位:分钟</param>
/// <returns></returns>
public async Task<bool> SetObj(string key, object value, int timeSpan = 0)
{
value = value.IsNotEmptyOrNull() ? value : string.Empty;
// 缓存过期时间:分钟
var cacheTimeOut = Appsettings.app(new string[] { "AppSettings", "CacheTimeOut" }).ObjToInt();
var cacheTime = TimeSpan.FromSeconds(timeSpan == 0 ? cacheTimeOut * 60 : timeSpan * 60);
//序列化将object值生成RedisValue
return await _database.StringSetAsync(key, SerializeHelper.Serialize(value), cacheTime);
}
/// <summary>
/// 保存-永不过期
/// </summary>
/// <param name="key"></param>
/// <param name="value"></param>
/// <returns></returns>
public async Task<bool> SetObjL(string key, object value)
{
value = value.IsNotEmptyOrNull() ? value : string.Empty;
//序列化将object值生成RedisValue
return await _database.StringSetAsync(key, SerializeHelper.Serialize(value));
}
/// <summary>
/// 获取值,并序列化
/// </summary>
/// <typeparam name="TEntity"></typeparam>
/// <param name="key"></param>
/// <returns></returns>
public async Task<TEntity> Get<TEntity>(string key)
{
var value = await _database.StringGetAsync(key);
if (value.HasValue)
{
//需要用的反序列化将Redis存储的Byte[],进行反序列化
return SerializeHelper.Deserialize<TEntity>(value);
}
else
{
return default(TEntity);
}
}
/// <summary>
/// 根据key获取RedisValue
/// </summary>
/// <typeparam name="T"></typeparam>
/// <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, int db = -1)
{
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, int db = -1)
{
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, int db = -1)
{
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, int db = -1) where T : class
{
return JsonConvert.DeserializeObject<T>(await _database.ListLeftPopAsync(redisKey));
}
/// <summary>
/// 移除并返回存储在该键列表的最后一个元素 反序列化
/// 只能是对象集合
/// </summary>
/// <param name="redisKey"></param>
/// <returns></returns>
public async Task<T> ListRightPopAsync<T>(string redisKey, int db = -1) where T : class
{
return JsonConvert.DeserializeObject<T>(await _database.ListRightPopAsync(redisKey));
}
/// <summary>
/// 移除并返回存储在该键列表的第一个元素
/// </summary>
/// <param name="redisKey"></param>
/// <param name="db"></param>
/// <returns></returns>
public async Task<string> ListLeftPopAsync(string redisKey, int db = -1)
{
return await _database.ListLeftPopAsync(redisKey);
}
/// <summary>
/// 移除并返回存储在该键列表的最后一个元素
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="redisKey"></param>
/// <param name="db"></param>
/// <returns></returns>
public async Task<string> ListRightPopAsync(string redisKey, int db = -1)
{
return await _database.ListRightPopAsync(redisKey);
}
/// <summary>
/// 列表长度
/// </summary>
/// <param name="redisKey"></param>
/// <param name="db"></param>
/// <returns></returns>
public async Task<long> ListLengthAsync(string redisKey, int db = -1)
{
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>
/// <param name="db"></param>
/// <returns></returns>
public async Task<IEnumerable<string>> ListRangeAsync(string redisKey, int start, int stop, int db = -1)
{
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>
/// <param name="db"></param>
/// <returns></returns>
public async Task<long> ListDelRangeAsync(string redisKey, string redisValue, long type = 0, int db = -1)
{
return await _database.ListRemoveAsync(redisKey, redisValue, type);
}
/// <summary>
/// 清空List
/// </summary>
/// <param name="redisKey"></param>
/// <param name="db"></param>
public async Task ListClearAsync(string redisKey, int db = -1)
{
await _database.ListTrimAsync(redisKey, 1, 0);
}
}
}