您现在的位置是:首页 > 文章详情

spring-boot整合redis和lettuce

日期:2019-06-17点击:488

1.在项目pom中引人

<dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-pool2</artifactId> </dependency> <!--redis数据库--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency>
  1. 在application.properties配置
#redis数据库配置 #连接池最大连接数(使用负值表示没有限制) spring.redis.lettuce.pool.max-active=-1 #连接池中的最大空闲连接 spring.redis.lettuce.pool.max-idle=100 #连接池最大阻塞等待时间(使用负值表示没有限制) spring.redis.lettuce.pool.max-wait=-1ms #连接池中的最小空闲连接 spring.redis.lettuce.pool.min-idle=0 #redis数据库链接配置 spring.redis.host=127.0.0.1 spring.redis.port=6379 spring.redis.password= spring.redis.database=0

3.使用StringRedisTemplate操作redis

 @Service public class RedisService { @Autowired private StringRedisTemplate stringRedisTemplate; /** * 执行set操作 * * @param key * @return */ public void set(final String key, final String value) { stringRedisTemplate.opsForValue().set(key, value); } /** * 执行get操作 * @param key * @return */ public String get(final String key) { return stringRedisTemplate.opsForValue().get(key); } /** * 执行delete操作 * * @param key * @return */ public boolean del(final String key) { return stringRedisTemplate.delete(key); } /** * 执行set操作并且设置生存时间,单位为:秒 * * @param key * @param value //TimeUnit.SECONDS 秒 * //TimeUnit.MINUTES 分 * @return */ public void set(final String key, final String value, final Integer seconds) { stringRedisTemplate.opsForValue().set(key, value, seconds, TimeUnit.SECONDS); } /** * 执行hset操作 * * @param key * @param * @return */ public void hset(final String key, final String mapkey, final String mapvalue) { stringRedisTemplate.opsForHash().put(key, mapkey, mapvalue); } /** * 执行hgetAll操作 * * @param key * @param * @return */ public Map<String, String> hgetAll(final String key) { return (Map)stringRedisTemplate.opsForHash().entries(key); } /** * 执行hdel操作 * * @param key * @param * @return */ public long hdel(final String key, final String[] strings) { return stringRedisTemplate.opsForHash().delete(key, strings); } /** * 执行hkeys操作 * * @param * @param key * @return */ public Set<String> hkeys(final String key) { return (Set) stringRedisTemplate.opsForHash().keys(key); } /** * 执行hvalues操作 * * @param * @param key * @return */ public List<String> hvalues(final String key) { return (List) stringRedisTemplate.opsForHash().values(key); } /** * 执行hget操作 * * @param * @param key * @return */ public String hget(final String key, final String mapkey) { return (String)stringRedisTemplate.opsForHash().get(key, mapkey); } /** * 执行hmset操作 * * @param * @param key * @return */ public void hmset(final String key, final Map<String, String> mapvalue) { stringRedisTemplate.opsForHash().putAll(key, mapvalue); } /** * 执行lpush操作 * @param * @param key * @return */ public long lpush(final String key,final String value) { return stringRedisTemplate.opsForList().leftPush(key,value); } /** * 执行lpop操作 * @param * @param key * @return */ public String lpop(final String key) { return stringRedisTemplate.opsForList().leftPop(key); } /** * 执行rpop操作 * @param * @param key * @return */ public String rpop(final String key) { return stringRedisTemplate.opsForList().rightPop(key); } /** * 执行list操作 * 在列表中的尾部添加一个个值,返回列表的长度 * * @param key * @return */ public Long rpush(final String key, final String value) { return stringRedisTemplate.opsForList().rightPush(key,value); } /** * 执行list操作 * 在列表中的尾部添加多个值,返回列表的长度 * * @param key * @return */ public Long rpush(final String key, final String[] value) { return stringRedisTemplate.opsForList().rightPushAll(key,value); } /** * 执行list操作 * 获取List列表 * * @param key * @return */ public List<String> lrange(final String key, final long start, final long end) { return stringRedisTemplate.opsForList().range(key,start,end); } /** * 执行list操作 * 通过索引获取列表中的元素 * * @param key * @return */ public String lindex(final String key, final long index) { return stringRedisTemplate.opsForList().index(key,index); } /** * 执行list操作 * 获取列表长度,key为空时返回0 * * @param key * @return */ public Long llen(final String key) { return stringRedisTemplate.opsForList().size(key); } public boolean expire(final String key, final Integer seconds) { return stringRedisTemplate.expire(key,seconds,TimeUnit.SECONDS); } } 

4.如果对lettuce有更好的使用方式,请评论区沟通。

原文链接:https://yq.aliyun.com/articles/705798
关注公众号

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。

持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。

转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。

文章评论

共有0条评论来说两句吧...

文章二维码

扫描即可查看该文章

点击排行

推荐阅读

最新文章