zoukankan      html  css  js  c++  java
  • 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

    测试结果:

  • 相关阅读:
    Tomcat+Nginx+Linux+Mysql部署豆瓣TOP250的项目到腾讯云服务器
    使用JSP+Servlet+Jdbc+Echatrs实现对豆瓣电影Top250的展示
    环境搭建-CentOS集群搭建
    环境搭建-Hadoop集群搭建
    ELK搭建实时日志分析平台
    Flume和Kafka完成实时数据的采集
    Python日志产生器
    腐竹木耳炒肉
    [转]Apple耳机兼容非Mac设置
    文件及文件夹操作
  • 原文地址:https://www.cnblogs.com/JoeyZJ/p/11719484.html
Copyright © 2011-2022 走看看