2015-12-02 56 views
0

我在使用MongoDB的mule object-store-caching-strategy中进行了配置。在mule中使用MongoDB的cach的TTL

<ee:object-store-caching-strategy name="MongoDB_Caching_Strategy" doc:name="Caching Strategy"> <spring-object-store ref="MongoDB_object_store" /> </ee:object-store-caching-strategy>

使用弹簧:

<spring:bean id="MongoDB_object_store" class="org.mule.module.mongo.MongoObjectStore" init-method="initialize" scope="singleton"> <spring:property name="host" value="localhost"/> <spring:property name="port" value="27017"/> <spring:property name="database" value="test"/> <spring:property name="username" value="test"/> <spring:property name="writeConcern" value="DATABASE_DEFAULT"/> </spring:bean>

我想设置缓存的TTL。

我需要怎么做?

编辑:

我添加类 “ExpirableMongoObjectStore” -

public class ExpirableMongoObjectStore<T extends Serializable> extends MongoObjectStore { 

/** 
* The maxEntries of the Mongo cache 
*/ 
@Optional 
@DefaultValue("-1") 
private int maxEntries; 

/** 
* The maxEntries of the Mongo cache 
*/ 
@Optional 
@DefaultValue("600000") 
private int entryTTL; 

/** 
* The maxEntries of the Mongo cache 
*/ 
@Optional 
@DefaultValue("1000") 
private int expirationInterval; 

private MongoClient mongoClient; 
private static final String TIMESTAMP_FIELD = "timestamp"; 
protected final Log logger = LogFactory.getLog(this.getClass()); 

public void setEntryTTL(int entryTTL) {this.entryTTL = entryTTL; } 
public void setExpirationInterval(int expirationInterval) { this.expirationInterval = expirationInterval;} 
public void setMaxEntries(int maxEntries){this.maxEntries = maxEntries; } 
public int getEntryTTL(){ return entryTTL;} 
public int getExpirationInterval() {return expirationInterval;} 
public int getMaxEntries() { return maxEntries;} 

protected ConcurrentSkipListMap<Long, StoredObject<T>> store; 
public ExpirableMongoObjectStore() 
{ 

     this.store = new ConcurrentSkipListMap<Long, StoredObject<T>>(); 
     logger.debug("ddd"); 
} 

@Override 
public void expire(int entryTTL, int maxEntries) throws ObjectStoreException   
{ 
    //Option 1: 
     /*if ((entryTTL > 0)) 
     { 
      final long now = System.nanoTime(); 
      int expiredEntries = 0; 
      Map.Entry<?, ?> oldestEntry; 

      purge: 
      while ((oldestEntry = store.firstEntry()) != null) 
      { 
       Long oldestKey = (Long) oldestEntry.getKey(); 
       long oldestKeyValue = oldestKey.longValue(); 

       if (TimeUnit.NANOSECONDS.toMillis(now - oldestKeyValue) >= entryTTL) 
       { 
        store.remove(oldestKey); 
        expiredEntries++; 
       } 
       else 
       { 
        break purge; 
       } 
      } 

      if (logger.isDebugEnabled()) 
      { 
       logger.debug("Expired " + expiredEntries + " old entries"); 
      } 
     }*/ 

    //Option 2: 
     super.expire(getEntryTTL(), getMaxEntries()); 

    //Option 3: 
     /*final String partitionName = "OBJECTSTORE_DEFAULT_PARTITION_NAME"; 

     final String collection = getCollectionName(partitionName); 
     final long expireAt = System.currentTimeMillis() - entryTTL; 
     final DBObject query = QueryBuilder.start(TIMESTAMP_FIELD).lessThan(expireAt).get(); 
     mongoClient.removeObjects(collection, query, getWriteConcern());*/ 
} 

protected static class StoredObject<T> 
{ 
    private Serializable id; 
    private T item; 

    public StoredObject(Serializable id, T item) 
    { 
     this.id = id; 
     this.item = item; 
    } 

    public Serializable getId() 
    { 
     return id; 
    } 

    public T getItem() 
    { 
     return item; 
    } 

    @Override 
    @SuppressWarnings("unchecked") 
    public boolean equals(Object o) 
    { 
     if (this == o) 
     { 
      return true; 
     } 

     if (o == null || getClass() != o.getClass()) 
     { 
      return false; 
     } 

     StoredObject<T> that = (StoredObject<T>) o; 
     if (!id.equals(that.id)) 
     { 
      return false; 
     } 
     return true; 
    } 

    @Override 
    public int hashCode() 
    { 
     return id.hashCode(); 
    } 

    @Override 
    public String toString() 
    { 
     final StringBuilder sb = new StringBuilder(); 
     sb.append("StoredObject"); 
     sb.append("{id='").append(id).append('\''); 
     sb.append(", item=").append(item); 
     sb.append('}'); 
     return sb.toString(); 
    } 
} 

有在 '过期' 的方法有3个选项,如何实现它。 我不知道,哪个更喜欢。 和每一个从他们看来它不工作 - 过期的方法是不runnig。 有人知道为什么吗?什么是问题?

回答

0

InMemoryObjectStore继承,它允许您设置entryTTL

MongoObjectStore没有。

您可以尝试延伸MongoobjectStore并添加一个entryTTL字段。然后覆盖已由MongoObjectStore实施的org.mule.api.store.PartitionableExpirableObjectStore的过期方法。您可以尝试使用InMemoryObjectStore类中的expire方法中的相同逻辑:http://grepcode.com/file/repo1.maven.org/maven2/org.mule/mule-core/3.5.0/org/mule/util/store/InMemoryObjectStore.java#InMemoryObjectStore.expire%28%29

+0

谢谢,如果你可以用示例代码告诉我你的意思是什么,它对我非常有帮助。 – user4593578

+0

或我如何访问MongoobjectStore类... – user4593578

+0

我的意思 - 我需要在XML中创建或创建类吗? – user4593578