You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
367 lines
12 KiB
367 lines
12 KiB
using Newtonsoft.Json;
|
|
using StackExchange.Redis;
|
|
|
|
namespace Znyc.CloudCar.Caching
|
|
{
|
|
public class RedisOperationRepository : IRedisOperationRepository
|
|
{
|
|
private readonly ConnectionMultiplexer _redis;
|
|
private readonly IDatabase _database;
|
|
|
|
public RedisOperationRepository(ConnectionMultiplexer redis)
|
|
{
|
|
_redis = redis;
|
|
_database = redis.GetDatabase();
|
|
}
|
|
|
|
private IServer GetServer()
|
|
{
|
|
var endpoint = _redis.GetEndPoints();
|
|
return _redis.GetServer(endpoint.First());
|
|
}
|
|
|
|
/// <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>
|
|
/// 检查给定 key 是否存在
|
|
/// </summary>
|
|
/// <param name="key"></param>
|
|
/// <returns></returns>
|
|
public bool Exists(string key)
|
|
{
|
|
return _database.KeyExists(key);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 检查给定 key 是否存在
|
|
/// </summary>
|
|
/// <param name="key">键</param>
|
|
/// <returns></returns>
|
|
public Task<bool> ExistsAsync(string key)
|
|
{
|
|
return _database.KeyExistsAsync(key);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取指定 key 的值
|
|
/// </summary>
|
|
/// <param name="key">键</param>
|
|
/// <returns></returns>
|
|
public string Get(string key)
|
|
{
|
|
return _database.StringGet(key);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取指定 key 的值
|
|
/// </summary>
|
|
/// <typeparam name="T">byte[] 或其他类型</typeparam>
|
|
/// <param name="key">键</param>
|
|
/// <returns></returns>
|
|
public T Get<T>(string key)
|
|
{
|
|
var value = _database.StringGet(key);
|
|
if (value.HasValue)
|
|
{
|
|
return JsonConvert.DeserializeObject<T>(value);
|
|
}
|
|
else
|
|
{
|
|
return default;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取指定 key 的值
|
|
/// </summary>
|
|
/// <param name="key">键</param>
|
|
/// <returns></returns>
|
|
public async Task<string> GetAsync(string key)
|
|
{
|
|
return await _database.StringGetAsync(key);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取指定 key 的值
|
|
/// </summary>
|
|
/// <typeparam name="T">byte[] 或其他类型</typeparam>
|
|
/// <param name="key">键</param>
|
|
/// <returns></returns>
|
|
public async Task<T> GetAsync<T>(string key)
|
|
{
|
|
var value = await _database.StringGetAsync(key);
|
|
if (value.HasValue)
|
|
{
|
|
|
|
return JsonConvert.DeserializeObject<T>(value);
|
|
}
|
|
else
|
|
{
|
|
return default;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 设置指定 key 的值,所有写入参数object都支持string | byte[] | 数值 | 对象
|
|
/// </summary>
|
|
/// <param name="key">键</param>
|
|
/// <param name="value">值</param>
|
|
/// <returns></returns>
|
|
public bool Set(string key, object value)
|
|
{
|
|
return _database.StringSet(key, JsonConvert.SerializeObject(value));
|
|
}
|
|
|
|
/// <summary>
|
|
/// 设置指定 key 的值,所有写入参数object都支持string | byte[] | 数值 | 对象
|
|
/// </summary>
|
|
/// <param name="key">键</param>
|
|
/// <param name="value">值</param>
|
|
/// <param name="expire">有效期</param>
|
|
/// <returns></returns>
|
|
public bool Set(string key, object value, TimeSpan expire)
|
|
{
|
|
return _database.StringSet(key, JsonConvert.SerializeObject(value), expire);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 设置指定 key 的值,所有写入参数object都支持string | byte[] | 数值 | 对象
|
|
/// </summary>
|
|
/// <param name="key">键</param>
|
|
/// <param name="value">值</param>
|
|
/// <returns></returns>
|
|
public async Task<bool> SetAsync(string key, object value)
|
|
{
|
|
return await _database.StringSetAsync(key, JsonConvert.SerializeObject(value));
|
|
}
|
|
|
|
/// <summary>
|
|
/// 设置指定 key 的值,所有写入参数object都支持string | byte[] | 数值 | 对象
|
|
/// </summary>
|
|
/// <param name="key">键</param>
|
|
/// <param name="value">值</param>
|
|
/// <param name="expire">有效期</param>
|
|
/// <returns></returns>
|
|
public Task<bool> SetAsync(string key, object value, TimeSpan expire)
|
|
{
|
|
return _database.StringSetAsync(key, JsonConvert.SerializeObject(value), expire);
|
|
}
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
/// <param name="key"></param>
|
|
/// <returns></returns>
|
|
public bool Del(string key)
|
|
{
|
|
return _database.KeyDelete(key);
|
|
}
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
/// <param name="key"></param>
|
|
/// <returns></returns>
|
|
public Task<bool> DelAsync(string key)
|
|
{
|
|
return _database.KeyDeleteAsync(key);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 将 key 所储存的值加上给定的增量值(increment)
|
|
/// </summary>
|
|
/// <param name="key">键</param>
|
|
/// <returns></returns>
|
|
public async Task<long> IncrAsync(string key)
|
|
{
|
|
return await _database.StringIncrementAsync(key);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 将 key 所储存的值加上给定的增量值(increment)
|
|
/// </summary>
|
|
/// <param name="key">键</param>
|
|
/// <returns></returns>
|
|
public long Incr(string key)
|
|
{
|
|
return _database.StringIncrement(key);
|
|
}
|
|
|
|
#region Hash
|
|
/// <summary>
|
|
/// 将哈希表 key 中的字段 field 的值设为 value
|
|
/// </summary>
|
|
/// <param name="key">键</param>
|
|
/// <param name="filed">字段</param>
|
|
/// <param name="value">值</param>
|
|
/// <returns> 如果字段是哈希表中的一个新建字段,并且值设置成功,返回true。如果哈希表中域字段已经存在且旧值已被新值覆盖,返回false。</returns>
|
|
public bool HSet(string key, string filed, object value)
|
|
{
|
|
return _database.HashSet(key, filed, JsonConvert.SerializeObject(value));
|
|
}
|
|
|
|
/// <summary>
|
|
/// 将哈希表 key 中的字段 field 的值设为 value
|
|
/// </summary>
|
|
/// <param name="key">键</param>
|
|
/// <param name="filed">字段</param>
|
|
/// <param name="value">值</param>
|
|
/// <returns> 如果字段是哈希表中的一个新建字段,并且值设置成功,返回true。如果哈希表中域字段已经存在且旧值已被新值覆盖,返回false。</returns>
|
|
public Task<bool> HSetAsync(string key, string filed, object value)
|
|
{
|
|
return _database.HashSetAsync(key, filed, JsonConvert.SerializeObject(value));
|
|
}
|
|
|
|
/// <summary>
|
|
/// 同时将多个field-value对设置到哈希表key中
|
|
/// </summary>
|
|
/// <param name="key"></param>
|
|
/// <param name="value"></param>
|
|
/// <returns></returns>
|
|
//public Task<bool> HMSetAsync(string key, object[] value)
|
|
//{
|
|
// return _database.HMSetAsync(key, value);
|
|
//}
|
|
|
|
/// <summary>
|
|
/// 获取在哈希表中指定 key 的所有字段和值
|
|
/// </summary>
|
|
/// <typeparam name="T"></typeparam>
|
|
/// <param name="key"></param>
|
|
/// <returns></returns>
|
|
public async Task<IDictionary<string, object>> HGetAllAsync(string key)
|
|
{
|
|
IDictionary<string, object> keyValues = new Dictionary<string, object>();
|
|
var res = await _database.HashGetAllAsync(key);
|
|
foreach (var item in res)
|
|
{
|
|
keyValues.Add(item.Name, item.Value);
|
|
}
|
|
return keyValues;
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region Redis 有序集合(sorted set)
|
|
/// <summary>
|
|
/// 向有序集合添加一个或多个成员,或者更新已存在成员的分数
|
|
/// </summary>
|
|
/// <param name="key"></param>
|
|
/// <param name="member"></param>
|
|
/// <param name="score"></param>
|
|
/// <returns></returns>
|
|
public async Task<bool> ZAddAsync(string key, string member, double score)
|
|
{
|
|
return await _database.SortedSetAddAsync(key, member, score);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 用于计算集合中元素的数量
|
|
/// </summary>
|
|
/// <param name="key"></param>
|
|
/// <returns></returns>
|
|
public async Task<long> ZCardAsync(string key)
|
|
{
|
|
return await _database.SortedSetLengthAsync(key);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 有序集合中对指定成员的分数加上增量 increment
|
|
/// </summary>
|
|
/// <param name="key"></param>
|
|
/// <param name="member"></param>
|
|
/// <param name="increment"></param>
|
|
/// <returns></returns>
|
|
public async Task<double> ZIncrByAsync(string key, string member, double increment = 1)
|
|
{
|
|
return await _database.SortedSetIncrementAsync(key, member, increment);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 通过索引区间返回有序集合成指定区间内的成员
|
|
/// </summary>
|
|
/// <param name="key"></param>
|
|
/// <param name="start"></param>
|
|
/// <param name="stop"></param>
|
|
/// <param name="sortType">true倒序排列,false正序排列</param>
|
|
/// <returns></returns>
|
|
public async Task<List<string>> ZRemRangeByRankAsync(string key, long start, long stop, bool sortType = true)
|
|
{
|
|
if (sortType)
|
|
{
|
|
return (await _database.SortedSetRangeByRankAsync(key, start, stop, Order.Descending)).ToStringArray().ToList();
|
|
}
|
|
else
|
|
{
|
|
return (await _database.SortedSetRangeByRankAsync(key, start, stop, Order.Ascending)).ToStringArray().ToList();
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 返回有序集中的成员和分数,通过索引,分数从高到低
|
|
/// </summary>
|
|
/// <param name="key"></param>
|
|
/// <returns></returns>
|
|
public async Task<IDictionary<string, double>> ZRevRangeWithScoresAsync(string key)
|
|
{
|
|
return ConvertSortedSetEntryToDictionary(await _database.SortedSetRangeByScoreWithScoresAsync(key));
|
|
}
|
|
|
|
/// <summary>
|
|
/// 删除
|
|
/// </summary>
|
|
/// <param name="key"></param>
|
|
/// <param name="member"></param>
|
|
/// <returns></returns>
|
|
public async Task<bool> SortedSetRemoveAsync(string key, string member)
|
|
{
|
|
return await _database.SortedSetRemoveAsync(key, member);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 数据转换
|
|
/// </summary>
|
|
/// <param name="keyValues"></param>
|
|
/// <returns></returns>
|
|
public SortedSetEntry[] ConvertDictionaryToSortedSetEntry(IDictionary<string, double> keyValues)
|
|
{
|
|
List<SortedSetEntry> setEntries = new List<SortedSetEntry>();
|
|
foreach (var item in keyValues)
|
|
{
|
|
setEntries.Add(new SortedSetEntry(item.Key, item.Value));
|
|
}
|
|
return setEntries.ToArray();
|
|
}
|
|
|
|
/// <summary>
|
|
/// 数据转换字典
|
|
/// </summary>
|
|
/// <param name="keyValues"></param>
|
|
/// <returns></returns>
|
|
public IDictionary<string, double> ConvertSortedSetEntryToDictionary(SortedSetEntry[] setEntries)
|
|
{
|
|
IDictionary<string, double> keyValues = new Dictionary<string, double>();
|
|
foreach (var item in setEntries)
|
|
{
|
|
keyValues.Add(item.Element, item.Score);
|
|
}
|
|
return keyValues;
|
|
}
|
|
|
|
#endregion
|
|
}
|
|
}
|
|
|