• Redis封装帮助类


      1 /// <summary>
      2 /// Redis操作
      3 /// </summary>
      4 public class RedisHelper
      5 {
      6 
      7     //static NewtonsoftSerializer serializer = new NewtonsoftSerializer();
      8     //static StackExchangeRedisCacheClient cacheClient = new StackExchangeRedisCacheClient(serializer);
      9     //private static IDatabase db = cacheClient.Database;
     10 
     11     private static string connstr = System.Configuration.ConfigurationManager.AppSettings["redisCacheClient"];// "127.0.0.1:6379,allowadmin=true";
     12     private static ConnectionMultiplexer conn = ConnectionMultiplexer.Connect(connstr);
     13     private static IDatabase db =  conn.GetDatabase(1);
     14 
     15 
     16     /// <summary>
     17     /// 获取系统的redis key前缀
     18     /// </summary>
     19     /// <param name="resourceid">资源Id</param>
     20     /// <returns></returns>
     21     public static string GetMyKey(string resourceid = "")
     22     {
     23        
     24         string Key = "report_";
     25         if (!string.IsNullOrWhiteSpace(resourceid))
     26         {
     27             Key = string.Format("report_res_{0}", resourceid);
     28         }
     29         return Key;
     30     }
     31 
     32     #region String 可以设置过期时间 同步
     33 
     34     /// <summary>
     35     /// 保存单个key value
     36     /// </summary>
     37     /// <param name="key">Redis Key</param>
     38     /// <param name="value">保存的值</param>
     39     /// <param name="expiry">过期时间</param>
     40     /// <returns></returns>
     41     public static bool SetStringKey(string key, string value, TimeSpan? expiry = default(TimeSpan?))
     42     {
     43        
     44         return db.StringSet(key, value, expiry);
     45     }
     46 
     47     /// <summary>
     48     /// 保存多个key value
     49     /// </summary>
     50     /// <param name="arr">key</param>
     51     /// <returns></returns>
     52     public static bool SetStringKey(KeyValuePair<RedisKey, RedisValue>[] arr)
     53     {
     54         return db.StringSet(arr);
     55     }
     56 
     57     /// <summary>
     58     /// 保存一个对象
     59     /// </summary>
     60     /// <typeparam name="T"></typeparam>
     61     /// <param name="key"></param>
     62     /// <param name="obj"></param>
     63     /// <returns></returns>
     64     public static bool SetStringKey<T>(string key, T obj, TimeSpan? expiry = default(TimeSpan?))
     65     {
     66         string json = JsonConvert.SerializeObject(obj);
     67         return db.StringSet(key, json, expiry);
     68     }
     69 
     70     /// <summary>
     71     /// 获取单个key的值
     72     /// </summary>
     73     /// <param name="key">Redis Key</param>
     74     /// <returns></returns>
     75 
     76     public static RedisValue GetStringKey(string key)
     77     {
     78         return db.StringGet(key);
     79     }
     80 
     81 
     82     /// <summary>
     83     /// 获取多个Key
     84     /// </summary>
     85     /// <param name="listKey">Redis Key集合</param>
     86     /// <returns></returns>
     87     public static RedisValue[] GetStringKey(List<RedisKey> listKey)
     88     {
     89         return db.StringGet(listKey.ToArray());
     90     }
     91 
     92     /// <summary>
     93     /// 获取一个key的对象
     94     /// </summary>
     95     /// <typeparam name="T"></typeparam>
     96     /// <param name="key"></param>
     97     /// <returns></returns>
     98     public static T GetStringKey<T>(string key)
     99     {
    100         return JsonConvert.DeserializeObject<T>(db.StringGet(key));
    101     }
    102     /// <summary>
    103     /// 为数字增长val
    104     /// </summary>
    105     /// <param name="key"></param>
    106     /// <param name="val">可以为负</param>
    107     /// <returns>增长后的值</returns>
    108     public double StringIncrement(string key, double val = 1)
    109     {
    110         return db.StringIncrement(key, val);
    111     }
    112 
    113     /// <summary>
    114     /// 为数字减少val
    115     /// </summary>
    116     /// <param name="key"></param>
    117     /// <param name="val">可以为负</param>
    118     /// <returns>减少后的值</returns>
    119     public double StringDecrement(string key, double val = 1)
    120     {
    121         return db.StringDecrement(key, val);
    122     }
    123 
    124     #endregion
    125 
    126     #region String 可以设置过期时间 异步
    127 
    128     /// <summary>
    129     /// 保存单个key value
    130     /// </summary>
    131     /// <param name="key">Redis Key</param>
    132     /// <param name="value">保存的值</param>
    133     /// <param name="expiry">过期时间</param>
    134     /// <returns></returns>
    135     public static async Task<bool> SetStringKeyAsync(string key, string value, TimeSpan? expiry = default(TimeSpan?))
    136     {
    137 
    138         return await db.StringSetAsync(key, value, expiry);
    139     }
    140 
    141     /// <summary>
    142     /// 保存多个key value
    143     /// </summary>
    144     /// <param name="arr">key</param>
    145     /// <returns></returns>
    146     public static async Task<bool> SetStringKeyAsync(KeyValuePair<RedisKey, RedisValue>[] arr)
    147     {
    148         return await db.StringSetAsync(arr);
    149     }
    150 
    151     /// <summary>
    152     /// 保存一个对象
    153     /// </summary>
    154     /// <typeparam name="T"></typeparam>
    155     /// <param name="key"></param>
    156     /// <param name="obj"></param>
    157     /// <returns></returns>
    158     public static async Task<bool> SetStringKeyAsync<T>(string key, T obj, TimeSpan? expiry = default(TimeSpan?))
    159     {
    160         string json = JsonConvert.SerializeObject(obj);
    161         return await db.StringSetAsync(key, json, expiry);
    162     }
    163 
    164     /// <summary>
    165     /// 获取单个key的值
    166     /// </summary>
    167     /// <param name="key">Redis Key</param>
    168     /// <returns></returns>
    169 
    170     public static async Task<RedisValue>  GetStringKeyAsync(string key)
    171     {
    172         return await db.StringGetAsync(key);
    173     }
    174 
    175 
    176     /// <summary>
    177     /// 获取多个Key
    178     /// </summary>
    179     /// <param name="listKey">Redis Key集合</param>
    180     /// <returns></returns>
    181     public static async Task<RedisValue[]> GetStringKeyAsync(List<RedisKey> listKey)
    182     {
    183         return await db.StringGetAsync(listKey.ToArray());
    184     }
    185 
    186     /// <summary>
    187     /// 获取一个key的对象
    188     /// </summary>
    189     /// <typeparam name="T"></typeparam>
    190     /// <param name="key"></param>
    191     /// <returns></returns>
    192     public static async Task<T> GetStringKeyAsync<T>(string key)
    193     {
    194         return JsonConvert.DeserializeObject<T>(await db.StringGetAsync(key));
    195     }
    196     /// <summary>
    197     /// 为数字增长val
    198     /// </summary>
    199     /// <param name="key"></param>
    200     /// <param name="val">可以为负</param>
    201     /// <returns>增长后的值</returns>
    202     public async Task<double> StringIncrementAsync(string key, double val = 1)
    203     {
    204         return await db.StringIncrementAsync(key, val);
    205     }
    206 
    207     /// <summary>
    208     /// 为数字减少val
    209     /// </summary>
    210     /// <param name="key"></param>
    211     /// <param name="val">可以为负</param>
    212     /// <returns>减少后的值</returns>
    213     public async Task<double> StringDecrementAsync(string key, double val = 1)
    214     {
    215         return await db.StringDecrementAsync(key, val);
    216     }
    217     #endregion
    218 
    219     #region Hash 同步
    220     /// <summary>
    221     /// 存储数据到hash表
    222     /// </summary>
    223     /// <typeparam name="T"></typeparam>
    224     /// <param name="key"></param>
    225     /// <param name="dataKey"></param>
    226     /// <param name="t"></param>
    227     /// <returns></returns>
    228     public static bool HashSet1<T>(string key, string dataKey, List<T> t)
    229     {
    230         string json = "";
    231         foreach (var item in t)
    232         {
    233             json = JsonConvert.SerializeObject(item);
    234             //listHashEntry.Add(new HashEntry(getModelId(item), json));
    235         }
    236         return db.HashSet(key, dataKey, json);
    237 
    238         //return await  ({
    239         //    string json = ConvertJson(t);
    240         //    return db.HashSetAsync(key, dataKey, json);
    241         //});
    242     }
    243     /// <summary>
    244     /// 保存一个集合
    245     /// </summary>
    246     /// <typeparam name="T"></typeparam>
    247     /// <param name="key">Redis Key</param>
    248     /// <param name="list">数据集合</param>
    249     /// <param name="getModelId"></param>
    250     public static void HashSet<T>(string key, List<T> list, Func<T, string> getModelId)
    251     {
    252         List<HashEntry> listHashEntry = new List<HashEntry>();
    253         foreach (var item in list)
    254         {
    255             string json = JsonConvert.SerializeObject(item);
    256             listHashEntry.Add(new HashEntry(getModelId(item), json));
    257         }
    258         db.HashSet(key, listHashEntry.ToArray());
    259     }
    260 
    261     /// <summary>
    262     /// 获取Hash中的单个key的值
    263     /// </summary>
    264     /// <typeparam name="T"></typeparam>
    265     /// <param name="key">Redis Key</param>
    266     /// <param name="hasFildValue">RedisValue</param>
    267     /// <returns></returns>
    268     public static T GetHashKey<T>(string key, string hasFildValue)
    269     {
    270         if (!string.IsNullOrWhiteSpace(key) && !string.IsNullOrWhiteSpace(hasFildValue))
    271         {
    272             RedisValue value = db.HashGet(key, hasFildValue);
    273             if (!value.IsNullOrEmpty)
    274             {
    275                 return JsonConvert.DeserializeObject<T>(value);
    276             }
    277         }
    278         return default(T);
    279     }
    280 
    281     /// <summary>
    282     /// 获取hash中的多个key的值
    283     /// </summary>
    284     /// <typeparam name="T"></typeparam>
    285     /// <param name="key">Redis Key</param>
    286     /// <param name="listhashFields">RedisValue value</param>
    287     /// <returns></returns>
    288     public static List<T> GetHashKey<T>(string key, List<RedisValue> listhashFields)
    289     {
    290         List<T> result = new List<T>();
    291         if (!string.IsNullOrWhiteSpace(key) && listhashFields.Count > 0)
    292         {
    293             RedisValue[] value = db.HashGet(key, listhashFields.ToArray());
    294             foreach (var item in value)
    295             {
    296                 if (!item.IsNullOrEmpty)
    297                 {
    298                     result.Add(JsonConvert.DeserializeObject<T>(item));
    299                 }
    300             }
    301         }
    302         return result;
    303     }
    304 
    305     /// <summary>
    306     /// 获取hashkey所有Redis key
    307     /// </summary>
    308     /// <typeparam name="T"></typeparam>
    309     /// <param name="key"></param>
    310     /// <returns></returns>
    311     public static List<T> GetHashAll<T>(string key)
    312     {
    313         List<T> result = new List<T>();
    314         RedisValue[] arr = db.HashKeys(key);
    315         foreach (var item in arr)
    316         {
    317             if (!item.IsNullOrEmpty)
    318             {
    319                 result.Add(JsonConvert.DeserializeObject<T>(item));
    320             }
    321         }
    322         return result;
    323     }
    324 
    325     /// <summary>
    326     /// 获取hashkey所有的值
    327     /// </summary>
    328     /// <typeparam name="T"></typeparam>
    329     /// <param name="key"></param>
    330     /// <returns></returns>
    331     public static List<T> HashGetAll<T>(string key)
    332     {
    333         List<T> result = new List<T>();
    334         HashEntry[] arr = db.HashGetAll(key);
    335         foreach (var item in arr)
    336         {
    337             if (!item.Value.IsNullOrEmpty)
    338             {
    339                 result.Add(JsonConvert.DeserializeObject<T>(item.Value));
    340             }
    341         }
    342         return result;
    343     }
    344 
    345     /// <summary>
    346     /// 删除hasekey
    347     /// </summary>
    348     /// <param name="key"></param>
    349     /// <param name="hashField"></param>
    350     /// <returns></returns>
    351     public static bool DeleteHase(RedisKey key, RedisValue hashField)
    352     {
    353         return db.HashDelete(key, hashField);
    354     }
    355 
    356     #endregion
    357 
    358     #region Hash 异步
    359     /// <summary>
    360     /// 存储数据到hash表
    361     /// </summary>
    362     /// <typeparam name="T"></typeparam>
    363     /// <param name="key"></param>
    364     /// <param name="dataKey"></param>
    365     /// <param name="t"></param>
    366     /// <returns></returns>
    367     public static async Task<bool> HashSetAsync<T>(string key, string dataKey, List<T> t)
    368     {
    369         string json = "";
    370         foreach (var item in t)
    371         {
    372             json = JsonConvert.SerializeObject(item);
    373             //listHashEntry.Add(new HashEntry(getModelId(item), json));
    374         }
    375         return await db.HashSetAsync(key, dataKey, json);
    376 
    377         //return await  ({
    378         //    string json = ConvertJson(t);
    379         //    return db.HashSetAsync(key, dataKey, json);
    380         //});
    381     }
    382 
    383     ///// <summary>
    384     ///// 保存一个集合
    385     ///// </summary>
    386     ///// <typeparam name="T"></typeparam>
    387     ///// <param name="key">Redis Key</param>
    388     ///// <param name="list">数据集合</param>
    389     ///// <param name="getModelId"></param>
    390     //public static async Task<bool> HashSetAsync<T>(string key, List<T> list, Func<T, string> getModelId)
    391     //{
    392     //    List<HashEntry> listHashEntry = new List<HashEntry>();
    393     //    string json = "";
    394     //    foreach (var item in list)
    395     //    {
    396     //        json = JsonConvert.SerializeObject(item);
    397     //        listHashEntry.Add(new HashEntry(getModelId(item), json));
    398     //    }
    399     //   return await db.HashSetAsync(key, listHashEntry.ToArray());
    400     //}
    401 
    402     /// <summary>
    403     /// 获取Hash中的单个key的值
    404     /// </summary>
    405     /// <typeparam name="T"></typeparam>
    406     /// <param name="key">Redis Key</param>
    407     /// <param name="hasFildValue">RedisValue</param>
    408     /// <returns></returns>
    409     public static async Task<T> GetHashKeyAsync<T>(string key, string hasFildValue)
    410     {
    411         if (!string.IsNullOrWhiteSpace(key) && !string.IsNullOrWhiteSpace(hasFildValue))
    412         {
    413             RedisValue value = await db.HashGetAsync(key, hasFildValue);
    414             if (!value.IsNullOrEmpty)
    415             {
    416                 return JsonConvert.DeserializeObject<T>(value);
    417             }
    418         }
    419         return default(T);
    420     }
    421 
    422     /// <summary>
    423     /// 获取hash中的多个key的值
    424     /// </summary>
    425     /// <typeparam name="T"></typeparam>
    426     /// <param name="key">Redis Key</param>
    427     /// <param name="listhashFields">RedisValue value</param>
    428     /// <returns></returns>
    429     public static async Task<List<T>> GetHashKeyAsync<T>(string key, List<RedisValue> listhashFields)
    430     {
    431         List<T> result = new List<T>();
    432         if (!string.IsNullOrWhiteSpace(key) && listhashFields.Count > 0)
    433         {
    434             RedisValue[] value = await db.HashGetAsync(key, listhashFields.ToArray());
    435             foreach (var item in value)
    436             {
    437                 if (!item.IsNullOrEmpty)
    438                 {
    439                     result.Add(JsonConvert.DeserializeObject<T>(item));
    440                 }
    441             }
    442         }
    443         return result;
    444     }
    445 
    446     /// <summary>
    447     /// 获取hashkey所有Redis key
    448     /// </summary>
    449     /// <typeparam name="T"></typeparam>
    450     /// <param name="key"></param>
    451     /// <returns></returns>
    452     public static async Task<List<T>> GetHashAllAsync<T>(string key)
    453     {
    454         List<T> result = new List<T>();
    455         RedisValue[] arr =await db.HashKeysAsync(key);
    456         foreach (var item in arr)
    457         {
    458             if (!item.IsNullOrEmpty)
    459             {
    460                 result.Add(JsonConvert.DeserializeObject<T>(item));
    461             }
    462         }
    463         return result;
    464     }
    465 
    466     /// <summary>
    467     /// 获取hashkey所有的值
    468     /// </summary>
    469     /// <typeparam name="T"></typeparam>
    470     /// <param name="key"></param>
    471     /// <returns></returns>
    472     public static async Task<List<T>> HashGetAllAsync<T>(string key)
    473     {
    474         List<T> result = new List<T>();
    475         HashEntry[] arr =await db.HashGetAllAsync(key);
    476         foreach (var item in arr)
    477         {
    478             if (!item.Value.IsNullOrEmpty)
    479             {
    480                 result.Add(JsonConvert.DeserializeObject<T>(item.Value));
    481             }
    482         }
    483         return result;
    484     }
    485 
    486     /// <summary>
    487     /// 删除hasekey
    488     /// </summary>
    489     /// <param name="key"></param>
    490     /// <param name="hashField"></param>
    491     /// <returns></returns>
    492     public static async Task<bool> DeleteHaseAsync(RedisKey key, RedisValue hashField)
    493     {
    494         return await db.HashDeleteAsync(key, hashField);
    495     }
    496 
    497     #endregion
    498 
    499     #region key 同步
    500 
    501     /// <summary>
    502     /// 删除单个key
    503     /// </summary>
    504     /// <param name="key">redis key</param>
    505     /// <returns>是否删除成功</returns>
    506     public static bool KeyDelete(string key)
    507     {
    508         return db.KeyDelete(key);
    509     }
    510 
    511     /// <summary>
    512     /// 删除多个key
    513     /// </summary>
    514     /// <param name="keys">rediskey</param>
    515     /// <returns>成功删除的个数</returns>
    516     public static long keyDelete(RedisKey[] keys)
    517     {
    518         return db.KeyDelete(keys);
    519     }
    520 
    521     /// <summary>
    522     /// 判断key是否存储
    523     /// </summary>
    524     /// <param name="key">redis key</param>
    525     /// <returns></returns>
    526     public static bool KeyExists(string key)
    527     {
    528         return db.KeyExists(key);
    529     }
    530 
    531     /// <summary>
    532     /// 重新命名key
    533     /// </summary>
    534     /// <param name="key">就的redis key</param>
    535     /// <param name="newKey">新的redis key</param>
    536     /// <returns></returns>
    537     public static bool KeyRename(string key, string newKey)
    538     {
    539         return db.KeyRename(key, newKey);
    540     }
    541 
    542     /// <summary>
    543     /// 设置Key的时间
    544     /// </summary>
    545     /// <param name="key">redis key</param>
    546     /// <param name="expiry"></param>
    547     /// <returns></returns>
    548     public static bool KeyExpire(string key, TimeSpan? expiry = default(TimeSpan?))
    549     {
    550         return db.KeyExpire(key, expiry);
    551     }
    552     #endregion
    553 
    554     #region key 异步
    555 
    556     /// <summary>
    557     /// 删除单个key
    558     /// </summary>
    559     /// <param name="key">redis key</param>
    560     /// <returns>是否删除成功</returns>
    561     public static async Task<bool> KeyDeleteAsync(string key)
    562     {
    563         return await db.KeyDeleteAsync(key);
    564     }
    565 
    566     /// <summary>
    567     /// 删除多个key
    568     /// </summary>
    569     /// <param name="keys">rediskey</param>
    570     /// <returns>成功删除的个数</returns>
    571     public static async Task<long> keyDeleteAsync(RedisKey[] keys)
    572     {
    573         return await db.KeyDeleteAsync(keys);
    574     }
    575 
    576     /// <summary>
    577     /// 判断key是否存储
    578     /// </summary>
    579     /// <param name="key">redis key</param>
    580     /// <returns></returns>
    581     public static async Task<bool> KeyExistsAsync(string key)
    582     {
    583         return await db.KeyExistsAsync(key);
    584     }
    585 
    586     /// <summary>
    587     /// 重新命名key
    588     /// </summary>
    589     /// <param name="key">就的redis key</param>
    590     /// <param name="newKey">新的redis key</param>
    591     /// <returns></returns>
    592     public static async Task<bool> KeyRenameAsync(string key, string newKey)
    593     {
    594         return await db.KeyRenameAsync(key, newKey);
    595     }
    596 
    597 
    598     /// <summary>
    599     /// 设置Key的时间
    600     /// </summary>
    601     /// <param name="key">redis key</param>
    602     /// <param name="expiry"></param>
    603     /// <returns></returns>
    604     public static async Task<bool> KeyExpireAsync(string key, TimeSpan? expiry = default(TimeSpan?))
    605     {
    606         return await db.KeyExpireAsync(key, expiry);
    607     }
    608     #endregion
    609 
    610 
    611     #region List 同步
    612 
    613     /// <summary>
    614     /// 移除指定ListId的内部List的值
    615     /// </summary>
    616     /// <param name="key"></param>
    617     /// <param name="value"></param>
    618     public void ListRemove<T>(string key, T value)
    619     {
    620          db.ListRemove(key, JsonConvert.SerializeObject(value));
    621     }
    622 
    623     /// <summary>
    624     /// 获取指定key的List
    625     /// </summary>
    626     /// <param name="key"></param>
    627     /// <returns></returns>
    628     public List<T> ListRange<T>(string key)
    629     {
    630         var values = db.ListRange(key);
    631         List<T> result = new List<T>();
    632         foreach (var item in values)
    633         {
    634             var model = JsonConvert.DeserializeObject<T>(item);
    635             result.Add(model);
    636         }
    637         return result;
    638     }
    639 
    640     /// <summary>
    641     /// 入队
    642     /// </summary>
    643     /// <param name="key"></param>
    644     /// <param name="value"></param>
    645     public void ListRightPush<T>(string key, T value)
    646     {
    647         db.ListRightPush(key, JsonConvert.SerializeObject(value));
    648     }
    649 
    650     /// <summary>
    651     /// 出队
    652     /// </summary>
    653     /// <typeparam name="T"></typeparam>
    654     /// <param name="key"></param>
    655     /// <returns></returns>
    656     public T ListRightPop<T>(string key)
    657     {
    658         var value = db.ListRightPop(key);
    659         return JsonConvert.DeserializeObject<T>(value);
    660     }
    661 
    662     /// <summary>
    663     /// 获取集合中的数量
    664     /// </summary>
    665     /// <param name="key"></param>
    666     /// <returns></returns>
    667     public long ListLength(string key)
    668     {
    669         return db.ListLength(key);
    670     }
    671 
    672     #endregion 同步方法
    673 
    674     #region List 异步
    675 
    676     /// <summary>
    677     /// 移除指定ListId的内部List的值
    678     /// </summary>
    679     /// <param name="key"></param>
    680     /// <param name="value"></param>
    681     public async Task<long> ListRemoveAsync<T>(string key, T value)
    682     {
    683         return await db.ListRemoveAsync(key, JsonConvert.SerializeObject(value));
    684     }
    685 
    686     /// <summary>
    687     /// 获取指定key的List
    688     /// </summary>
    689     /// <param name="key"></param>
    690     /// <returns></returns>
    691     public async Task<List<T>> ListRangeAsync<T>(string key)
    692     {
    693         var values =await db.ListRangeAsync(key);
    694         List<T> result = new List<T>();
    695         foreach (var item in values)
    696         {
    697             var model = JsonConvert.DeserializeObject<T>(item);
    698             result.Add(model);
    699         }
    700         return result;
    701     }
    702 
    703     /// <summary>
    704     /// 入队
    705     /// </summary>
    706     /// <param name="key"></param>
    707     /// <param name="value"></param>
    708     public async Task<long> ListRightPushAsync<T>(string key, T value)
    709     {
    710         return await db.ListRightPushAsync(key, JsonConvert.SerializeObject(value));
    711     }
    712 
    713     /// <summary>
    714     /// 出队
    715     /// </summary>
    716     /// <typeparam name="T"></typeparam>
    717     /// <param name="key"></param>
    718     /// <returns></returns>
    719     public async Task<T> ListRightPopAsync<T>(string key)
    720     {
    721         var value = await db.ListRightPopAsync(key);
    722         return JsonConvert.DeserializeObject<T>(value);
    723     }
    724 
    725    
    726     /// <summary>
    727     /// 获取集合中的数量
    728     /// </summary>
    729     /// <param name="key"></param>
    730     /// <returns></returns>
    731     public async Task<long> ListLengthAsync(string key)
    732     {
    733         return await db.ListLengthAsync(key);
    734     }
    735 
    736     #endregion 异步方法
    737     
    738 
    739     #region SortedSet 同步
    740 
    741     /// <summary>
    742     /// 添加
    743     /// </summary>
    744     /// <param name="key"></param>
    745     /// <param name="value"></param>
    746     /// <param name="score"></param>
    747     public static bool SortedSetAdd<T>(string key, T value, double score)
    748     {
    749         return db.SortedSetAdd(key, JsonConvert.SerializeObject(value), score);
    750     }
    751 
    752     /// <summary>
    753     /// 删除
    754     /// </summary>
    755     /// <param name="key"></param>
    756     /// <param name="value"></param>
    757     public static bool SortedSetRemove<T>(string key, T value)
    758     {
    759         return db.SortedSetRemove(key, JsonConvert.SerializeObject(value));
    760     }
    761 
    762     /// <summary>
    763     /// 获取全部
    764     /// </summary>
    765     /// <param name="key"></param>
    766     /// <returns></returns>
    767     public static List<T> SortedSetRangeByRank<T>(string key)
    768     {
    769             var values = db.SortedSetRangeByRank(key);
    770 
    771         List<T> result = new List<T>();
    772         foreach (var item in values)
    773         {
    774             var model = JsonConvert.DeserializeObject<T>(item);
    775             result.Add(model);
    776         }
    777         return result;
    778     }
    779 
    780     /// <summary>
    781     /// 获取集合中的数量
    782     /// </summary>
    783     /// <param name="key"></param>
    784     /// <returns></returns>
    785     public static long SortedSetLength(string key)
    786     {
    787         return db.SortedSetLength(key);
    788     }
    789 
    790     #endregion 同步方法
    791 
    792     #region SortedSet 异步
    793 
    794     /// <summary>
    795     /// 添加
    796     /// </summary>
    797     /// <param name="key"></param>
    798     /// <param name="value"></param>
    799     /// <param name="score"></param>
    800     public static async Task<bool> SortedSetAddAsync<T>(string key, T value, double score)
    801     {
    802         return await db.SortedSetAddAsync(key, JsonConvert.SerializeObject(value), score);
    803     }
    804 
    805     /// <summary>
    806     /// 删除
    807     /// </summary>
    808     /// <param name="key"></param>
    809     /// <param name="value"></param>
    810     public static async Task<bool> SortedSetRemoveAsync<T>(string key, T value)
    811     {
    812         return await db.SortedSetRemoveAsync(key, JsonConvert.SerializeObject(value));
    813     }
    814 
    815     /// <summary>
    816     /// 获取全部
    817     /// </summary>
    818     /// <param name="key"></param>
    819     /// <returns></returns>
    820     public static async Task<List<T>> SortedSetRangeByRankAsync<T>(string key)
    821     {
    822         var values = await db.SortedSetRangeByRankAsync(key);
    823         List<T> result = new List<T>();
    824         foreach (var item in values)
    825         {
    826             var model = JsonConvert.DeserializeObject<T>(item);
    827             result.Add(model);
    828         }
    829         return result;
    830     }
    831 
    832     /// <summary>
    833     /// 获取集合中的数量
    834     /// </summary>
    835     /// <param name="key"></param>
    836     /// <returns></returns>
    837     public static async Task<long> SortedSetLengthAsync(string key)
    838     {
    839         return await db.SortedSetLengthAsync(key);
    840     }
    841 
    842     #endregion 异步方法
    843     
    844 
    845     /// <summary>
    846     /// 追加值
    847     /// </summary>
    848     /// <param name="key"></param>
    849     /// <param name="value"></param>
    850     public static void StringAppend(string key, string value)
    851     {
    852         ////追加值,返回追加后长度
    853         long appendlong = db.StringAppend(key, value);
    854     }
    855 
    856 }
  • 相关阅读:
    [内核编程] 串口过滤
    大数问题(高精度运算)
    [内核编程] 实战调试,第一个内核程序
    [内核编程] Windebug双机调试环境搭建
    树状数组
    mysql常用语句
    解决maven工程无法创建src/main/java包名的方法
    java Date中方法toLocaleString过时的替代方案
    Eclipse 无法编译,提示“错误: 找不到或无法加载主类”
    Eclipse如何设置编译文件.class输出路径
  • 原文地址:https://www.cnblogs.com/anyihen/p/12830627.html
Copyright © 2020-2023  润新知