你知道如何在springboot中使用redis吗

2021/5/7 19:31:12

本文主要是介绍你知道如何在springboot中使用redis吗,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

图片


说明:本文针对的是新版 spring boot 2.1.3,其 spring data 依赖为 spring-boot-starter-data-redis,且其默认连接池为 lettuce

  redis 作为一个高性能的内存数据库,如果不会用就太落伍了,之前在 node.js 中用过 redis,本篇记录如何将 redis 集成到 spring boot 中。提供 redis 操作类,和注解使用 redis 两种方式。主要内容如下:

  • docker 安装 redis

  • springboot 集成 redis

  • 编写 redis 操作类

  • 通过注解使用 redis

安装 redis

  通过 docker 安装,docker compose 编排文件如下:

# docker-compose.ymlversion: "2"services:  redis:    container_name: redis    image: redis:3.2.10    ports:      - "6379:6379"

  然后在 docker-compose.yml所在目录使用 docker-compose up-d命令,启动 redis。

集成 springboot

  说明:springboot 版本为 2.1.3

添加 maven 依赖

  只需添加 spring-boot-starter-data-redis依赖即可,并排除 lettuce 依赖,然后引入 jedis 和 jedis 的依赖 commons-pool2

  1. <dependency>

  2.    <groupId>org.springframework.boot</groupId>

  3.    <artifactId>spring-boot-starter-data-redis</artifactId>

  4.    <exclusions>

  5.        <exclusion>

  6.            <groupId>io.lettuce</groupId>

  7.            <artifactId>lettuce-core</artifactId>

  8.        </exclusion>

  9.    </exclusions>

  10. </dependency>


  11. <dependency>

  12.    <groupId>org.apache.commons</groupId>

  13.    <artifactId>commons-pool2</artifactId>

  14. </dependency>


  15. <dependency>

  16.    <groupId>redis.clients</groupId>

  17.    <artifactId>jedis</artifactId>

  18. </dependency>

编写 springboot 配置文件

  配置文件如下:

server:  port: 8081  servlet:    context-path: /ssospring:  application:    name: SSO  cache:    type: redis  redis:    database: 0    host: 192.168.226.5    port: 6379    # 有密码填密码,没有密码不填    password:    # 连接超时时间(ms)    timeout: 1000ms    # 高版本springboot中使用jedis或者lettuce    jedis:      pool:        # 连接池最大连接数(负值表示无限制)        max-active: 8        # 连接池最大阻塞等待时间(负值无限制)        max-wait: 5000ms        # 最大空闲链接数        max-idle: 8        # 最小空闲链接数        min-idle: 0

编写配置类

  配置类代码如下:

  1. @EnableCaching//开启缓存

  2. @Configuration

  3. public class RedisConfig extends CachingConfigurerSupport {


  4.    /**

  5.     * 设置缓存管理器,这里可以配置默认过期时间等

  6.     *

  7.     * @param connectionFactory 连接池

  8.     * @return

  9.     */

  10.    @Bean

  11.    public CacheManager cacheManager(RedisConnectionFactory connectionFactory) {

  12.        RedisCacheConfiguration redisCacheConfiguration = RedisCacheConfiguration

  13.                .defaultCacheConfig()

  14.                .entryTtl(Duration.ofSeconds(60));

  15.        //注意:请勿使用先new 配置对象,然后在调用entryTtl方法的方式来操作

  16.        //会导致配置不生效,原因是调用.entryTtl方法会返回一个新的配置对象,而不是在原来的配置对象上修改


  17.        RedisCacheWriter redisCacheWriter = RedisCacheWriter.nonLockingRedisCacheWriter(connectionFactory);

  18.        RedisCacheManager manager = new RedisCacheManager(redisCacheWriter, redisCacheConfiguration);

  19.        return manager;

  20.    }


  21.    @SuppressWarnings("all")

  22.    @Bean

  23.    public RedisTemplate<String, String> redisTemplate(JedisConnectionFactory factory) {

  24.        StringRedisTemplate template = new StringRedisTemplate(factory);

  25.        Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);

  26.        ObjectMapper om = new ObjectMapper();

  27.        om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);

  28.        om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);

  29.        jackson2JsonRedisSerializer.setObjectMapper(om);

  30.        RedisSerializer stringSerializer = new StringRedisSerializer();

  31.        template.setKeySerializer(stringSerializer);

  32.        template.setValueSerializer(jackson2JsonRedisSerializer);

  33.        template.setHashKeySerializer(stringSerializer);

  34.        template.setHashValueSerializer(jackson2JsonRedisSerializer);

  35.        template.afterPropertiesSet();

  36.        return template;

  37.    }


  38.    //使用jedis连接池建立jedis连接工厂

  39.    @Bean

  40.    public JedisConnectionFactory jedisConnectionFactory() {

  41.        logger.info("jedisConnectionFactory:初始化了");

  42.        JedisPoolConfig config = new JedisPoolConfig();

  43.        config.setMaxIdle(maxIdle);

  44.        config.setMinIdle(minIdle);

  45.        config.setMaxWaitMillis(maxWaitMillis);

  46.        config.setMaxTotal(maxActive);

  47.        //链接耗尽时是否阻塞,默认true

  48.        config.setBlockWhenExhausted(true);

  49.        //是否启用pool的jmx管理功能,默认true

  50.        config.setJmxEnabled(true);

  51.        JedisConnectionFactory factory = new JedisConnectionFactory();

  52.        factory.setPoolConfig(config);

  53.        factory.setHostName(host);

  54.        factory.setPort(port);

  55.        factory.setPassword(password);

  56.        factory.setDatabase(database);

  57.        factory.setTimeout(timeout);

  58.        return factory;

  59.    }

  60. }

使用方法

  有两种方法来进行缓存操作,一种是在方法上添加缓存注解实现各种操作,一种是手动控制。个人比较喜欢手动控制,觉得这样都在自己的掌控中。

通过注解使用

  主要有以下 5 个注解:

  • @CacheConfig: 类级别缓存,设置缓存 key 前缀之类的

  • @Cacheable: 触发缓存入口

  • @CacheEvict: 触发移除缓存

  • @CachePut: 更新缓存

  • @Caching: 组合缓存

@CacheConfig

  该注解可以将缓存分类,它是类级别注解,主要用于给某个类的缓存全局配置,例子如下:

@CacheConfig(cacheNames = "redis_test")@Servicepublic class RedisService {  //....}

上面 CacheConfig 会给类下通过注解生成的 key 加上 redis_test 的前缀。

@Cacheable

  方法级别注解,根据 key 查询缓存:

  • 如果 key 不存在,将方法返回值缓存到 redis 中

  • 如果 key 存在,直接从缓存中取值 例子如下:

    /**     * 缓存时间,首次查询后会缓存结果,key中的值可使用表达式计算.     * 如不提供key,将使用默认key构造方法生成一个key     * @return long     */    @Cacheable(key = "'currentTime'")    public long getTime() {        return System.currentTimeMillis();    }

多次调用此段代码会发现每次返回的值都是一样的。

CachePut

  用于更新缓存,每次调用都会想 db 请求,缓存数据

  • 如果 key 存在,更新内容

  • 如果 key 不存在,插入内容

代码如下:

/**     * 一般用于更新查插入操作,每次都会请求db     */    @CachePut(key = "'currentTime'+#id")    public long updateTime(String id) {        return System.currentTimeMillis();    }

每次调用此方法都会根据 key 刷新 redis 中的缓存数据。

@CacheEvict

  根据 key 删除缓存中的数据。allEntries=true 表示删除缓存中所有数据。代码如下:

    @CacheEvict(key = "'currentTime'+#id",allEntries=false)    public void deleteTime(String id) {    }

@Caching

  本注解可将其他注解组合起来使用。比如下面的例子:

    //value属性为key指定前缀    @Caching(put = {@CachePut(value = "user", key = "'name_'+#user.name"),            @CachePut(value = "user", key = "'pass_'+#user.password")})    public User testCaching(User user) {        return user;    }

上面的代码执行后将在 redis 中插入两条记录。使用 keys*将看到如下结果:

图片

手动控制

  手动控制就相当于 mybatis 的手写 sql 语句,需要调用 redisTemplate中的各种方法来进行缓存查询,缓存更新,缓存删除等操作。

  使用方法参见 util/RedisUtil 中的方法。redisTemplate基本可以实现所有的 redis 操作。




这篇关于你知道如何在springboot中使用redis吗的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程