Guava CacheBuilder:暗示对实体删除的附加条件 [英] Guava CacheBuilder: imply additional conditions to entity removal

查看:439
本文介绍了Guava CacheBuilder:暗示对实体删除的附加条件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want: Remove entity when both conditions are true


  • 已过期

  • 部分外部条件为真实

em> 除了超时,我应该如何暗示其他删除条件?或者如何从移除侦听器恢复实体(请参阅下面的代码)?

我的代码):

My code (which already remove based on timeout only):

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);
                    }
                });


推荐答案

尝试使用 isExternalCondition ()方法。

这篇关于Guava CacheBuilder:暗示对实体删除的附加条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆