google-guava-cache

java - google guava cache difference between invalidateAll() and cleanUp()

拟墨画扇 提交于 2020-01-11 05:37:50
问题 Say I have a Cache that is defined like this: private static Cache<String, Long> alertsUIDCache = CacheBuilder.newBuilder(). expireAfterAccess(60).build(); From what I read (please correct me if I am wrong): If value is written to Cache at 0:00, it should be moved to "ready to be evicted" status after 60 seconds. The actual removing of the value from the Cache will happen at the next cache modification (what is exactly is cache modification ?). is that right? Also, I am not sure what the

Unit-testing a class that calls a static method

[亡魂溺海] 提交于 2020-01-03 13:04:50
问题 I am trying to unit-test a class 'A' which calls a static method of a class 'B'. Class 'B' essentially has a google guava cache which retrieves a value(Object) from the cache given a key, or loads the object into the cache (in case of a cache-miss) using a service adapter. The service-adapter class in turn has other autowired dependencies to retrieve the object. These are the classes for illustration purposes: Class A public class A { public Object getCachedObject(String key) { return B

Unit-testing a class that calls a static method

时光总嘲笑我的痴心妄想 提交于 2020-01-03 13:04:07
问题 I am trying to unit-test a class 'A' which calls a static method of a class 'B'. Class 'B' essentially has a google guava cache which retrieves a value(Object) from the cache given a key, or loads the object into the cache (in case of a cache-miss) using a service adapter. The service-adapter class in turn has other autowired dependencies to retrieve the object. These are the classes for illustration purposes: Class A public class A { public Object getCachedObject(String key) { return B

How to empty Guava cache every 30 seconds while sending it to another method?

纵饮孤独 提交于 2019-12-31 03:26:13
问题 I am populating my guava cache from multiple threads by calling add method. Now from the background thread which runs every 30 seconds, I want to send whatever is there in the cache to sendToDB method atomically? Below is my code: public class Example { private final ScheduledExecutorService executorService = Executors .newSingleThreadScheduledExecutor(); private final Cache<Integer, List<Process>> cache = CacheBuilder.newBuilder().maximumSize(100000) .removalListener(RemovalListeners

Concurrent calls to cached method

假装没事ソ 提交于 2019-12-25 07:05:12
问题 I'm using Spring cache abstraction with Guava cache. I have a method with @Cacheable annotation and parameter (that serves as a cache key) to put values into the cache. But this method is accessed in a multi threaded env so there are multiple concurrent calls to the method with the same parameter value. So that means the same logic that creates the value to be cached is done for the same cache key multiple times and put into the cache multiple times concurrently. It'd be much more efficient

How to reliably drop records from Guava LoadingCache?

99封情书 提交于 2019-12-25 04:33:14
问题 I am using Guava LoadingCache to populate some data into it and I want to remove all the entries from that LoadingCache every 1 minute. public class MetricHolder { private final ExecutorService executor = Executors.newFixedThreadPool(2); private final LoadingCache<String, AtomicLongMap<String>> clientIdMetricCounterCache = CacheBuilder.newBuilder().expireAfterWrite(1, TimeUnit.MINUTES) .removalListener(RemovalListeners.asynchronous(new SendToDatabase(), executor)) .build(new CacheLoader

How to mock Google Guava cache builder?

放肆的年华 提交于 2019-12-24 04:09:13
问题 @Component public class LibraryService { @Autowired private BookService bookService; private Cache<UUID, Book> bookCache = CacheBuilder.newBuilder().maximumSize(512).expireAfterWrite(15, TimeUnit.MINUTES).build(); public void someMethod(UUID bookId) { try { Book book = bookCache.get(bookId, () -> bookService.findBookByUuid(bookId)); //some operations } catch (ExecutionException e) { throw new ProcessingFailureException("Failed to load cache value", e); } } } I need to write unit test for this

How does timed cache expiry work?

时间秒杀一切 提交于 2019-12-12 03:45:21
问题 I know that Guava Cache allows individual caches to be configured with an expiry time. Does Guava do this using a timer that wakes up after a configured number of seconds to invalidate the cache? I have a transaction that is long-running. Whatever is in the cache at the start of the transaction, I would like it to continue till the end of the transaction. So even if the number of seconds of validity of a cache gets expired during the transaction, the values accessed from the cache should

Scala Guava type mismatch issue

大城市里の小女人 提交于 2019-12-11 10:51:38
问题 I am trying to implement a simple usecase using Guava caching but facing some issues as shown below: case class Person(x:Int, y:String) val db = Map(1 -> Person(1,"A"), 2 -> Person(2,"B"), 3 -> Person(3,"C")) val loader:CacheLoader[Int,Person] = new CacheLoader[Int,Person](){ def load(key: Int): Person = { db(key) } } lazy val someData = CacheBuilder.newBuilder().expireAfterWrite(60, MINUTES).maximumSize(10).build(loader) someData.get(3) The error I am getting is related to types which I am

spring - using google guava cache

烈酒焚心 提交于 2019-12-07 17:26:50
问题 I'm trying to use google guava cache in my spring app, but result is never caching. This are my steps: in conf file: @EnableCaching @Configuration public class myConfiguration { @Bean(name = "CacheManager") public CacheManager cacheManager() { return new GuavaCacheManager("MyCache"); } } In class I want to use caching: public class MyClass extends MyBaseClass { @Cacheable(value = "MyCache") public Integer get(String key) { System.out.println("cache not working"); return 1; } } Then when I'm