Redis 工具类
项目里的Redis 工具类,写下来以备后用
public class RedisConnector
{
public class RedisParseResult<T>
{
public bool success;
public T value;
}
private static string ConnectionString { get; set; }
private static ConnectionMultiplexer RedisConnection { get; set; }
public RedisConnector(string redisConnectionString)
{
ConnectionString = redisConnectionString;
try
{
RedisConnection = ConnectionMultiplexer.Connect(ConnectionString);
}
catch (RedisConnectionException e)
{
AILogger.LogError(e.Message, $"Redis server : {redisConnectionString}").Wait();
}
}
public static void Initialize(string redisConnectionString)
{
ConnectionString = redisConnectionString;
try
{
RedisConnection = ConnectionMultiplexer.Connect(ConnectionString);
}
catch (RedisConnectionException e)
{
AILogger.LogError(e.Message, $"Redis server : {redisConnectionString}").Wait();
}
}
public static IDatabase RedisStore => (null != RedisConnection) ? RedisConnection.GetDatabase() : ConnectionMultiplexer.Connect(ConnectionString).GetDatabase();
public static ConnectionMultiplexer Redis => RedisConnection ?? ConnectionMultiplexer.Connect(ConnectionString);
, bool replace = false) => await RedisStore.StringSetAsync(key, value, TimeSpan.FromSeconds(expiry), replace ? When.Always : When.NotExists);
, bool replace = false) => await RedisStore.StringSetAsync(key, value.ToString(), TimeSpan.FromSeconds(expiry), replace ? When.Always : When.NotExists);
, bool replace = false) => await RedisStore.StringSetAsync(key, JsonConvert.SerializeObject(value), TimeSpan.FromSeconds(expiry), replace ? When.Always : When.NotExists);
public static string Get(string key) => RedisStore.StringGet(key);
public static RedisParseResult<T> Get<T>(string key)
{
var storedValue = RedisStore.StringGet(key);
if (string.IsNullOrEmpty(storedValue))
return new RedisParseResult<T> { success = false };
else
return new RedisParseResult<T> { success = true, value = JsonConvert.DeserializeObject<T>(storedValue) };
}
}
Redis 工具类的更多相关文章
- Redis操作Hash工具类封装,Redis工具类封装
Redis操作Hash工具类封装,Redis工具类封装 >>>>>>>>>>>>>>>>>> ...
- Redis操作字符串工具类封装,Redis工具类封装
Redis操作字符串工具类封装,Redis工具类封装 >>>>>>>>>>>>>>>>>>& ...
- redis 工具类 单个redis、JedisPool 及多个redis、shardedJedisPool与spring的集成配置
http://www.cnblogs.com/edisonfeng/p/3571870.html http://javacrazyer.iteye.com/blog/1840161 http://ww ...
- SpringBoot整合Redis及Redis工具类撰写
SpringBoot整合Redis的博客很多,但是很多都不是我想要的结果.因为我只需要整合完成后,可以操作Redis就可以了,并不需要配合缓存相关的注解使用(如@Cacheable). ...
- redistemplate优雅地操作redis redis 工具类
参考:https://www.cnblogs.com/superfj/p/9232482.html redis 工具类 package com.service; import org.springfr ...
- java的redis工具类
package com.mracale.sell.utils; /** * @Auther: Mracale */ import org.springframework.beans.factory.a ...
- Redis 工具类 java 实现的redis 工具类
最近了解了一下非关系型数据库 redis 会使用简单的命令 在自己本地电脑 使用时必须先启动服务器端 在启动客户端 redis 简介 Redis是一个开源的使用ANSI C语言编写.支持网络.可基于内 ...
- Java操作Redis工具类
依赖 jar 包 <dependency> <groupId>redis.clients</groupId> <artifactId>jedis< ...
- spring boot 使用redis 及redis工具类
1-添加maven依赖 2-添加redis配置 3-工具类 1-添加maven依赖 实际上是封装了jedis <!-- redis 依赖--> <dependency> < ...
随机推荐
- /etc/rc5.d/s991local: line25: eject:command not found错误
使用虚拟机安装centos出现错误,原因是我使用的镜像是最小级别的,没有图形化界面,只有终端窗口 有人用vmware安装minimal centos报错/etc/rc5.d/s99local : ...
- 从零玩转JavaWeb系列7web服务器-----get与post的区别
总结get与post的区别 get参数通过url传递,post放在request body中. get请求在url中传递的参数是有长度限制的,而post没有. get比post更不安全,因为参数直接暴 ...
- springboot 跳过单元测试
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-suref ...
- 使apk具有system权限
使apk具有system权限的方法: 方法一: 1. 在应用程序的AndroidManifest.xml中的manifest节点中加入 android:sharedUserId=" ...
- screen command of linux
[screen command of linux] 常用键: 补充: Ctrl-a S # split terminal horizon Ctrl-a TAB # switch to ano ...
- centos6.5 源码安装 mysql
1.下载源码包 我的版本:mysql-5.6.4-m7.tar.gz 2.安装之前先卸载CentOS自带的MySQL [root@localhost ~]# yum remove mysql 3.编译 ...
- c++ 观察者模式(observer)
观察者模式:定义对象间的一种一对多的依赖关系,当一个对象的状态发生改变时,所有依赖于它的对象都得到通知并被自动更新.它还有两个别名,依赖 (Dependents),发布-订阅(Publish-Subs ...
- 第一个Django应用程序_part3
一.概述 此文延续第一个Django应用程序part2. 官方文档:https://docs.djangoproject.com/en/1.11/intro/tutorial03/ view是Djan ...
- [Plan]计划
1. scala 2. kafka 1. lua 2. openResty 1. 日志收集 - python 2. 代码生成 3. 权限系统
- Qt程序无法输入中文的问题
问题 在Linux环境下,用Qt编写的程序运行时不能在诸如输入框.文本框中输入中文(不会激活中文输入法). 注意与输入法类型有关(基于iBus或Fcitx) 原因 Qt程序的中文输入支持需要用Qt插件 ...