b2科目四模拟试题多少题驾考考爆了怎么补救
b2科目四模拟试题多少题 驾考考爆了怎么补救

SpringBoot 2.x 使用Redis作为缓存 设置有效时间及手动升级思路

电脑杂谈  发布时间:2019-08-28 10:01:49  来源:网络整理

boot(x:)怎么删除_one x刷boot_boot(x:)可以删除吗

本文链接:

本文基于Springboot2.0.4 使用mysql

由于在redis的客户端上采用了Letture

这里讲一下jedis和Letture的简单说明

所以一开始在推进程序的之后就碰到这个错误

Caused by: java.lang.ClassNotFoundException: org.apache.commons.pool2.impl.GenericObjectPoolConfig
    at java.net.URLClassLoader.findClass(URLClassLoader.java:381) ~[na:1.8.0_91]
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424) ~[na:1.8.0_91]
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331) ~[na:1.8.0_91]
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357) ~[na:1.8.0_91]
    ... 49 common frames omitted

boot(x:)怎么删除_one x刷boot_boot(x:)可以删除吗

在pom文件中引入以下的包

<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-pool2</artifactId>
</dependency>
与Redis相关的POM依赖
<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>
</dependency>
Application里面的配置

这里使用的time-to-live是对所有redis缓存统一配置的时间 实际使用会有不便于的地方 可能不同的缓存必须不同的超时时间

spring:
  application:
    name: redis-demo
  cache:
    type: redis
    redis:
      time-to-live: 20000 #缓存超时时间ms
      cache-null-values: false #是否缓存空值
  redis:
    port: 6379
    host: localhost
    lettuce:
      pool:
        max-active: 8
        max-wait: -1
        max-idle: 8
        min-idle: 0
    timeout: 10000  #redis 连接超时时间ms
    database: 0
RedisCacheManager的配置

单独为不同的缓存可以配置不同的超时时间

disableCachingNullValues 不缓存空值

one x刷boot_boot(x:)怎么删除_boot(x:)可以删除吗

网上好多教程的配置是

        RedisCacheConfiguration defaultCacheConfig = RedisCacheConfiguration.defaultCacheConfig();
        defaultCacheConfig.entryTtl(Duration
                .ofSeconds(30L));
        defaultCacheConfig.disableCachingNullValues();

这种配置是出错的 看完entryTtl和disableCachingNullValues的返回值均为RedisCacheConfiguration 所以下面的配置方式是无效的

下面是配置源码

@Bean
    CacheManager cacheManager(RedisConnectionFactory connectionFactory) {
        /* 默认配置, 默认超时时间为30s */
        RedisCacheConfiguration defaultCacheConfig = RedisCacheConfiguration.defaultCacheConfig().entryTtl(Duration
                .ofSeconds(30L)).disableCachingNullValues();
        /* 配置test的超时时间为120s*/
        RedisCacheManager cacheManager = RedisCacheManager.builder(RedisCacheWriter.lockingRedisCacheWriter
                (connectionFactory)).cacheDefaults(defaultCacheConfig).withInitialCacheConfigurations(singletonMap
                ("test", RedisCacheConfiguration.defaultCacheConfig().entryTtl(Duration.ofSeconds(120L))
                        .disableCachingNullValues())).transactionAware().build();
        return cacheManager;
    }
注解式的Redis处理

注解式的使用就是在步骤里面加上Cacheable / CacheEvict / CachePut的注解

one x刷boot_boot(x:)可以删除吗_boot(x:)怎么删除

注解支持使用EL表达式 这里就是支持使用相关的参数和属性来表示

#root.targetClass 是类名

#p0是第一个参数值

@Cacheable(value = "test", key = "#root.targetClass + '_' + #p0 + '_' + #p1")

到此使用简洁的注解式的redis缓存配置就结束了

实际在项目中会碰到这种特殊的画面 某些缓存更期望用一个线程负责升级缓存 而不是单独的请求去判定 本文缓存更新采用了RedisTemplate手动写入的模式

boot(x:)怎么删除_one x刷boot_boot(x:)可以删除吗

5. RedisTemplate的配置

一开始使用的序列化方式不对 导致序列化出来的和前面平台自动缓存的不一致,导致后面调用缓存的之后总是值出错 在网上看了好多资料 后来看了源码 试了好多序列化方式 发现默认的是这个序列化类JdkSerializationRedisSerializer

在同时使用了前面注解的缓存和这些自动缓存的之后 特别应该切记的就是这个序列化方式的一致性 也可以改里面默认的序列化方式

     @Bean
    public RedisTemplate<String, Integer> redisTemplate(RedisConnectionFactory factory) {
        RedisTemplate<String, Integer> redisTemplate = new RedisTemplate<>();
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        redisTemplate.setValueSerializer(new JdkSerializationRedisSerializer());
        redisTemplate.setExposeConnection(true);
        redisTemplate.setConnectionFactory(factory);
        redisTemplate.afterPropertiesSet();
        return redisTemplate;
    }
使用RedisTemplate进行增删

封装一个类用于自动对缓存进行操作 还有其它操作 这个demo用不到就没有做 这里只有提高和删除两种操作

@Component
public class MyRedisCacheManager {
    @Autowired
    private RedisTemplate redisTemplate;
    /* 插入数据或者更新数据 */
    public void insert(String key, Object value, long timeout, TimeUnit timeUnit) {
        if (StringUtils.isBlank(key) || !ObjectUtils.anyNotNull(value)) {
            return;
        }
        if (timeout == 0) {
            redisTemplate.opsForValue().set(key, value);
        } else {
            redisTemplate.opsForValue().set(key, value, timeout, timeUnit);
        }
    }
    public void delete(String key) {
        redisTemplate.opsForValue().getOperations().delete(key);
    }
}

这里是整个项目的源码 可供参考GITHUB

微信公众号不定时更新中


本文来自电脑杂谈,转载请注明本文网址:
http://www.pc-fly.com/a/jisuanjixue/article-120834-1.html

    相关阅读
      发表评论  请自觉遵守互联网相关的政策法规,严禁发布、暴力、反动的言论

      热点图片
      拼命载入中...