亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

有沒有辦法讓 eclipselink/JPA 使用 redis 來保存和檢索持久單元級緩存?

有沒有辦法讓 eclipselink/JPA 使用 redis 來保存和檢索持久單元級緩存?

慕工程0101907 2023-07-19 16:08:13
我正在設置一個集群環境,并且希望在整個集群節點中復制 JPA 的二級緩存。我使用 eclipselink 作為 JPA 提供程序,使用 redis 進行緩存管理。
查看完整描述

1 回答

?
ITMISS

TA貢獻1871條經驗 獲得超8個贊

沒有官方或社區項目支持它。

但是,您可以實現自己的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() {

? ? }

}


查看完整回答
反對 回復 2023-07-19
  • 1 回答
  • 0 關注
  • 122 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號