如果 Couchbase 连接失败,如何在运行时禁用缓存?

How to disable Caching at runtime if Couchbase connection failed?

我有一个与此处询问的类似问题 - How to disable Redis Caching at run time if redis connection failed. My application is using @Cacheable 在大多数 database/static 资源调用的服务层。

缓存由 Couchbase 支持,每当应用程序无法连接时 Couchbase 节点应用程序就会关闭。这是我们没有预料到的,我们希望只要连接失败就应该从源系统提供数据。

我们尝试实施 CacheErrorHandler 但它没有按预期工作,因为 我们想执行进行服务调用的实际方法和 return 响应 而不是记录缓存失败,基本上绕过缓存,一旦 Couchbase 节点启动或建立连接,就从缓存中获取数据。

知道我们如何实现它吗?

感谢@Daniel Bickler 的建议,下面是我参考@John Blum answer.

编写的实现

CouchbaseCustomCacheManager:

import java.util.Map;
import org.springframework.cache.Cache;
import com.couchbase.client.spring.cache.CacheBuilder;
import com.couchbase.client.spring.cache.CouchbaseCacheManager;

public class CouchbaseCustomCacheManager extends CouchbaseCacheManager {

    public CouchbaseCustomCacheManager(
            final Map<String, CacheBuilder> initialCaches) {
        super(initialCaches);
    }

    @Override
    public Cache getCache(String name) {
        return new CouchbaseCacheWrapper(super.getCache(name));
    }

    protected static class CouchbaseCacheWrapper implements Cache {

        private final Cache delegate;

        public CouchbaseCacheWrapper(Cache couchbaseCache) {
            this.delegate = couchbaseCache;
        }

        @Override
        public String getName() {
            try {
                return delegate.getName();
            } catch (Exception e) {
                return null;
            }
        }

        @Override
        public Object getNativeCache() {
            try {
                return delegate.getNativeCache();
            } catch (Exception e) {
                return null;
            }
        }

        @Override
        public ValueWrapper get(Object key) {
            try {
                return delegate.get(key);
            } catch (Exception e) {
                return null;
            }
        }

        @Override
        public <T> T get(Object key, Class<T> type) {
            try {
                return delegate.get(key, type);
            } catch (Exception e) {
                return null;
            }
        }

        @Override
        public void put(Object key, Object value) {
            try {
                delegate.put(key, value);
            } catch (Exception e) {
                try {
                    handleErrors(e);
                } catch (Exception e1) {
                }
            }
        }

        @Override
        public ValueWrapper putIfAbsent(Object key, Object value) {
            try {
                return delegate.putIfAbsent(key, value);
            } catch (Exception e) {
                return null;
            }
        }

        @Override
        public void evict(Object key) {
            try {
                delegate.evict(key);
            } catch (Exception e) {
                try {
                    handleErrors(e);
                } catch (Exception e1) {
                }
            }
        }

        @Override
        public void clear() {
            try {
                delegate.clear();
            } catch (Exception e) {
                try {
                    handleErrors(e);
                } catch (Exception e1) {
                }
            }
        }

        protected <T> T handleErrors(Exception e) throws Exception {
            if (e instanceof Exception) {
                return null;
            } else {
                throw e;
            }
        }
    }

}

并将其用作:

@Bean
public CacheManager cacheManager() {
    final Map<String, CacheBuilder> cache = new HashMap<>();
    for (final String appCache : "127.0.0.1,127.0.0.2,127.0.0.3".split(",")) {
     cache.put(appCache, CacheBuilder.newInstance(CouchbaseCluster.create().openBucket(
                    "default", "")));
    }
    return new CouchbaseCustomCacheManager(cache);
}