【问题标题】:TTL of cach using MongoDB in mule在 mule 中使用 MongoDB 的缓存 TTL
【发布时间】:2015-12-02 18:37:13
【问题描述】:

我在 mule object-store-caching-strategy 中配置了使用 MongoDB。

<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 方法没有运行。 有人知道为什么吗?什么问题?

【问题讨论】:

    标签: mongodb caching mule


    【解决方案1】:

    很确定

    <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

    【讨论】:

    • 虽然我认为这只适用于in-memory-object-store。
    【解决方案2】:

    InMemoryObjectStore 继承自 http://www.mulesoft.org/docs/site/current3/apidocs/org/mule/util/store/AbstractMonitoredObjectStore.html,它允许您设置 entryTTL。

    而MongoObjectStore 没有。

    您可以尝试扩展 MongoobjectStore 并添加一个 entryTTL 字段。然后覆盖 org.mule.api.store.PartitionableExpirableObjectStore 中的 expire 方法,该方法已经由 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

    【讨论】:

    • 谢谢,如果你能用示例代码告诉我你的意思是什么,这对我很有帮助。
    • 我的意思是——我需要在 xml 中做还是创建类?
    • 创建一个 Java 类并对其进行扩展。公共类 ExpirableMongoObjectStore 扩展 MongoObjectStore
    • 但是 MongoObjectStore 中有 'expire' 函数 - public void expire(final int entryTtl, final int ignoredMaxEntries, final String partitionName) throws ObjectStoreException { final String collection = getCollectionName(partitionName); final long expireAt = System.currentTimeMillis() - entryTtl;最终 DBObject 查询 = QueryBuilder.start(TIMESTAMP_FIELD).lessThan(expireAt).get(); mongoClient.removeObjects(集合,查询,getWriteConcern()); }
    • 是的,但是覆盖它,所以你可以设置自己的 ttl。为 ttl 添加一个字段,覆盖 expire 方法并调用传入 ttl 的现有 expire 方法。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-19
    相关资源
    最近更新 更多