给第三方登录时用的数据库表-user_connection-与-auth_token-添加-redis-cache

技术文档网 2021-04-13
spring:
  # 设置缓存为 Redis
  cache:
    type: redis
  # redis
  redis:
    host: 192.168.88.88
    port: 6379
    password:
    database: 0
    # 连接超时的时间
    timeout: 10000
    # redis-lettuce-pool
    lettuce:
      shutdown-timeout: PT500S
      pool:
        max-active: 8
        max-wait: PT10S
        max-idle: 8
        min-idle: 1

ums:
  # 第三方授权登录用户信息(user_connection) 与 auth_token 表的缓存设置
  cache:
    # redisCacheManager 设置, 默认实现: 对查询结果 null 值进行缓存, 添加时更新缓存 null 值.
    redis:
      # 是否开启缓存, 默认 false
      open: true
      # 是否使用 spring IOC 容器中的 RedisConnectionFactory, 默认: false
      # 如果使用 spring IOC 容器中的 RedisConnectionFactory,则要注意 cache.database-index 要与 spring.redis.database 一样。
      use-ioc-redis-connection-factory: true
      cache:
        # redis cache 存放的 database index, 默认: 0
        database-index: 1
        # 设置缓存管理器管理的缓存的默认过期时间, 默认: 200s
        default-expire-time: PT200S
        # cache ttl 。使用 0 声明一个永久的缓存。 默认: 180, 单位: 秒<br>
        # 取缓存时间的 20% 作为动态的随机变量上下浮动, 防止同时缓存失效而缓存击穿
        entry-ttl: PT180S
        # Names of the default caches to consider for caching operations defined in the annotated class.
        # 此设置不对 user_connection 与 auth_token 使用的缓存名称(UCC/UCHC/UCHACC)产生影响.
        cache-names:
          - cacheName
  • 依赖
    <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-redis</artifactId>
    </dependency>
    <!-- 为了解决 ClassNotFoundException: org.apache.commons.pool2.impl.GenericObjectPoolConfig -->
    <dependency>
      <groupId>org.apache.commons</groupId>
      <artifactId>commons-pool2</artifactId>
      <version>2.8.0</version>
    </dependency>
    
  • @Cacheable 替换默认的操作异常处理, 下面的自定义实现逻辑跟默认实现逻辑是一样的 ```java package demo.cache;

import lombok.extern.slf4j.Slf4j; import org.springframework.cache.Cache; import org.springframework.cache.annotation.CachingConfigurerSupport; import org.springframework.cache.interceptor.CacheErrorHandler; import org.springframework.lang.NonNull; import org.springframework.stereotype.Component;

/**

  • Cacheable 操作自定义异常处理: 注入 IOC 容器即可自动注入 {@link CachingConfigurerSupport}, 当然也可自定义 {@link CachingConfigurerSupport}.

  • 异常处理在日志中打印出错误信息,但是放行,保证redis服务器出现连接等问题的时候不影响程序的正常运行,使得能够出问题时不用缓存

  • @author zyw

  • @version V2.0 Created by 2020/10/18 12:06

  • / @Component @Slf4j public class DemoCacheErrorHandler implements CacheErrorHandler {

    @Override public void handleCacheGetError(@NonNull RuntimeException e, @NonNull Cache cache, @NonNull Object key) {

      log.error("redis异常:cacheName={}, key={}", cache.getName(), key, e);
    

    }

    @Override public void handleCachePutError(@NonNull RuntimeException e, @NonNull Cache cache, @NonNull Object key, Object value) {

      log.error("redis异常:cacheName={}, key={}", cache.getName(), key, e);
    

    }

    @Override public void handleCacheEvictError(@NonNull RuntimeException e, @NonNull Cache cache, @NonNull Object key) {

      log.error("redis异常:cacheName={}, key={}", cache.getName(), key, e);
    

    }

    @Override public void handleCacheClearError(@NonNull RuntimeException e, @NonNull Cache cache) {

      log.error("redis异常:cacheName={}, ", cache.getName(), e);
    

    } } ```

相关文章

  1. 基于-SLF4J-MDC-机制的日志链路追踪配置属性

    ums: # ================ 基于 SLF4J MDC 机制的日志链路追踪配置属性 ================ mdc: # 是否支持基于 SLF4J MDC

  2. ajax-跨域访问

    ajax 跨域访问 &lt;!DOCTYPE html&gt; &lt;html xmlns:th="http://www.w3.org/1999/xhtml"&gt; &lt;head&gt;

  3. 给第三方登录时用的数据库表-user_connection-与-auth_token-添加-redis-cache

    spring: # 设置缓存为 Redis cache: type: redis # redis redis: host: 192.168.88.88 port

  4. Java动态代理

    Jdk动态代理 通过InvocationHandler和Proxy针对实现了接口的类进行动态代理,即必须有相应的接口 应用 public class TestProxy { public

  5. Java读取classpath中的文件

    public void init() { try { //URL url = Thread.currentThread().getContextClassLo

随机推荐

  1. 基于-SLF4J-MDC-机制的日志链路追踪配置属性

    ums: # ================ 基于 SLF4J MDC 机制的日志链路追踪配置属性 ================ mdc: # 是否支持基于 SLF4J MDC

  2. ajax-跨域访问

    ajax 跨域访问 &lt;!DOCTYPE html&gt; &lt;html xmlns:th="http://www.w3.org/1999/xhtml"&gt; &lt;head&gt;

  3. 给第三方登录时用的数据库表-user_connection-与-auth_token-添加-redis-cache

    spring: # 设置缓存为 Redis cache: type: redis # redis redis: host: 192.168.88.88 port

  4. Java动态代理

    Jdk动态代理 通过InvocationHandler和Proxy针对实现了接口的类进行动态代理,即必须有相应的接口 应用 public class TestProxy { public

  5. Java读取classpath中的文件

    public void init() { try { //URL url = Thread.currentThread().getContextClassLo