2010-10-19 28 views
9

Trong EHCache, có cách nào để thực hiện một số loại trình nghe db trong đó cahce sẽ tự động cập nhật nếu dữ liệu không đồng bộ? (ví dụ ngay khi người dùng yêu cầu dữ liệu, cahce sẽ kiểm tra nếu dữ liệu không đồng bộ, nếu có ... tự cập nhật và trả về dữ liệu, nếu không ... chỉ cần trả lại dữ liệu từ bộ nhớ cache) Nếu ai đó có thể chỉ cho tôi một phần của đặc điểm kỹ thuật làm nổi bật việc sử dụng này, điều đó thật tuyệt vời!Làm mới EHCache

Mục đích là luôn cung cấp dữ liệu mới nhất cho người dùng. Vì vậy, tôi đoán một cơ chế làm mới theo thời gian sẽ không làm như dữ liệu có thể thay đổi bất cứ lúc nào.

ehcache không bắt buộc để sử dụng trong trường hợp của tôi, vì vậy bất kỳ cơ chế đáp ứng này sẽ được chào đón nhất ...

Cảm ơn !!

Trả lời

3

Đối với EhCache this là những gì tôi tin rằng bạn đang tìm kiếm. Nếu bạn không muốn thực hiện làm mới theo thời gian (mặc dù nó là một giải pháp đơn giản), trình kích hoạt hoặc bản tin cập nhật dựa trên xe buýt sẽ là cách để đi. Bạn có thể thực hiện một số thống kê và xem tần suất cập nhật khi kích hoạt đã được thiết lập và chuyển sang cập nhật theo thời gian với tần suất đủ để đáp ứng Nyquist.

+0

Rất tiếc, liên kết không còn hoạt động nữa. – emanciperingsivraren

+0

đây là liên kết hoạt động http://www.ehcache.org/documentation/2.8/recipes/expiration.html – ChainLooper

3

Tôi đã làm điều đó bằng cách sử dụng chú thích ehcache-spring. Đây là những phụ thuộc của tôi trong maven pom.xml

<dependency> 
    <groupId>org.springframework</groupId> 
    <artifactId>spring-core</artifactId> 
    <version>3.0.5.RELEASE</version> 
</dependency> 
<dependency> 
    <groupId>net.sf.ehcache</groupId> 
    <artifactId>ehcache-core</artifactId> 
    <version>2.2.0</version> 
</dependency> 
    <dependency> 
    <groupId>com.googlecode.ehcache-spring-annotations</groupId> 
    <artifactId>ehcache-spring-annotations</artifactId> 
    <version>1.2.0-M1</version> 
</dependency> 

@Có thể hoạt động, nhưng tiếc là @TriggersRemove không hoạt động. Giải pháp thay thế là làm mất hiệu lực bộ nhớ cache theo cách thủ công. Dưới đây là ví dụ của tôi về việc sử dụng:

package com.company.project.dao; 

import java.util.List; 

import net.sf.ehcache.CacheManager; 
import net.sf.ehcache.Ehcache; 

import org.hibernate.SessionFactory; 
import org.springframework.beans.factory.FactoryBean; 
import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.beans.factory.annotation.Qualifier; 
import org.springframework.stereotype.Component; 
import org.springframework.transaction.annotation.Propagation; 
import org.springframework.transaction.annotation.Transactional; 

import com.company.project.domain.Parent; 
import com.company.project.domain.Child; 

import com.googlecode.ehcache.annotations.Cacheable; 
import com.googlecode.ehcache.annotations.KeyGenerator; 
import com.googlecode.ehcache.annotations.Property; 

@Component("Example") 
public class EhcacheExample { 

    @Autowired 
    @Qualifier("ehCacheManager") 
    private FactoryBean<CacheManager> ehCacheManager; 

    public void createParen(Parent parent) { 
     cleanCache(parent); 
     create(parent); 
    } 

    private void cleanCache(Parent parent) { 
     try { 
      CacheManager cacheManager = ehCacheManager.getObject(); 
      Ehcache ehcache = cacheManager.getEhcache("myCache"); 
      ehcache.remove(parent.getChild().hashCode()); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
    } 

    @Cacheable 
    ( cacheName = "myCache", 
     keyGenerator = @KeyGenerator (   
      name = "com.company.project.util.ChildCacheKeyGenerator",     
      properties = @Property(name="includeMethod", value="false") 
     ) 
    ) 
    public List<SerieRecording> getParentsByChild(Child child) { 
     return ...; 
    } 

    @Override 
    public void deleteParentById(long id) { 
     Parent parent = findById(id); 
     cleanCache(parent); 
     delete(parent); 
    } 

... 
} 

Việc thực hiện KeyGenerator có thể là:

package com.company.project.util; 

import java.io.Serializable; 

import org.slf4j.Logger; 
import org.slf4j.LoggerFactory; 

import com.company.project.domain.Child; 

import com.googlecode.ehcache.annotations.key.AbstractCacheKeyGenerator; 


public class ChildCacheKeyGenerator extends AbstractCacheKeyGenerator<Serializable> { 

    Logger logger = LoggerFactory.getLogger(this.getClass()); 

    @Override 
    public Serializable generateKey(Object... data) { 

     if (data[0] instanceof Child) { 
      Child child = (Child)data[0]; 
      return child.hashCode(); 
     } 
     new IllegalArgumentException(); 
     return null; 
    } 

} 

Trong cấu hình Spring:

<bean id="ehCacheManager" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean" > 
    <property name="configLocation" value="classpath:config/ehcache-methods.xml"/> 
</bean> 

ehcache-methods.xml:

<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
         xsi:noNamespaceSchemaLocation="ehcache.xsd"> 

    <cache name="myCache" eternal="false" 
     maxElementsInMemory="12600" overflowToDisk="false" diskPersistent="false" 
     timeToIdleSeconds="0" timeToLiveSeconds="1800" 
     memoryStoreEvictionPolicy="LRU" /> 

</ehcache> 

Tôi hy vọng nó hữu ích.

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