将Redis设置为可选

huangapple go评论81阅读模式
英文:

Make Redis as optional

问题

我正在使用 Spring Boot 与 Redis。Redis 正在作为 Docker 容器运行。

spring.cache.type=redis
spring.redis.host=localhost
spring.redis.port=6379

Redis 是一个内存数据库,如果它根据键在 Redis 中找到数据,就从 Redis 中检索数据,否则将会进行实际的数据库调用。当 Redis 运行时,代码正常工作。但是有时候由于某种原因,如果 Redis 宕机,我会得到异常 RedisConnectionException: 无法连接到 localhost:6379

我想要使它变成可选的。即使 Redis 宕机,代码也应该正常工作,通过调用实际的数据库数据(使用 SQL 服务存储库)。

是否有任何方法可以使 Redis 调用变为可选的。

@Cacheable(cacheNames = CACHE_USER_DETAILS)
public User getUserDetails(String username) {
    // 从 SQL 服务器通过存储库调用数据。
}
英文:

I am using spring boot with Redis.Redis is running as Docker container

spring.cache.type=redis
spring.redis.host=localhost
spring.redis.port=6379 

Redis is a memory DB, if it finds data in Redis based on the key, it retrieved from Redis otherwise go into actual db call.
when Redis is running, code works fine. but sometimes for any reason, if Redis is down, I am getting exception RedisConnectionException: Unable to connect to localhost:6379

I want to make it optional. if it gets down, code should work as it is, by calling actual DB data(sql service repositories).

is there any way to make Redis call as optional.

if running, work with Redis, 
if down, can to actual DB without exception.

my code

@Cacheable(cacheNames = CACHE_USER_DETAILS)
	public User getUserDetails(String username) {
      //call data from sql serever via repositories.
}

答案1

得分: 1

因为您正在使用@Spring的抽象缓存(@Cachable)进行缓存,所以请编写一个实现CacheErrorHandler接口的类。您可以重写它的方法并进行您的逻辑处理(例如记录错误)。

如果Redis不可用,将自动执行getUserDetails(String username)。

请查看这个问题

希望这能帮助您。

英文:

Since you are using @Cachaeble spring abstraction for caching, Write a class which implements CacheErrorHandler interface.You can override it's methods and do your logic side(For example log the error).

If the redis is down, getUserDetails(String username ) will be done automatically.

Check out this question

Hope this helps.

答案2

得分: 1

我通过创建自己的错误处理程序并覆盖Spring缓存错误处理程序来解决了这个问题

package com.crif.credity.tenancy;

import org.springframework.cache.Cache;
import org.springframework.cache.annotation.CachingConfigurerSupport;
import org.springframework.cache.interceptor.CacheErrorHandler;
import org.springframework.cache.interceptor.KeyGenerator;
import org.springframework.context.annotation.Configuration;

import com.crif.credity.config.CredityKeyGenerator;

import lombok.extern.slf4j.Slf4j;

@Configuration
public class CachingConfiguration extends CachingConfigurerSupport {

    @Override
    public KeyGenerator keyGenerator() {
        return new CredityKeyGenerator();
    }

    @Override
    public CacheErrorHandler errorHandler() {
        return new CredityRedisCacheErrorHandler();
    }

    @Slf4j
    public static class CredityRedisCacheErrorHandler implements CacheErrorHandler {

        @Override
        public void handleCacheGetError(RuntimeException exception, Cache cache, Object key) {
            log.info("无法从缓存 " + cache.getName() + " 获取数据:" + exception.getMessage());
        }

        @Override
        public void handleCachePutError(RuntimeException exception, Cache cache, Object key, Object value) {
            log.info("无法将数据放入缓存 " + cache.getName() + " :" + exception.getMessage());
        }

        @Override
        public void handleCacheEvictError(RuntimeException exception, Cache cache, Object key) {
            log.info("无法从缓存 " + cache.getName() + " 驱逐数据:" + exception.getMessage());
        }

        @Override
        public void handleCacheClearError(RuntimeException exception, Cache cache) {
            log.info("无法清除缓存 " + cache.getName() + " :" + exception.getMessage());
        }
    }
}
英文:

I fixed the problem by creating own error handler and overried the spring cache error handler

package com.crif.credity.tenancy;
import org.springframework.cache.Cache;
import org.springframework.cache.annotation.CachingConfigurerSupport;
import org.springframework.cache.interceptor.CacheErrorHandler;
import org.springframework.cache.interceptor.KeyGenerator;
import org.springframework.context.annotation.Configuration;
import com.crif.credity.config.CredityKeyGenerator;
import lombok.extern.slf4j.Slf4j;
@Configuration
public class CachingConfiguration extends CachingConfigurerSupport {
@Override
public KeyGenerator keyGenerator() {
return new CredityKeyGenerator();
}
@Override
public CacheErrorHandler errorHandler() {
return new CredityRedisCacheErrorHandler();
}
@Slf4j
public static class CredityRedisCacheErrorHandler implements CacheErrorHandler {
@Override
public void handleCacheGetError(RuntimeException exception, Cache cache, Object key) {
log.info("Unable to get from cache " + cache.getName() + " : " + exception.getMessage());
}
@Override
public void handleCachePutError(RuntimeException exception, Cache cache, Object key, Object value) {
log.info("Unable to put into cache " + cache.getName() + " : " + exception.getMessage());
}
@Override
public void handleCacheEvictError(RuntimeException exception, Cache cache, Object key) {
log.info("Unable to evict from cache " + cache.getName() + " : " + exception.getMessage());
}
@Override
public void handleCacheClearError(RuntimeException exception, Cache cache) {
log.info("Unable to clean cache " + cache.getName() + " : " + exception.getMessage());
}
}
}

huangapple
  • 本文由 发表于 2020年9月18日 17:32:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/63953089.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定