标签: shared-cache

JPA在WildFly中共享缓存/二级缓存

我正在使用WildFly 8.1,所以JPA 2.1和Hibernate 4.3.5

我想在WildFly中使用JPA共享缓存/二级缓存

我遵循WildFly文档:https://docs.jboss.org/author/display/WFLY8/JPA+Reference+Guide#JPAReferenceGuide-UsingtheInfinispansecondlevelcache

这是我的persitience.xml:

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1" xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
  <persistence-unit name="myAppPU" transaction-type="JTA">
    <jta-data-source>java:/jdbc/myAppDS</jta-data-source>
    <exclude-unlisted-classes>false</exclude-unlisted-classes>
    <shared-cache-mode>ENABLE_SELECTIVE</shared-cache-mode>
    <properties>
      <property name="hibernate.show_sql" value="true"/>
      <property name="hibernate.format_sql" value="true"/>
      <property name="org.hibernate.flushMode" value="MANUAL"/>
      <property name="hibernate.cache.use_second_level_cache" value="true"/>
    </properties>
  </persistence-unit>
</persistence>
Run Code Online (Sandbox Code Playgroud)

我将属性hibernate.cache.use_second_level_cache设置为true并将shared-cache-mode设置为ENABLE_SELECTIVE

我希望缓存的实体(@Entity)使用@Cacheable(true)进行注释,如下所示:

@Entity
@Cacheable(true)
public class Tooltip implements Serializable {
  @Id
  private String path ;
  private String description ;
  private Boolean rendered ;
  //...
}
Run Code Online (Sandbox Code Playgroud)

但每次我访问一个网页时,hibernate都会生成大量的select来获取我指示为@Cacheable(true)的所有实体,即使我已经访问过该页面(在同一个会话中).

所以似乎共享缓存/二级缓存不起作用

我错过了什么?


谢谢hwellmann

我试图在persitence.xml中将hibernate.cache.use_query_cache设置为true

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1" xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
  <persistence-unit …
Run Code Online (Sandbox Code Playgroud)

hibernate jpa second-level-cache shared-cache wildfly

6
推荐指数
1
解决办法
3319
查看次数

缓存关闭导致运行测试套件时出现异常

我遇到了与此问题中描述的类似的问题。

我有一个在开发环境中运行良好的测试套件。在 Bitbucket Pipelines 中执行时,其中一项测试失败,但出现以下异常:

org.springframework.dao.InvalidDataAccessApiUsageException: Cache[model.Role] is closed; nested exception is java.lang.IllegalStateException: Cache[model.Role] is closed
    at org.springframework.orm.jpa.EntityManagerFactoryUtils.convertJpaAccessExceptionIfPossible(EntityManagerFactoryUtils.java:364)
    at org.springframework.orm.jpa.vendor.HibernateJpaDialect.translateExceptionIfPossible(HibernateJpaDialect.java:225)
    at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.translateExceptionIfPossible(AbstractEntityManagerFactoryBean.java:527)
    at org.springframework.dao.support.ChainedPersistenceExceptionTranslator.translateExceptionIfPossible(ChainedPersistenceExceptionTranslator.java:61)
    at org.springframework.dao.support.DataAccessUtils.translateIfNecessary(DataAccessUtils.java:242)
   ....
Run Code Online (Sandbox Code Playgroud)

我想尝试接受的解决方案,但我不知道如何将其应用到我的项目中。第二种解决方案依赖于ehcache.xml 文件。我没有这个文件,一切都在JavaConfig中配置。如何在 JavaConfig 中采用EhCache + JCache (JSR-107) 的建议解决方案?

我的缓存配置:

@Configuration
@EnableCaching
public class CacheConfig {

    private final javax.cache.configuration.Configuration<Object, Object> jcacheConfiguration =
            Eh107Configuration.fromEhcacheCacheConfiguration(CacheConfigurationBuilder
                    .newCacheConfigurationBuilder(Object.class, Object.class,
                            ResourcePoolsBuilder.newResourcePoolsBuilder()
                                    .heap(100, EntryUnit.ENTRIES))
                    .withExpiry(ExpiryPolicyBuilder.timeToLiveExpiration(Duration.ofSeconds(60)))
                    .build());

    @Bean
    public JCacheManagerCustomizer cacheManagerCustomizer() {
        return cm -> {
            createIfNotExists(cm, "model.Role");
            createIfNotExists(cm, "model.User.roles");
            // ...
        };
    }

    private …
Run Code Online (Sandbox Code Playgroud)

ehcache spring-test shared-cache jcache

6
推荐指数
1
解决办法
5136
查看次数

SQLite SharedCache MultiThread读取

我正在Ubuntu虚拟机上开发一个多线程sqlite数据库应用程序,该虚拟机分配了4个处理器.我使用的是sqlite版本3.7.13.我创建了一个测试来验证多个线程/连接可以同时从数据库中读取.

我有两个可执行文件.第一个可执行文件只创建一个数据库,在该数据库中创建1个表,在该表中插入50个项,然后关闭数据库.这根本不涉及任何多线程,只是为了在数据库中提供条目.

第二个可执行文件创建多个线程以从数据库中读取并等待它们完成并记录所有线程完成所花费的时间.每个线程执行以下操作: - 使用sqlite_open_v2()创建数据库连接,以便每个线程都有自己的连接到从第一个可执行文件创建的数据库 - 在一个数据库表上执行100000 SELECTS(每个选择查询一行中的一行)表) - 关闭数据库连接

当我在每个线程中使用指定为sqlite_open_v2的标志的SQLITE_OPEN_READWRITE运行此测试时,我得到以下结果以便执行所有查询的总时间:

1线程 - 0.65秒2线程 - 0.70秒3线程 - 0.76秒4线程 - 0.91秒5线程 - 1.10秒6线程 - 1.28秒7线程 - 1.57秒8线程 - 1.78秒

当我添加线程时,这些结果与预期的时间增加一样(可能来自线程和其他原因之间的上下文切换),这意味着读取基本上是并行完成的.

但是,当我使用SQLITE_OPEN_READWRITE |运行相同的测试时 对于标志的SQLITE_OPEN_SHAREDCACHE,我得到以下结果:

1线程 - 0.67秒2线程 - 2.43秒3线程 - 4.81秒4线程 - 6.60秒5线程 - 8.03秒6线程 - 9.41秒7线程 - 11.17秒8线程 - 12.79秒

从这些结果看来,共享缓存模式中的某些东西似乎阻止了同时在数据库中发生多次读取.我已经验证了并行运行的线程确实不同(线程4读取,线程8读取,线程2读取等,而不是线程1执行其所有读取,线程2执行其所有读取,线程3执行其所有读取,等等.).但是,似乎每个单独事务的读取都是串行完成的,或者其他东西正在减慢共享缓存中的数据库速度.

为什么我在共享缓存模式下添加线程时看到如此高的增长,而不是没有呢?有没有办法解决这个问题,仍然使用共享缓存模式?

谢谢你的帮助.非常感谢.

sqlite multithreading shared-cache

5
推荐指数
1
解决办法
1682
查看次数

Windows Azure角色缓存与共享缓存

我们在Azure中有一个网站,我们希望在网站上缓存内容.将更新内容的应用程序将在Azure之外.我们让这个场景与共享缓存一起使用.但是,共享缓存被认为是一项遗留功能,因此我们希望了解其他解决方案,包括使用角色内缓存.缓存的内容非常小,不应超过1 MB,并且将由C#代码使用.

我们可以使用辅助角色在Web角色或专用缓存中使用共存缓存.

我们使用角色内缓存的问题是:

  • 如何从外部应用程序更新共存缓存?

  • 如果有办法从外部应用程序更新共存缓存,可以使用缓存通知使所有共存缓存节点无效,对吗?

  • 我们现在使用超小型Web角色实例 - 我们是否需要升级到小型/中型实例?

  • 针对我们的场景,专用缓存更好吗?

    提前致谢.

caching azure shared-cache azure-worker-roles azure-web-roles

1
推荐指数
1
解决办法
1771
查看次数