EhCache +休眠

发布于 2024-11-24 17:11:10 字数 2809 浏览 1 评论 0原文

我有以下问题: 我有一个查询返回 35 个结果,我想保留在二级缓存中:

public List<Product> getAllProducts() {
        Session session = this.sessionfactory.getCurrentSession();
        String queryString = "from com.ewave.upromotions.objects.Product product where product.active=:active";
        Query query = session.createQuery(queryString); 
        query.setBoolean("active", true);
        query.setCacheable(true);
        query.setCacheRegion("productCache");
        List<Product> products =query.list();
        return products;
    }

我的对象如下:

@Entity
@Table(name="products",schema="test11")
@Cacheable
@Cache( usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
public class Product implements Serializable {

//all setters and getters ommited:

}

我的 ehcache.xml 文件位于 /src/ 目录中:

<ehcache>  
    <diskStore path="java.io.tmpdir"/>  
    <defaultCache maxElementsInMemory="10000"  
        eternal="false"  
        timeToIdleSeconds="120"  
        timeToLiveSeconds="120"  
        overflowToDisk="false"/>  
    <cache name="hibernate.test.org.hibernate.cache.UpdateTimestampsCache"   
        maxElementsInMemory="10000"  
        eternal="false"  
        timeToIdleSeconds="120"  
        timeToLiveSeconds="120"  
        overflowToDisk="true"/>  
    <cache name="hibernate.test.org.hibernate.cache.StandardQueryCache"   
        maxElementsInMemory="10000"  
        eternal="false"  
        timeToIdleSeconds="120"  
        timeToLiveSeconds="120"  
        overflowToDisk="true"/>
     <cache name="com.vanilla.objects.Product"
        maxElementsInMemory="300"
        eternal="false"
        overflowToDisk="false"
        timeToIdleSeconds="600"  
        timeToLiveSeconds="600"  
        />  
       <cache name="productCache" 
       maxElementsInMemory="100" 
       eternal="true" 
       overflowToDisk="false" />

</ehcache>  

我的 Hibernate 配置是:

<props>
    <prop key="hibernate.dialect">org.hibernate.dialect.OracleDialect</prop> 
    <prop key="hibernate.cache.use_second_level_cache">true</prop> 
     <prop key="hibernate.show_sql">true</prop> 
  <prop key="hibernate.cache.use_query_cache">true</prop> 
  <prop key="hibernate.cache.provider_class">net.sf.ehcache.hibernate.SingletonEhCacheProvider</prop>
  <prop key="hibernate.connection.release_mode">after_transaction</prop>
  <prop key="hibernate.cache.provider_configuration_file_resource_path">/ehcache.xml</prop>
  </props>

我的问题如下:第一次打开页面时,我看到 select 带来了所有 35 个结果:

当我刷新页面时,我看到的不是从缓存中取出 35 个对象,而是 35 个 select 语句,这些语句通过 id 逐一查询对象。

怎么了?

I have the following problem:
I have a query which return me 35 results and I would like to keep in second level cache:

public List<Product> getAllProducts() {
        Session session = this.sessionfactory.getCurrentSession();
        String queryString = "from com.ewave.upromotions.objects.Product product where product.active=:active";
        Query query = session.createQuery(queryString); 
        query.setBoolean("active", true);
        query.setCacheable(true);
        query.setCacheRegion("productCache");
        List<Product> products =query.list();
        return products;
    }

My object is the following:

@Entity
@Table(name="products",schema="test11")
@Cacheable
@Cache( usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
public class Product implements Serializable {

//all setters and getters ommited:

}

my ehcache.xml file is in /src/ directory:

<ehcache>  
    <diskStore path="java.io.tmpdir"/>  
    <defaultCache maxElementsInMemory="10000"  
        eternal="false"  
        timeToIdleSeconds="120"  
        timeToLiveSeconds="120"  
        overflowToDisk="false"/>  
    <cache name="hibernate.test.org.hibernate.cache.UpdateTimestampsCache"   
        maxElementsInMemory="10000"  
        eternal="false"  
        timeToIdleSeconds="120"  
        timeToLiveSeconds="120"  
        overflowToDisk="true"/>  
    <cache name="hibernate.test.org.hibernate.cache.StandardQueryCache"   
        maxElementsInMemory="10000"  
        eternal="false"  
        timeToIdleSeconds="120"  
        timeToLiveSeconds="120"  
        overflowToDisk="true"/>
     <cache name="com.vanilla.objects.Product"
        maxElementsInMemory="300"
        eternal="false"
        overflowToDisk="false"
        timeToIdleSeconds="600"  
        timeToLiveSeconds="600"  
        />  
       <cache name="productCache" 
       maxElementsInMemory="100" 
       eternal="true" 
       overflowToDisk="false" />

</ehcache>  

and my Hibernate configuration is:

<props>
    <prop key="hibernate.dialect">org.hibernate.dialect.OracleDialect</prop> 
    <prop key="hibernate.cache.use_second_level_cache">true</prop> 
     <prop key="hibernate.show_sql">true</prop> 
  <prop key="hibernate.cache.use_query_cache">true</prop> 
  <prop key="hibernate.cache.provider_class">net.sf.ehcache.hibernate.SingletonEhCacheProvider</prop>
  <prop key="hibernate.connection.release_mode">after_transaction</prop>
  <prop key="hibernate.cache.provider_configuration_file_resource_path">/ehcache.xml</prop>
  </props>

My problem is the following: when bringing page for first time I see the select which brings all 35 results:

when I refresh page, instead of bringing 35 objects from cache I see 35 select statements which queries objects by id one by one.

What's wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

爱本泡沫多脆弱 2024-12-01 17:11:10

当查询被缓存但对象没有被缓存时,这种情况就发生在我身上。在您的示例代码中,我看到该查询引用了 com.ewave.upromotions.objects.Product,但您为 com.vanilla.objects.Product 定义了缓存区域。并且您没有向 Product 类提供任何缓存区域。因此,我建议您显式指定 Product 的缓存区域并在缓存配置中定义它。首先,我会尝试使用相同的区域进行查询和对象,只是为了看看它是否有效,然后尝试单独的配置。

It was happening to me when query was cached but objects were not. In you sample code I see that query refers to com.ewave.upromotions.objects.Product but you have cache region defined for com.vanilla.objects.Product. And you didn't provide any cache region to the Product class. So I'd suggest that you explicitly specify cache region for Product and define it in cache configuration. To start, I would try the same region for query and object, just to see if it works, and then try separate configurations.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文