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

薄暮传说

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

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

  • docker 安装 redis
  • springboot 集成 redis
  • 编写 redis 操做类
  • 经过注解使用 redis

安装 redis

  经过 docker 安装,docker compose 编排文件以下:git

# docker-compose.yml
version: "2"
services:
 redis:
 container_name: redis
 image: redis:3.2.10
 ports:
 - "6379:6379"
复制代码

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

集成 springboot

  说明:springboot 版本为 2.1.3redis

添加 maven 依赖

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

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-redis</artifactId>
    <exclusions>
        <exclusion>
            <groupId>io.lettuce</groupId>
            <artifactId>lettuce-core</artifactId>
        </exclusion>
    </exclusions>
</dependency>

<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-pool2</artifactId>
</dependency>

<dependency>
    <groupId>redis.clients</groupId>
    <artifactId>jedis</artifactId>
</dependency>
复制代码

编写 springboot 配置文件

  配置文件以下:sql

server:
 port: 8081
 servlet:
 context-path: /sso
spring:
 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
复制代码

编写配置类

  配置类代码以下:docker

@EnableCaching//开启缓存
@Configuration
public class RedisConfig extends CachingConfigurerSupport {

    /** * 设置缓存管理器,这里能够配置默认过时时间等 * * @param connectionFactory 链接池 * @return */
    @Bean
    public CacheManager cacheManager(RedisConnectionFactory connectionFactory) {
        RedisCacheConfiguration redisCacheConfiguration = RedisCacheConfiguration
                .defaultCacheConfig()
                .entryTtl(Duration.ofSeconds(60));
        //注意:请勿使用先new 配置对象,而后在调用entryTtl方法的方式来操做
        //会致使配置不生效,缘由是调用.entryTtl方法会返回一个新的配置对象,而不是在原来的配置对象上修改

        RedisCacheWriter redisCacheWriter = RedisCacheWriter.nonLockingRedisCacheWriter(connectionFactory);
        RedisCacheManager manager = new RedisCacheManager(redisCacheWriter, redisCacheConfiguration);
        return manager;
    }

    @SuppressWarnings("all")
    @Bean
    public RedisTemplate<String, String> redisTemplate(JedisConnectionFactory factory) {
        StringRedisTemplate template = new StringRedisTemplate(factory);
        Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
        ObjectMapper om = new ObjectMapper();
        om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
        om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
        jackson2JsonRedisSerializer.setObjectMapper(om);
        RedisSerializer stringSerializer = new StringRedisSerializer();
        template.setKeySerializer(stringSerializer);
        template.setValueSerializer(jackson2JsonRedisSerializer);
        template.setHashKeySerializer(stringSerializer);
        template.setHashValueSerializer(jackson2JsonRedisSerializer);
        template.afterPropertiesSet();
        return template;
    }

    //使用jedis链接池创建jedis链接工厂
    @Bean
    public JedisConnectionFactory jedisConnectionFactory() {
        logger.info("jedisConnectionFactory:初始化了");
        JedisPoolConfig config = new JedisPoolConfig();
        config.setMaxIdle(maxIdle);
        config.setMinIdle(minIdle);
        config.setMaxWaitMillis(maxWaitMillis);
        config.setMaxTotal(maxActive);
        //连接耗尽时是否阻塞,默认true
        config.setBlockWhenExhausted(true);
        //是否启用pool的jmx管理功能,默认true
        config.setJmxEnabled(true);
        JedisConnectionFactory factory = new JedisConnectionFactory();
        factory.setPoolConfig(config);
        factory.setHostName(host);
        factory.setPort(port);
        factory.setPassword(password);
        factory.setDatabase(database);
        factory.setTimeout(timeout);
        return factory;
    }
}
复制代码

使用方法

  有两种方法来进行缓存操做,一种是在方法上添加缓存注解实现各类操做,一种是手动控制。我的比较喜欢手动控制,以为这样都在本身的掌控中。数据库

经过注解使用

  主要有如下 5 个注解:apache

  • @CacheConfig: 类级别缓存,设置缓存 key 前缀之类的
  • @Cacheable: 触发缓存入口
  • @CacheEvict: 触发移除缓存
  • @CachePut: 更新缓存
  • @Caching: 组合缓存

@CacheConfig

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

@CacheConfig(cacheNames = "redis_test")
@Service
public 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

项目源码:github

相关文章
相关标签/搜索