首页天道酬勤常用的一些工具类,redis工具类封装

常用的一些工具类,redis工具类封装

张世龙 05-05 19:22 50次浏览

此工具类提供简单的密钥值存储和散列存储。 使用StringRedisTemplate实现添加maven为: dependencygroupidredis.clients/groupidartifactidjedis/artifactidversion3.3.0/依赖于版本的ependencydependencygroupidorg.spring framework.data/groupidartifactidspring-data-redis/artifact id/从属配置reden cy redis spring.redis.time out=5000 spring.redis.jedis.pool.max-active=200 spring.redis.jedis dis.jedis.pool.time-between-eviction-runs=50000 spring.redis.host=xx importorg.spring framework.data.rens importorg.spring framework.stereotype.service; import java.util.HashMap; import java.util.HashSet; import java.util.Map; import java.util.Optional; import java.util.Set; import Java.util.concurrent.time unit; @ slf4j @ servicepublicclassredisservice { privatefinalstringredistemplatestringredistemplate; publicredisservice (stringredistemplatestringredistemplate (this.stringredistemplate=stringredistemplate; //普通key,valuepublicvoidset (操作string key,String value,long timeout,timeunit ) try ) stringredistemplate.ops for } cor } publicoptionalstringget (string key ) try ) return optional.of nullable (stringredistemplate.ops for value (.get ) ) ) } return Optional.empty (; }公共语音戴尔(字符串密钥) try ) stringredistemplate.delete )密钥; }catch(exceptione ) log.error ) setwithexpiretime ),e ); }//操作hash setName是hash表的名称key是构造的,值publicvoidhset(stringsetname,stringkey(try ) stringredistemplate.opsforhash } } publicvoidhincrementby (字符串集名称,字符串密钥) try ) stringredistemplate.opsforhash ).increment (字符串集名称,集名称) }catch(exceptione ) log.error ) (Hincrbyerror ),e ); } (公共映射对象,对象集名称(try ) returnstringredistemplate.opsforhash ).entries (set name ) )。 }catch(exceptione ) log.error )、Hgetallerror )、e ); }返回new hashmap (; } publicsetobjecthkeys (字符串集名称) try ) returnstringredistemplate.opsforhash ).keys ) setname; }catch(exceptione ) log.error ) (Hkeyserror ),e ); } return new HashSet (; } publiclonghdel (字符串集名称) try ) returnstringredistemplate.opsforhash ).delete ) setname ); }catch(exceptione ) log.error )、e ); }返回- 1l; }注:打印日志使用的是lombok插件

redis和java,redis集群和哨兵的区别