【Redis】springboot集成redis

一 、前言

pring为我们提供的缓存注解Spring Cache。Spring支持多种缓存技术:RedisCacheManager,EhCacheCacheManager、GuavaCacheManager等,今天的内容是集成RedisCacheManager实现缓存技术。

二 、Spring Cache

2.1@Cacheable
作用:查询数据加入缓存
参数如下:
cacheNames 缓存名称
key 缓存的key, SPEL表达式写法
condition 缓存执行的条件,返回true时候执行

2.2@CachePut
作用:修改了数据库的数据,同时更新缓存。
参数如下:
cacheNames 缓存名称
key 缓存的key, SPEL表达式写法
condition 缓存执行的条件,返回true时候执行

2.3@CacheEvict
作用:删除数据,删除缓存
参数如下:
allEntries boolean类型,表示是否需要清除缓存中的所有元素
key 需要删除的缓存的key

三、 集成配置

SpringBoot 2..(pom.xml)

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-websocket</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>javax</groupId>
        <artifactId>javaee-api</artifactId>
        <version>8.0</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-devtools</artifactId>
        <optional>true</optional>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-configuration-processor</artifactId>
        <version>2.0.2.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>p6spy</groupId>
        <artifactId>p6spy</artifactId>
        <version>3.8.0</version>
    </dependency>

    <dependency>
        <groupId>com.alibaba</groupId>
        <artifactId>druid-spring-boot-starter</artifactId>
        <version>1.1.10</version>
    </dependency>

    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <version>1.18.4</version>
    </dependency>
    <ependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-redis</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-test</artifactId>
        <version>5.1.5.RELEASE</version>
        <scope>compile</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-logging</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-test</artifactId>
    </dependency>
</dependencies>

applincation.yml

server:
  port: 809o
  servlet:
    context-path: /redis-web
spring:
  profiles: dev
  application:
    name: redis-provider
  datasource:
    type: com.alibaba.druid.pool.DruidDataSource
    driver-class-name: com.p6spy.engine.spy.P6SpyDriver
    url: jdbc:p6spy:mysql://127.0.0.1:3306/redis?autoReconnect=true&useSSL=false&characterEncoding=utf-8&serverTimezone=Asia/Shanghai&allowMultiQueries=true
    username: root(自己MySQL账户名、密码,别傻愣愣的拿过去就用)
    password: root
  #redis 
  redis:
    host: localhost
    port: 6379
    password:   (Rdis有密码就写上,默认为空)
    database: 0
    timeout: 300000ms
    jedis:
      pool:
        max-active: 8
        max-wait: -1ms
        max-idle: 500

redisConfig(配置)

/**
 * @author Zachary
 * @version 创建时间:2020年5月16日17:57:25
 */
@Configuration
@EnableCaching
public class RedisCacheConfig {

    /**
     * jackson ObjectMapper
     *
     * @return
     */
    @Bean
    public ObjectMapper objectMapper() {
        return JSONUtils.getObjectMapper();
    }

    /**
     * jackson 序列化器
     *
     * @param om
     * @return
     */
    @Bean
    public GenericJackson2JsonRedisSerializer genericJackson2JsonRedisSerializer(ObjectMapper om) {
        return new GenericJackson2JsonRedisSerializer(om);
    }

    @Bean
    public CacheManager cacheManager(RedisConnectionFactory connectionFactory) {
        return RedisCacheManager.builder(connectionFactory).build();
    }

    @Bean
    public RedisTemplate<String, String> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
        RedisTemplate<String, String> redisTemplate = new RedisTemplate<>();
        redisTemplate.setConnectionFactory(redisConnectionFactory);

        RedisSerializer stringSerializer = new StringRedisSerializer();

        // key value序列化
        redisTemplate.setKeySerializer(stringSerializer);
        redisTemplate.setValueSerializer(stringSerializer);

        // hash key value 序列化
        redisTemplate.setHashKeySerializer(stringSerializer);
        redisTemplate.setHashValueSerializer(stringSerializer);

        redisTemplate.afterPropertiesSet();
        return redisTemplate;
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Zachary_CN

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值