2011-07-17 38 views
6

Tôi có vấn đề sau đây: Tôi đã một truy vấn mà trả lại cho tôi 35 kết quả và tôi muốn giữ trong bộ nhớ cache mức độ thứ hai:ehcache + ngủ đông

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

đối tượng của tôi là như sau:

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

//all setters and getters ommited: 

} 

tập tin ehcache.xml của tôi là trong/src/thư mục:

<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> 

và cấu hình Hibernate của tôi là:

<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> 

Vấn đề của tôi là như sau: khi mang trang cho lần đầu tiên tôi nhìn thấy các lựa chọn mang lại tất cả 35 kết quả:

khi tôi làm mới trang, thay vì đưa 35 đối tượng từ bộ nhớ cache Tôi thấy 35 chọn các câu lệnh truy vấn các đối tượng theo từng cái một.

Có vấn đề gì?

Trả lời

5

Điều đó xảy ra với tôi khi truy vấn được lưu trong bộ nhớ cache nhưng đối tượng thì không. Trong mã mẫu, tôi thấy truy vấn đó đề cập đến com.ewave.upromotions.objects.Product nhưng bạn có vùng bộ nhớ cache được xác định cho com.vanilla.objects.Product. Và bạn không cung cấp bất kỳ vùng bộ nhớ cache nào cho lớp Product. Vì vậy, tôi khuyên bạn nên chỉ định rõ ràng vùng bộ nhớ cache cho Product và xác định nó trong cấu hình bộ nhớ cache. Để bắt đầu, tôi sẽ thử cùng một vùng cho truy vấn và đối tượng, chỉ để xem nó có hoạt động hay không và sau đó thử các cấu hình riêng biệt.

Các vấn đề liên quan