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.
 
 

212 lines
7.4 KiB

using StackExchange.Redis;
namespace Znyc.CloudCar.Caching
{
/// <summary>
/// 缓存接口
/// </summary>
public interface IRedisOperationRepository
{
/// <summary>
/// 检查给定 key 是否存在
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
bool Exists(string key);
/// <summary>
/// 检查给定 key 是否存在
/// </summary>
/// <param name="key">键</param>
/// <returns></returns>
Task<bool> ExistsAsync(string key);
/// <summary>
/// 获取指定 key 的值
/// </summary>
/// <param name="key">键</param>
/// <returns></returns>
string Get(string key);
/// <summary>
/// 获取指定 key 的值
/// </summary>
/// <typeparam name="T">byte[] 或其他类型</typeparam>
/// <param name="key">键</param>
/// <returns></returns>
T Get<T>(string key);
/// <summary>
/// 获取指定 key 的值
/// </summary>
/// <param name="key">键</param>
/// <returns></returns>
Task<string> GetAsync(string key);
/// <summary>
/// 获取指定 key 的值
/// </summary>
/// <typeparam name="T">byte[] 或其他类型</typeparam>
/// <param name="key">键</param>
/// <returns></returns>
Task<T> GetAsync<T>(string key);
/// <summary>
/// 设置指定 key 的值,所有写入参数object都支持string | byte[] | 数值 | 对象
/// </summary>
/// <param name="key">键</param>
/// <param name="value">值</param>
/// <returns></returns>
bool Set(string key, object value);
/// <summary>
/// 设置指定 key 的值,所有写入参数object都支持string | byte[] | 数值 | 对象
/// </summary>
/// <param name="key">键</param>
/// <param name="value">值</param>
/// <param name="expire">有效期</param>
/// <returns></returns>
bool Set(string key, object value, TimeSpan expire);
/// <summary>
/// 设置指定 key 的值,所有写入参数object都支持string | byte[] | 数值 | 对象
/// </summary>
/// <param name="key">键</param>
/// <param name="value">值</param>
/// <returns></returns>
Task<bool> SetAsync(string key, object value);
/// <summary>
/// 设置指定 key 的值,所有写入参数object都支持string | byte[] | 数值 | 对象
/// </summary>
/// <param name="key">键</param>
/// <param name="value">值</param>
/// <param name="expire">有效期</param>
/// <returns></returns>
Task<bool> SetAsync(string key, object value, TimeSpan expire);
/// <summary>
///
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
bool Del(string key);
/// <summary>
///
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
Task<bool> DelAsync(string key);
/// <summary>
/// 将 key 所储存的值加上给定的增量值(increment)
/// </summary>
/// <param name="key">键</param>
/// <returns></returns>
Task<long> IncrAsync(string key);
/// <summary>
/// 将 key 所储存的值加上给定的增量值(increment)
/// </summary>
/// <param name="key">键</param>
/// <returns></returns>
long Incr(string key);
#region Hash
/// <summary>
/// 将哈希表 key 中的字段 field 的值设为 value
/// </summary>
/// <param name="key">键</param>
/// <param name="filed">字段</param>
/// <param name="value">值</param>
/// <returns> 如果字段是哈希表中的一个新建字段,并且值设置成功,返回true。如果哈希表中域字段已经存在且旧值已被新值覆盖,返回false。</returns>
bool HSet(string key, string filed, object value);
/// <summary>
/// 将哈希表 key 中的字段 field 的值设为 value
/// </summary>
/// <param name="key">键</param>
/// <param name="filed">字段</param>
/// <param name="value">值</param>
/// <returns> 如果字段是哈希表中的一个新建字段,并且值设置成功,返回true。如果哈希表中域字段已经存在且旧值已被新值覆盖,返回false。</returns>
Task<bool> HSetAsync(string key, string filed, object value);
/// <summary>
/// 获取在哈希表中指定 key 的所有字段和值
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="key"></param>
/// <returns></returns>
Task<IDictionary<string, object>> HGetAllAsync(string key);
#endregion
#region Redis 有序集合(sorted set)
/// <summary>
/// 向有序集合添加一个或多个成员,或者更新已存在成员的分数
/// </summary>
/// <param name="key"></param>
/// <param name="scoreMembers"></param>
/// <returns></returns>
Task<bool> ZAddAsync(string key, string member, double score);
/// <summary>
/// 用于计算集合中元素的数量
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
Task<long> ZCardAsync(string key);
/// <summary>
/// 有序集合中对指定成员的分数加上增量 increment
/// </summary>
/// <param name="key"></param>
/// <param name="member"></param>
/// <param name="increment"></param>
/// <returns></returns>
Task<double> ZIncrByAsync(string key, string member, double increment = 1);
/// <summary>
/// 通过索引区间返回有序集合成指定区间内的成员
/// </summary>
/// <param name="key"></param>
/// <param name="start"></param>
/// <param name="stop"></param>
/// <param name="sortType">true倒序排列,false正序排列</param>
/// <returns></returns>
Task<List<string>> ZRemRangeByRankAsync(string key, long start, long stop, bool sortType = true);
/// <summary>
/// 返回有序集中的成员和分数,通过索引,分数从高到低
/// </summary>
/// <param name="key"></param>
/// <param name="start"></param>
/// <param name="stop"></param>
/// <returns></returns>
Task<IDictionary<string, double>> ZRevRangeWithScoresAsync(string key);
/// <summary>
/// 删除
/// </summary>
/// <param name="key"></param>
/// <param name="member"></param>
/// <returns></returns>
Task<bool> SortedSetRemoveAsync(string key, string member);
/// <summary>
/// 数据转换
/// </summary>
/// <param name="keyValues"></param>
/// <returns></returns>
SortedSetEntry[] ConvertDictionaryToSortedSetEntry(IDictionary<string, double> keyValues);
/// <summary>
/// 数据转换字典
/// </summary>
/// <param name="keyValues"></param>
/// <returns></returns>
IDictionary<string, double> ConvertSortedSetEntryToDictionary(SortedSetEntry[] setEntries);
#endregion
}
}