Guava CacheBuilder: imply additional conditions to entity removal

ぃ、小莉子 提交于 2019-12-23 17:06:58

问题


I want: Remove entity when both conditions are true

  • timeout expired
  • some external conditions are true

Question: How should I imply additional removal conditions besides timeout? Or how can I restore entity from removal listener (see code below)?

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

回答1:


Try Static class with isExternalCondition() method.



来源:https://stackoverflow.com/questions/21990133/guava-cachebuilder-imply-additional-conditions-to-entity-removal

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!