spring-boot整合redis和lettuce

本文涉及的产品
云数据库 Tair(兼容Redis),内存型 2GB
Redis 开源版,标准版 2GB
推荐场景:
搭建游戏排行榜
简介: Lettuce和Jedis的都是连接Redis Server的客户端程序。Jedis在实现上是直连redis server,多线程环境下非线程安全,除非使用连接池,为每个Jedis实例增加物理连接。Lettuce基于Netty的连接实例(StatefulRedisConnection),可以在多个线程间并发访问,且线程安全,满足多线程环境下的并发访问,同时它是可伸缩的设计,一个连接实例不够的情况也可以按需增加连接实例。

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有更好的使用方式,请评论区沟通。

相关实践学习
基于Redis实现在线游戏积分排行榜
本场景将介绍如何基于Redis数据库实现在线游戏中的游戏玩家积分排行榜功能。
云数据库 Redis 版使用教程
云数据库Redis版是兼容Redis协议标准的、提供持久化的内存数据库服务,基于高可靠双机热备架构及可无缝扩展的集群架构,满足高读写性能场景及容量需弹性变配的业务需求。 产品详情:https://www.aliyun.com/product/kvstore &nbsp; &nbsp; ------------------------------------------------------------------------- 阿里云数据库体验:数据库上云实战 开发者云会免费提供一台带自建MySQL的源数据库&nbsp;ECS 实例和一台目标数据库&nbsp;RDS实例。跟着指引,您可以一步步实现将ECS自建数据库迁移到目标数据库RDS。 点击下方链接,领取免费ECS&amp;RDS资源,30分钟完成数据库上云实战!https://developer.aliyun.com/adc/scenario/51eefbd1894e42f6bb9acacadd3f9121?spm=a2c6h.13788135.J_3257954370.9.4ba85f24utseFl
相关文章
|
10天前
|
SQL JSON Java
mybatis使用三:springboot整合mybatis,使用PageHelper 进行分页操作,并整合swagger2。使用正规的开发模式:定义统一的数据返回格式和请求模块
这篇文章介绍了如何在Spring Boot项目中整合MyBatis和PageHelper进行分页操作,并且集成Swagger2来生成API文档,同时定义了统一的数据返回格式和请求模块。
22 1
mybatis使用三:springboot整合mybatis,使用PageHelper 进行分页操作,并整合swagger2。使用正规的开发模式:定义统一的数据返回格式和请求模块
|
17天前
|
NoSQL Java Redis
redis的基本命令,并用netty操作redis(不使用springboot或者spring框架)就单纯的用netty搞。
这篇文章介绍了Redis的基本命令,并展示了如何使用Netty框架直接与Redis服务器进行通信,包括设置Netty客户端、编写处理程序以及初始化Channel的完整示例代码。
26 1
redis的基本命令,并用netty操作redis(不使用springboot或者spring框架)就单纯的用netty搞。
|
2天前
|
缓存 NoSQL Java
Spring Boot与Redis:整合与实战
【10月更文挑战第15天】本文介绍了如何在Spring Boot项目中整合Redis,通过一个电商商品推荐系统的案例,详细展示了从添加依赖、配置连接信息到创建配置类的具体步骤。实战部分演示了如何利用Redis缓存提高系统响应速度,减少数据库访问压力,从而提升用户体验。
9 2
|
18天前
|
NoSQL Java Redis
在 Spring 中操作 Redis
本文详细介绍了在Spring框架中如何通过引入依赖、配置文件、使用StringRedisTemplate类以及执行原生命令等方式来操作Redis数据库,并提供了对String、List、Set、Hash和ZSet数据类型的操作示例。
45 0
在 Spring 中操作 Redis
|
22天前
|
缓存 NoSQL Java
Springboot自定义注解+aop实现redis自动清除缓存功能
通过上述步骤,我们不仅实现了一个高度灵活的缓存管理机制,还保证了代码的整洁与可维护性。自定义注解与AOP的结合,让缓存清除逻辑与业务逻辑分离,便于未来的扩展和修改。这种设计模式非常适合需要频繁更新缓存的应用场景,大大提高了开发效率和系统的响应速度。
42 2
|
1月前
|
NoSQL 网络协议 Java
[Redis] 渐进式遍历+使用jedis操作Redis+使用Spring操作Redis
[Redis] 渐进式遍历+使用jedis操作Redis+使用Spring操作Redis
32 7
|
1月前
|
NoSQL Java 网络安全
[Redis] 渐进式遍历+使用jedis操作Redis+使用Spring操作Redis
[Redis] 渐进式遍历+使用jedis操作Redis+使用Spring操作Redis
|
23天前
|
存储 NoSQL Java
Spring Boot项目中使用Redis实现接口幂等性的方案
通过上述方法,可以有效地在Spring Boot项目中利用Redis实现接口幂等性,既保证了接口操作的安全性,又提高了系统的可靠性。
22 0
|
17天前
|
存储 缓存 NoSQL
数据的存储--Redis缓存存储(一)
数据的存储--Redis缓存存储(一)
53 1
|
17天前
|
存储 缓存 NoSQL
数据的存储--Redis缓存存储(二)
数据的存储--Redis缓存存储(二)
33 2
数据的存储--Redis缓存存储(二)