【问题标题】:monitoring elipselink cache L2,L1监控 elipselink 缓存 L2,L1
【发布时间】:2015-06-29 14:26:21
【问题描述】:

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

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

【问题讨论】:

    标签: java jpa caching eclipselink


    【解决方案1】:

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

    【讨论】:

    • ouw.getCloneMapping 总是返回空地图。是否需要进一步配置。请注意,所有与缓存相关的配置都保持默认,这表明缓存工作。
    • 澄清一下——我们谈论的是持久化上下文还是共享缓存?
    • 对不起,我的意思是共享缓存,问题已编辑。我将尝试浏览与 Session 相关的课程以找到实体,如果有办法请告诉我谢谢@wypieprz
    • 请@wypieprz 重新更新答案,必须解包 ServerSession 而不是 unitofwork 才能访问 L2 共享缓存。再次感谢
    • 嗯,你的意思是这样的:((org.eclipse.persistence.internal.sessions.IdentityMapAccessor)jem.unwrap(ServerSession.class).getIdentityMapAccessor()).getIdentityMap(Client.class).getSize()
    猜你喜欢
    • 2017-09-24
    • 2010-11-09
    • 2020-05-19
    • 1970-01-01
    • 2021-11-27
    • 2013-01-20
    • 1970-01-01
    • 2011-04-11
    • 1970-01-01
    相关资源
    最近更新 更多