java - 监控elipselink缓存L2,L1

标签 java jpa caching eclipselink

是否有工具或程序化方法来监控eclipse链接的一级,二级缓存。我的目标是知道为某个类缓存的实体数量。 这是我找到的一些链接,但它们还不够:

http://www.eclipse.org/eclipselink/documentation/2.5/solutions/performance002.htm https://wiki.eclipse.org/EclipseLink/Examples/JPA/Monitoring

最佳答案

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<Object, Object> 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();
}

关于java - 监控elipselink缓存L2,L1,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29788936/

相关文章:

java - hibernate 恩弗斯 : Better way to add multiple Projections

java - Hibernate JPA 序列(非 Id)

php - Google Chrome 对缓存的审核

html - Chrome下无法查看缓存文件

java - Spring ControllerAdvice 不会因某些异常而触发其他异常

Java 线程不能与 JTextArea 一起正常工作

java - 在 InheritanceType.JOINED 中使用 AttributeConverter 映射 MySQL ENUM 在 Hibernate 5.3.6 (JPA 2.1+) : No enum constant 上抛出 IllegalArgumentException

caching - Post/Redirect/Get 上的整页重新加载忽略缓存控制

java - 使用数组从 json 文件 java 获取 api 参数

java - 通过值的索引获取Guava Multimap的key