猿问

有没有办法让 eclipselink/JPA 使用 redis 来保存和检索持久单元级缓存?

我正在设置一个集群环境,并且希望在整个集群节点中复制 JPA 的二级缓存。我使用 eclipselink 作为 JPA 提供程序,使用 redis 进行缓存管理。



慕工程0101907
浏览 87回答 1
1回答

ITMISS

没有官方或社区项目支持它。但是,您可以实现自己的CacheInterceptor。例如。import org.eclipse.persistence.descriptors.ClassDescriptor;import org.eclipse.persistence.internal.identitymaps.CacheKey;import org.eclipse.persistence.internal.identitymaps.IdentityMap;import org.eclipse.persistence.internal.sessions.AbstractSession;import org.eclipse.persistence.sessions.interceptors.CacheInterceptor;import org.eclipse.persistence.sessions.interceptors.CacheKeyInterceptor;import java.util.Map;public class MyRedisCacheInterceptor extends CacheInterceptor {    private final MyCacheProvider cacheSupport;    private final String cacheName;    public DefaultCacheInterceptor(IdentityMap targetIdentityMap, AbstractSession interceptedSession,                                   String cacheName, DefaultCacheSupport cacheSupport) {        super(targetIdentityMap, interceptedSession);        this.cacheSupport = cacheSupport;        this.cacheName = cacheName;    }    @Override    public Object clone() {        return null;    }    @Override    protected CacheKeyInterceptor createCacheKeyInterceptor(CacheKey wrappedCacheKey) {        final long longKey = (long) wrappedCacheKey.getKey();        CacheKeyInterceptor newKey = new CacheKeyInterceptor(wrappedCacheKey) {            @Override            public Object getObject() {                return cacheSupport.getOrCreateCache(cacheName).get(longKey);            }            @Override            public void setObject(Object object) {                cacheSupport.getOrCreateCache(cacheName).put(longKey, object);            }        };        return newKey;    }    @Override    public boolean containsKey(Object primaryKey) {        return cacheSupport.getOrCreateCache(cacheName).containsKey(primaryKey);    }    @Override    public Map<Object, Object> getAllFromIdentityMapWithEntityPK(Object[] pkList, ClassDescriptor descriptor, AbstractSession session) {        return null;    }    @Override    public Map<Object, CacheKey> getAllCacheKeysFromIdentityMapWithEntityPK(Object[] pkList, ClassDescriptor descriptor, AbstractSession session) {        return null;    }    @Override    public void release() {    }}
随时随地看视频慕课网APP

相关分类

Java
我要回答