【问题标题】:Guava CacheBuilder: imply additional conditions to entity removalGuava CacheBuilder:暗示实体移除的附加条件
【发布时间】:2014-03-26 06:08:06
【问题描述】:

我想要:两个条件都为真时删除实体

  • 超时已过
  • 一些外部条件为真

问题: 我应该如何暗示除了超时之外的其他移除条件?或者我怎样才能从删除监听器中恢复实体(见下面的代码)?

我的代码(仅基于超时已删除):

LoadingCache<String, Integer> ints = CacheBuilder.newBuilder()
        .maximumSize(10000)
        .expireAfterAccess(ACCESS_TIMEOUT, TimeUnit.MILLISECONDS)
        .removalListener(
                new RemovalListener() {                       
                    public void onRemoval(RemovalNotification notification) {
                        if (notification.getCause() == RemovalCause.EXPIRED) {
                            if (!isExternalCondition()) {
                               //IS IT POSSIBLE TO RESTORE?
                              restore(notification.getValue()); 
                            }
                        } else {
                            System.out.println("Just removed for some reason");
                        }
                    }
                }
        )
        .build(
                new CacheLoader<String, Integer>() {
                    public Integer load(String key) throws Exception {
                        return new Integer(-1);
                    }
                });

【问题讨论】:

  • 如果你能做到这一点,我会很惊讶。

标签: java collections guava


【解决方案1】:

尝试使用 isExternalCondition() 方法的静态类。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-04-26
    • 2014-03-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-04
    相关资源
    最近更新 更多