项目里的Redis 工具类,写下来以备后用

  1. public class RedisConnector
  2. {
  3. public class RedisParseResult<T>
  4. {
  5. public bool success;
  6. public T value;
  7. }
  8. private static string ConnectionString { get; set; }
  9. private static ConnectionMultiplexer RedisConnection { get; set; }
  10.  
  11. public RedisConnector(string redisConnectionString)
  12. {
  13. ConnectionString = redisConnectionString;
  14. try
  15. {
  16. RedisConnection = ConnectionMultiplexer.Connect(ConnectionString);
  17. }
  18. catch (RedisConnectionException e)
  19. {
  20. AILogger.LogError(e.Message, $"Redis server : {redisConnectionString}").Wait();
  21. }
  22. }
  23.  
  24. public static void Initialize(string redisConnectionString)
  25. {
  26. ConnectionString = redisConnectionString;
  27. try
  28. {
  29. RedisConnection = ConnectionMultiplexer.Connect(ConnectionString);
  30. }
  31. catch (RedisConnectionException e)
  32. {
  33. AILogger.LogError(e.Message, $"Redis server : {redisConnectionString}").Wait();
  34. }
  35. }
  36. public static IDatabase RedisStore => (null != RedisConnection) ? RedisConnection.GetDatabase() : ConnectionMultiplexer.Connect(ConnectionString).GetDatabase();
  37. public static ConnectionMultiplexer Redis => RedisConnection ?? ConnectionMultiplexer.Connect(ConnectionString);
  38.  
  39. , bool replace = false) => await RedisStore.StringSetAsync(key, value, TimeSpan.FromSeconds(expiry), replace ? When.Always : When.NotExists);
  40. , bool replace = false) => await RedisStore.StringSetAsync(key, value.ToString(), TimeSpan.FromSeconds(expiry), replace ? When.Always : When.NotExists);
  41. , bool replace = false) => await RedisStore.StringSetAsync(key, JsonConvert.SerializeObject(value), TimeSpan.FromSeconds(expiry), replace ? When.Always : When.NotExists);
  42.  
  43. public static string Get(string key) => RedisStore.StringGet(key);
  44. public static RedisParseResult<T> Get<T>(string key)
  45. {
  46. var storedValue = RedisStore.StringGet(key);
  47. if (string.IsNullOrEmpty(storedValue))
  48. return new RedisParseResult<T> { success = false };
  49. else
  50. return new RedisParseResult<T> { success = true, value = JsonConvert.DeserializeObject<T>(storedValue) };
  51. }
  52. }

Redis 工具类的更多相关文章

  1. Redis操作Hash工具类封装,Redis工具类封装

    Redis操作Hash工具类封装,Redis工具类封装 >>>>>>>>>>>>>>>>>> ...

  2. Redis操作字符串工具类封装,Redis工具类封装

    Redis操作字符串工具类封装,Redis工具类封装 >>>>>>>>>>>>>>>>>>& ...

  3. redis 工具类 单个redis、JedisPool 及多个redis、shardedJedisPool与spring的集成配置

    http://www.cnblogs.com/edisonfeng/p/3571870.html http://javacrazyer.iteye.com/blog/1840161 http://ww ...

  4. SpringBoot整合Redis及Redis工具类撰写

            SpringBoot整合Redis的博客很多,但是很多都不是我想要的结果.因为我只需要整合完成后,可以操作Redis就可以了,并不需要配合缓存相关的注解使用(如@Cacheable). ...

  5. redistemplate优雅地操作redis redis 工具类

    参考:https://www.cnblogs.com/superfj/p/9232482.html redis 工具类 package com.service; import org.springfr ...

  6. java的redis工具类

    package com.mracale.sell.utils; /** * @Auther: Mracale */ import org.springframework.beans.factory.a ...

  7. Redis 工具类 java 实现的redis 工具类

    最近了解了一下非关系型数据库 redis 会使用简单的命令 在自己本地电脑 使用时必须先启动服务器端 在启动客户端 redis 简介 Redis是一个开源的使用ANSI C语言编写.支持网络.可基于内 ...

  8. Java操作Redis工具类

    依赖 jar 包 <dependency> <groupId>redis.clients</groupId> <artifactId>jedis< ...

  9. spring boot 使用redis 及redis工具类

    1-添加maven依赖 2-添加redis配置 3-工具类 1-添加maven依赖 实际上是封装了jedis <!-- redis 依赖--> <dependency> < ...

随机推荐

  1. django admin 中实现word文档下载

    为了实现此功能,需要用到的知识点: (1)django  admin  自定义字段:   参考:http://www.cnblogs.com/wumingxiaoyao/p/6928297.html ...

  2. 【Django】uWSGI和Gunicorn【转】

    因为nginx等优秀的开源项目,有不少本来不是做服务器的同学也可以写很多服务器端的程序了.但是在聊天中会发现,大家虽然写了不少代码,但是对wsgi是什么,gunicorn是什么,反向代理又是什么并不了 ...

  3. AngularJS的 $resource服务 关于CRUD操作

    AngularJs 的CRUD 操作 是指对数据库的操作: CRUD其实是数据库基本操作中的Create(创建).ReadRetrieve(读取).Update(更新).Delete(删除).而这里的 ...

  4. 如何在ArcMap中监听键盘鼠标事件

    昨天有个朋友想要实现一个功能,就是在ArcMap中编辑数据的时候,能够通过快捷键自动设置预定义的属性,比如,选中若干要素,按A键,就自动填充属性,按B键,则又自动填充另外的属性字段. 单就这个功能而言 ...

  5. Sso单点登录分析

    1.   Sso系统分析 1.1. 什么是sso系统 SSO英文全称Single Sign On,单点登录.SSO是在多个应用系统中,用户只需要登录一次就可以访问所有相互信任的应用系统.它包括可以将这 ...

  6. 阿里巴巴Java开发规约扫描插件-Alibaba Java Coding Guidelines 在idea上安装使用教程

    经过247天的持续研发,阿里巴巴于10月14日在杭州云栖大会上,正式发布众所期待的<阿里巴巴Java开发规约>扫描插件!该插件由阿里巴巴P3C项目组研发.P3C是世界知名的反潜机,专门对付 ...

  7. 在SharePoint解决方案中使用JavaScript (0)

    随着Web前段技术(JavaScript/HTML5)的日益发扬光大,在Web应用程序中,我们开始更多的使用JavaScript.很多以往是放在服务器上运行的逻辑,现在都开始逐渐的向前段转移.这种趋势 ...

  8. memcache分布式的高速缓存系统

    http://baike.baidu.com/link?url=8v9IdWg0i_ptrTfz0APh32-SbvNUAWvXrcZM5vuJ8BrjCR2oylrieOXJ3vkSuRAq3kQV ...

  9. SpringBoot01 InteliJ IDEA安装、Maven配置、创建SpringBoot项目、yml属性配置、多环境配置、自定义properties配置

    1 IntelliJ IDEA 安装 下载地址:点击前往 注意:需要下载专业版本的,注册码在网上随便搜一个就行啦 2 MAVEN工具的安装 2.1 获取安装包 下载地址:点击前往 2.2 安装过程 到 ...

  10. 541. Reverse String II 指定翻转前k个的字符串

    [抄题]: Given a string and an integer k, you need to reverse the first k characters for every 2k chara ...