Java Code Examples for com.github.benmanes.caffeine.cache.Cache#getIfPresent()
The following examples show how to use
com.github.benmanes.caffeine.cache.Cache#getIfPresent() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CaffeineCacheImpl.java From jboot with Apache License 2.0 | 5 votes |
@Override public <T> T get(String cacheName, Object key) { Cache cache = getCache(cacheName); CaffeineCacheObject data = (CaffeineCacheObject) cache.getIfPresent(key); if (data == null) { return null; } if (data.isDue()) { cache.invalidate(key); return null; } return (T) data.getValue(); }
Example 2
Source File: CaffeineCacheImpl.java From jboot with Apache License 2.0 | 5 votes |
@Override public <T> T get(String cacheName, Object key, IDataLoader dataLoader) { Cache cache = getCache(cacheName); CaffeineCacheObject data = (CaffeineCacheObject) cache.getIfPresent(key); if (data == null || data.isDue()) { Object newValue = dataLoader.load(); if (newValue != null) { data = new CaffeineCacheObject(newValue); putData(cache, key, data); } return (T) newValue; } else { return (T) data.getValue(); } }
Example 3
Source File: CaffeineCacheImpl.java From jboot with Apache License 2.0 | 5 votes |
@Override public <T> T get(String cacheName, Object key, IDataLoader dataLoader, int liveSeconds) { Cache cache = getCache(cacheName); CaffeineCacheObject data = (CaffeineCacheObject) cache.getIfPresent(key); if (data == null || data.isDue()) { Object newValue = dataLoader.load(); if (newValue != null) { data = new CaffeineCacheObject(newValue, liveSeconds); putData(cache, key, data); } return (T) newValue; } else { return (T) data.getValue(); } }
Example 4
Source File: CaffeineCacheImpl.java From jboot with Apache License 2.0 | 5 votes |
@Override public Integer getTtl(String cacheName, Object key) { Cache cache = getCacheOnly(cacheName); if (cache == null) { return null; } CaffeineCacheObject data = (CaffeineCacheObject) cache.getIfPresent(key); if (data == null) { return null; } return data.getTtl(); }
Example 5
Source File: CaffeineCacheImpl.java From jboot with Apache License 2.0 | 5 votes |
@Override public void setTtl(String cacheName, Object key, int seconds) { Cache cache = getCacheOnly(cacheName); if (cache == null) { return; } CaffeineCacheObject data = (CaffeineCacheObject) cache.getIfPresent(key); if (data == null) { return; } data.setLiveSeconds(seconds); putData(cache, key, data); }
Example 6
Source File: Demo.java From banyan with MIT License | 5 votes |
public static void test1() { Cache<String, double[][]> cache = Caffeine.newBuilder() .maximumSize(100) .expireAfterWrite(10, TimeUnit.MINUTES) .build(); String key = "name"; double[][] v = cache.getIfPresent(key); double[][] v1 = new double[0][0]; if (null == v) { cache.put(key, v1); } while (true) { try { Thread.sleep(1000); System.err.println(cache.getIfPresent(key)); } catch (InterruptedException e) { e.printStackTrace(); } } // // double[][] d1 = {{1, 2}, {3, 4}}; // double[][] d2 = new double[d1.length][d1[0].length]; // copy(d1, d2); // // System.out.println(d2[1][0]); }
Example 7
Source File: CacheMetricsCollectorTest.java From client_java with Apache License 2.0 | 5 votes |
@Test public void cacheExposesMetricsForHitMissAndEviction() throws Exception { Cache<String, String> cache = Caffeine.newBuilder().maximumSize(2).recordStats().executor(new Executor() { @Override public void execute(Runnable command) { // Run cleanup in same thread, to remove async behavior with evictions command.run(); } }).build(); CollectorRegistry registry = new CollectorRegistry(); CacheMetricsCollector collector = new CacheMetricsCollector().register(registry); collector.addCache("users", cache); cache.getIfPresent("user1"); cache.getIfPresent("user1"); cache.put("user1", "First User"); cache.getIfPresent("user1"); // Add to cache to trigger eviction. cache.put("user2", "Second User"); cache.put("user3", "Third User"); cache.put("user4", "Fourth User"); assertMetric(registry, "caffeine_cache_hit_total", "users", 1.0); assertMetric(registry, "caffeine_cache_miss_total", "users", 2.0); assertMetric(registry, "caffeine_cache_requests_total", "users", 3.0); assertMetric(registry, "caffeine_cache_eviction_total", "users", 2.0); }
Example 8
Source File: TestCaffeineCache.java From lucene-solr with Apache License 2.0 | 4 votes |
@Test public void testTimeDecay() { Cache<Integer, String> cacheDecay = Caffeine.newBuilder() .executor(Runnable::run) .maximumSize(20) .build(); for (int i = 1; i < 21; i++) { cacheDecay.put(i, Integer.toString(i)); } Map<Integer, String> itemsDecay; // Now increase the freq count for 5 items for (int i = 0; i < 5; ++i) { for (int j = 0; j < 10; ++j) { cacheDecay.getIfPresent(i + 13); } } // OK, 13 - 17 should have larger counts and should stick past next few collections cacheDecay.put(22, "22"); cacheDecay.put(23, "23"); cacheDecay.put(24, "24"); cacheDecay.put(25, "25"); itemsDecay = cacheDecay.policy().eviction().get().hottest(10); // 13 - 17 should be in cache, but 11 and 18 (among others) should not. Testing that elements before and // after the ones with increased counts are removed, and all the increased count ones are still in the cache assertNull(itemsDecay.get(11)); assertNull(itemsDecay.get(18)); assertNotNull(itemsDecay.get(13)); assertNotNull(itemsDecay.get(14)); assertNotNull(itemsDecay.get(15)); assertNotNull(itemsDecay.get(16)); assertNotNull(itemsDecay.get(17)); // Testing that all the elements in front of the ones with increased counts are gone for (int idx = 26; idx < 32; ++idx) { cacheDecay.put(idx, Integer.toString(idx)); } //Surplus count should be at 0 itemsDecay = cacheDecay.policy().eviction().get().hottest(10); assertNull(itemsDecay.get(20)); assertNull(itemsDecay.get(24)); assertNotNull(itemsDecay.get(13)); assertNotNull(itemsDecay.get(14)); assertNotNull(itemsDecay.get(15)); assertNotNull(itemsDecay.get(16)); assertNotNull(itemsDecay.get(17)); }
Example 9
Source File: AbstractCacheManager.java From Alpine with Apache License 2.0 | 2 votes |
/** * Retrieves an object (of the specified class) from cache. * @param clazz the class of the object to retrieve from cache * @param key the unique identifier of the object to retrieve from cache * @param <T> the object type * @return the cached object (if found) or null if not found * @since 1.5.0 */ @SuppressWarnings("unchecked") public <T> T get(final Class clazz, final String key) { final Cache<String, Object> cache = typeMap.get(clazz); return (cache == null) ? null : (T) cache.getIfPresent(key); }