簡體   English   中英

監視elipselink緩存L2,L1

[英]monitoring elipselink cache L2,L1

是否有工具或編程方法來監視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();
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM