SpringCache+Redis

1、引入依赖

		<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-cache</artifactId>
        </dependency>
        
        <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>redis.clients</groupId>
		    <artifactId>jedis</artifactId>
		</dependency>


2、相关配置

spring:
  application:
    name: Application
  redis:
    host: 127.0.0.1
    port: 6379
    database: 1
  cache:
    redis:
      time-to-live: 3600000
      cache-null-values: true
      key-prefix: CACHE_
      use-key-prefix: true
package com.zhangz.config;

import org.springframework.boot.autoconfigure.cache.CacheProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.cache.RedisCacheConfiguration;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializationContext;
import org.springframework.data.redis.serializer.StringRedisSerializer;

@Configuration
//绑定配置文件
@EnableConfigurationProperties(CacheProperties.class)
//开启SpringCache
@EnableCaching
public class RedisCacheConfig {

    @Bean
    public RedisCacheConfiguration getRedisCacheConfig(CacheProperties cacheProperties){
        //获取默认的redis缓存配置
        RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig();
        //设置key的序列化方式
        config.serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(new StringRedisSerializer()));
        //设置value 的序列化方式
        config.serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(new GenericJackson2JsonRedisSerializer()));
        
        CacheProperties.Redis redisProperties = cacheProperties.getRedis();
        
        //将CacheProperties(prefix = "spring.cache")相关配置导入
        if (redisProperties.getTimeToLive() != null) {
            //过期时间 spring.cache.redis.time-to-live
            config = config.entryTtl(redisProperties.getTimeToLive());
        }
        if (redisProperties.getKeyPrefix() != null) {
            //key前缀 spring.cache.redis.key-prefix
            config = config.prefixKeysWith(redisProperties.getKeyPrefix());
        }
        if (!redisProperties.isCacheNullValues()) {
            //是否缓存空值 spring.cache.redis.cache-null-values
            config = config.disableCachingNullValues();
        }
        if (!redisProperties.isUseKeyPrefix()) {
            //是否使用前缀 spring.cache.redis.use-key-prefix
            config = config.disableKeyPrefix();
        }
        return config;
    }
}

3、使用

    @RequestMapping("/rule")
    //默认使用value作为key值、自定义配置key可以使用SpEL表达式
    /**
	 * Spring Expression Language (SpEL) expression for computing the key dynamically.
	 * <p>Default is {@code ""}, meaning all method parameters are considered as a key,
	 * unless a custom {@link #keyGenerator} has been configured.
	 * <p>The SpEL expression evaluates against a dedicated context that provides the
	 * following meta-data:
	 * <ul>
	 * <li>{@code #root.method}, {@code #root.target}, and {@code #root.caches} for
	 * references to the {@link java.lang.reflect.Method method}, target object, and
	 * affected cache(s) respectively.</li>
	 * <li>Shortcuts for the method name ({@code #root.methodName}) and target class
	 * ({@code #root.targetClass}) are also available.
	 * <li>Method arguments can be accessed by index. For instance the second argument
	 * can be accessed via {@code #root.args[1]}, {@code #p1} or {@code #a1}. Arguments
	 * can also be accessed by name if that information is available.</li>
	 * </ul>
	 */
    @Cacheable(value = "rule",key = "#root.methodName")
    public String rule(){
        return service.putRule();
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值