• Redis使用记录


    1.安装及部署参考 https://www.cnblogs.com/888888CN/p/9519590.html

    2.使用Redis类库,我这里使用的是 StackExchange.Redis;

    3.标准使用Set/Get,自己封装一个标准使用的类库

      1 public class RedisService
      2     {
      3         private static string DefaultKey { get; set; }
      4         private ConnectionMultiplexer redis { get; set; }
      5         private IDatabase db { get; set; }
      6 
      7         public RedisService(string connection, string defaultKey)
      8         {
      9             DefaultKey = defaultKey;
     10             redis = ConnectionMultiplexer.Connect(connection);
     11             db = redis.GetDatabase();
     12         }
     13 
     14         /// <summary>
     15         /// 添加 key 的前缀
     16         /// </summary>
     17         /// <param name="key"></param>
     18         /// <returns></returns>
     19         private static string AddKeyPrefix(string key)
     20         {
     21             return $"{DefaultKey}:{key}";
     22         }
     23 
     24 
     25         /// <summary>
     26         /// 保存单个key value 
     27         /// </summary>
     28         /// <param name="key"></param>
     29         /// <param name="value"></param>
     30         /// <returns></returns>
     31         public bool Set(string key, string value)
     32         {
     33             key = AddKeyPrefix(key);
     34             return db.StringSet(key, value);
     35         }
     36 
     37         /// <summary>
     38         /// 保存单个key value,到时间过期
     39         /// </summary>
     40         /// <param name="key">Redis Key</param>
     41         /// <param name="value">保存的值</param>
     42         /// <param name="expiry">过期时间</param>
     43         /// <returns></returns>
     44         public bool Set(string key, string value, TimeSpan? expiry = default(TimeSpan?))
     45         {
     46             key = AddKeyPrefix(key);
     47             return db.StringSet(key, value, expiry);
     48         }
     49 
     50         /// <summary>
     51         /// 保存多个key-value
     52         /// </summary>
     53         /// <param name="keyValuePairs"></param>
     54         /// <returns></returns>
     55         public bool Set(IEnumerable<KeyValuePair<RedisKey, RedisValue>> keyValuePairs)
     56         {
     57             keyValuePairs =
     58                 keyValuePairs.Select(x => new KeyValuePair<RedisKey, RedisValue>(AddKeyPrefix(x.Key), x.Value));
     59             return db.StringSet(keyValuePairs.ToArray());
     60         }
     61 
     62         /// <summary>
     63         /// 获取一个key的value
     64         /// </summary>
     65         /// <typeparam name="T"></typeparam>
     66         /// <param name="key"></param>
     67         /// <returns></returns>
     68         public string Get(string key)
     69         {
     70             key = AddKeyPrefix(key);
     71             return db.StringGet(key);
     72         }
     73 
     74         /// <summary>
     75         /// 删除key
     76         /// </summary>
     77         /// <param name="key"></param>
     78         /// <returns></returns>
     79         public bool Delete(string key)
     80         {
     81             key = AddKeyPrefix(key);
     82             return db.KeyDelete(key);
     83         }
     84 
     85         /// <summary>
     86         /// 删除keys
     87         /// </summary>
     88         /// <param name="redisKeys"></param>
     89         /// <returns></returns>
     90         public long Delete(IEnumerable<string> keys)
     91         {
     92             var redisKeys = keys.Select(x => (RedisKey)AddKeyPrefix(x));
     93             return db.KeyDelete(redisKeys.ToArray());
     94         }
     95 
     96 
     97         /// <summary>
     98         /// 判断key是否存在
     99         /// </summary>
    100         /// <param name="key"></param>
    101         /// <returns></returns>
    102         public bool Exists(string key)
    103         {
    104             key = AddKeyPrefix(key);
    105             return db.KeyExists(key);
    106         }
    107 
    108         /// <summary>
    109         /// 重命名key
    110         /// </summary>
    111         /// <param name="oldKeyName"></param>
    112         /// <param name="newKeyName"></param>
    113         /// <returns></returns>
    114         public bool ReName(string oldKeyName, string newKeyName)
    115         {
    116             oldKeyName = AddKeyPrefix(oldKeyName);
    117             return db.KeyRename(oldKeyName, newKeyName);
    118         }
    119 
    120         /// <summary>
    121         /// 设置key 的过期时间
    122         /// </summary>
    123         /// <param name="redisKey"></param>
    124         /// <param name="expired"></param>
    125         /// <returns></returns>
    126         public bool Expire(string redisKey, TimeSpan? expired = null)
    127         {
    128             redisKey = AddKeyPrefix(redisKey);
    129             return db.KeyExpire(redisKey, expired);
    130         }
    131 
    132         /// <summary>
    133         /// 发布消息
    134         /// </summary>
    135         /// <param name="panel"></param>
    136         /// <param name="message"></param>
    137         public void Publish(string channel, string message)
    138         {
    139             ISubscriber sub = redis.GetSubscriber();
    140             sub.Publish(channel, message);
    141         }
    142 
    143 
    144         public void Subscribe(string panel, Action<string, string> handle)
    145         {
    146             ISubscriber sub = redis.GetSubscriber();
    147             sub.Subscribe(panel, (channel, message) =>
    148             {
    149                 handle(channel, message);
    150             });
    151         }
    152 
    153         /// <summary>
    154         /// 在列表头部插入值,如果键不存在,先创建再插入值
    155         /// </summary>
    156         /// <param name="redisKey">不区分项目</param>
    157         /// <param name="redisValue"></param>
    158         /// <returns></returns>
    159         public long ListLeftPush(string key, string value)
    160         {
    161             //var redisKey = AddKeyPrefix(key);
    162             return db.ListLeftPush(key, value);
    163         }
    164 
    165         /// <summary>
    166         /// 移除并返回key所对应列表的第一个元素
    167         /// </summary>
    168         /// <param name="redisKey">不区分项目</param>
    169         /// <returns></returns>
    170         public string ListLeftPop(string key, Action<string> handle)
    171         {
    172             //var redisKey = AddKeyPrefix(key);
    173             string value = db.ListLeftPop(key);
    174             handle(value);
    175             return value; 
    176         }
    177 
    178 
    179         public string ListRightPop(string key, Action<string> handle)
    180         {
    181             string value = db.ListRightPop(key);
    182             handle(value);
    183             return value;
    184 
    185         }
    186 
    187 
    188     }
    View Code

    测试结果:

  • 相关阅读:
    FPGrowth 实现
    数据库事务的4个特性ACID
    redis简介(keeper实时报表的基本部分)
    HADOOP中的CRC数据校验文件
    潜在语义分析
    AdaBoost 算法
    cocosBuilder生成cbbi文件,绑定到cocos2d-x
    C++继承模型
    Android应用经典主界面框架之中的一个:仿QQ (使用Fragment, 附源代码)
    从今天開始学习iOS开发(iOS 7版)--实现一款App之Foundation框架的使用
  • 原文地址:https://www.cnblogs.com/JoeyZJ/p/11719484.html
Copyright © 2020-2023  润新知