我配置了与MongoDB一起使用的mule object-store-caching-strategy。
<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();
}
}
'expire'方法中有3个选项,如何实现它。 我不知道,哪个更喜欢。 似乎它不起作用 - expire方法不是runnig。 有人知道为什么吗?什么是问题?
答案 0 :(得分:0)
很确定它是
<ee:object-store-caching-strategy name="MongoDB_Caching_Strategy" doc:name="Caching Strategy">
<spring-object-store ref="MongoDB_object_store" entryTTL="200"/>
</ee:object-store-caching-strategy>
HTH
答案 1 :(得分:0)
InMemoryObjectStore
继承自http://www.mulesoft.org/docs/site/current3/apidocs/org/mule/util/store/AbstractMonitoredObjectStore.html,可让您设置entryTTL
。
而MongoObjectStore
没有。
您可以尝试扩展MongoobjectStore
并添加entryTTL
字段。然后覆盖已由org.mule.api.store.PartitionableExpirableObjectStore
实施的MongoObjectStore
的过期方法。您可以尝试使用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