Java – guava cachebuilder delete listener

Please tell me where I lost it

I have a cache built by cachebuilder in datapool Datapool is a singleton object. Its instance can obtain various threads and operate Now I have a thread that generates data and adds it to the cache

To display the relevant part of the code:

private InputDataPool(){

    cache=CacheBuilder.newBuilder().expireAfterWrite(1000,TimeUnit.NANOSECONDS).removalListener(
            new RemovalListener(){
                {
                    logger.debug("Removal Listener created");
                }
                                public void onRemoval(RemovalNotification notification) {
                                    System.out.println("Going to remove data from InputDataPool");
                                    logger.info("Following data is being removed:"+notification.getKey());
                                    if(notification.getCause()==RemovalCause.EXPIRED)
                                    {
                                        logger.fatal("This data expired:"+notification.getKey());
                                    }else
                                    {
                                        logger.fatal("This data didn't expired but evacuated intentionally"+notification.getKey());
                                    }

                                }}
                    ).build(new CacheLoader(){

                        @Override
                        public Object load(Object key) throws Exception {
                                logger.info("Following data being loaded"+(Integer)key);
                                Integer uniqueId=(Integer)key;
                                return InputDataPool.getInstance().getAndRemoveDataFromPool(uniqueId);

                        }

                    });
}

public static InputDataPool getInstance(){
        if(clsInputDataPool==null){
            synchronized(InputDataPool.class){
                if(clsInputDataPool==null)
                {
                    clsInputDataPool=new InputDataPool();
                }
            }
        }
    return clsInputDataPool;
}

The call from the thread is as simple as this

while(true){
 inputDataPool.insertDataIntoPool(inputDataPacket);
     //call some logic which comes with inputDataPacket and sleep for 2 seconds.
}

And inputdatapool Insertdataintopool is the same

inputDataPool.insertDataIntoPool(InputDataPacket inputDataPacket){ 
 cache.get(inputDataPacket.getId());
}

The problem now is that the elements in the cache should expire after 1000 Nanosec So when inputdatapool is called the second time When insertdataintopool, the first inserted data will be evacuated because it must have expired because the call is 2 seconds after insertion The appropriate delete listener should then be called But it won't happen I looked at the cache statistics and the evictioncount is always zero, regardless of the call to cache How long does it take to get (ID)

But importantly, if I extend inputdatapool insertDataIntoPool

inputDataPool.insertDataIntoPool(InputDataPacket inputDataPacket){ 
 cache.get(inputDataPacket.getId());
    try{
     Thread.sleep(2000);
   }catch(InterruptedException ex){ex.printStackTrace();
     }
cache.get(inputDataPacket.getId())
}

Then the eviction occurs as expected and the remove listener is called

There's nothing I can do now because I've missed something that expects this behavior If you see anything, please help me see it

PS: please ignore any spelling mistakes No checks were made and generics were not used because this was only in the stage of testing the cache builder function

thank you

Solution

As explained in Javadoc and user guide, no thread can ensure that entries are deleted from the cache immediately after the delay Conversely, entries are deleted during write operations, and occasionally during read operations if there are few writes This is to achieve high throughput and low latency Of course, every write operation does not result in cleanup:

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>