java l1 l2缓存_java – 监视elipselink缓存L2,L1

JPA没有指定这样的功能,但你可以使用EclipseLink内部组件来实现,例如:

L1(事务缓存,持久化上下文)

import org.eclipse.persistence.jpa.JpaEntityManager;

import org.eclipse.persistence.internal.sessions.UnitOfWorkImpl;

...

JpaEntityManager jem = em.unwrap(JpaEntityManager.class);

UnitOfWorkImpl ouw = jem.unwrap(UnitOfWorkImpl.class);

...

long count = countCachedEntitiesL1(clazz);

和相应的方法:

// Java 7

public long countCachedEntitiesL1(Class clazz) {

long count = 0;

for (Map.Entry entity : ouw.getCloneMapping().entrySet()) {

if (entity.getKey().getClass().equals(clazz)) {

count++;

}

}

return count;

}

// Java 8

public long countCachedEntitiesL1(Class clazz) {

return ouw.getCloneMapping().keySet().stream()

.filter(entity -> entity.getClass().equals(clazz))

.count();

}

L2(共享缓存)

import org.eclipse.persistence.jpa.JpaEntityManager;

import org.eclipse.persistence.sessions.server.ServerSession;

import org.eclipse.persistence.internal.sessions.IdentityMapAccessor;

...

JpaEntityManager jem = em.unwrap(JpaEntityManager.class);

ServerSession ss = jem.unwrap(ServerSession.class);

IdentityMapAccessor ima = (IdentityMapAccessor) ss.getIdentityMapAccessor();

...

int count = countCachedEntitiesL2(clazz);

和相应的方法:

public int countCachedEntitiesL2(Class clazz) {

return ima.getIdentityMap(clazz).getSize();

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值