使用javathread定期从数据库更新ehcache

时间:2015-05-22 15:57:49

标签: java multithreading spring caching ehcache

我一直在运行用户不断添加数据的Web应用程序。我想定期使用Java线程更新我的Ehcache(下面提供的配置)。我有点困惑如何编写我的线程代码来定期更新Ehcache。请建议我,我现有的代码和配置如下: -

ehcache.xml中

<?xml version="1.0" encoding="UTF-8"?>
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd">
<cache name="allUserProfileMap" maxElementsInMemory="1000" maxElementsOnDisk="100000" eternal="true" overflowToDisk="true" />
</ehcache>

CacheRoomTemplate.java

public class CacheRoomTemplate implements Serializable{

private static final long serialVersionUID = -1025054737473362397L;
private CacheRoomTemplate storageTemplate;  

public void setDelegate(CacheRoomTemplate storageDelegate) {
    this.storageTemplate = storageDelegate;
}   

/*
 * All UserInfo
 */
public static Map<Integer, Profile> allUserProfileMap=new ConcurrentHashMap<Integer, Profile>();

@Cacheable(cacheName = "allUserProfileMap")
public Profile getAllUserProfileMap(Integer userId) {
    logger.debug("== find AllUserProfileMap by emailId={}", userId);
    if(storageTemplate != null)
        storageTemplate.getAllUserProfileMap(userId);
    return allUserProfileMap.get(userId);
}

@Cacheable(cacheName = "allUserProfileMap")
public Profile removeUserFromAllUserProfileMap(Integer userId) {
    logger.debug("== remove UserId by EmailId={}", userId);
    if(storageTemplate != null)
        storageTemplate.removeUserFromAllUserProfileMap(userId);
    return allUserProfileMap.remove(userId);
}

@Cacheable(cacheName = "allUserProfileMap")
public Set<Profile> findAllUserProfileMap() {
    Collection<Integer> keys = allUserProfileMap.keySet();
    Set<Profile> profileSet = new HashSet<Profile>();
    synchronized (profileSet) {
        Iterator<Integer> iterator = keys.iterator();
        while (iterator.hasNext()) {
            profileSet.add(getAllUserProfileMap(iterator.next()));
        }
    }
    logger.debug("== got all registered email Ids from providers, size={}", profileSet.size());
    if(storageTemplate != null)
        storageTemplate.findAllCacheLoggedInIds();
    return profileSet;
}


@TriggersRemove(cacheName = "allUserProfileMap", when = When.AFTER_METHOD_INVOCATION, removeAll = true)
public void addAllUserProfileMap(Integer userId, Profile profile) {
    allUserProfileMap.put(userId, profile);
    logger.debug("== added a AllUserProfileMap with id={}", userId);
    if(storageTemplate != null)
        storageTemplate.addAllUserProfileMap(userId, profile);
}
}

弹簧servlet.xml中

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xmlns:ehcache="http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring"
xsi:schemaLocation="http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring/ehcache-spring-1.1.xsd">
<ehcache:annotation-driven />

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

<bean id="cache" class="net.sf.ehcache.Cache" factory-bean="cacheManager" factory-method="getCache">
    <constructor-arg value="myCache"/>
</bean>

任何人都可以帮我编写可以使用多线程定期查询数据库和更新缓存的代码。

0 个答案:

没有答案