private void localCacheInit() { if (localCache != null) { statsCollector.recordLevel(CACHE_SIZE_AT_FLUSH, this.localCache.size()); } localCache = new SimpleLRUCache<>(this.localCacheSize); }
@Override public void getBytes(final Callback<Optional<byte[]>> callback, final String key) { final CacheEntry cacheEntry = localCache.get(key); if (cacheEntry == null) { callback.resolve(Optional.<byte[]>empty()); return; } if (cacheEntry.isExpired(time)) { localCache.remove(key); callback.resolve(Optional.<byte[]>empty()); } else { final byte[] value = ((CacheBytesEntry) cacheEntry).value; if (value == null) { callback.resolve(Optional.<byte[]>empty()); } else { callback.resolve(Optional.of(value)); } } }
@Override public void putBytesWithTimeout(final String key, final byte[] value, final Duration expiry) { localCache.put(key, new CacheBytesEntry(key, Optional.of(expiry), time, value)); }
public CharBuf addBigDecimal( BigDecimal key ) { if (bigDCache == null) { bigDCache = new SimpleLRUCache<> ( 100 ); } char [] chars = bigDCache.get ( key ); if ( chars == null ) { String str = key.toString(); chars = FastStringUtils.toCharArray ( str ); bigDCache.put ( key, chars ); } add ( chars ); return this; }
@Override public final void serializeDate( JsonSerializerInternal jsonSerializer, Date date, CharBuf builder ) { String string = dateCache.get ( date ); if ( string == null) { CharBuf buf = CharBuf.create ( Dates.JSON_TIME_LENGTH ); Dates.jsonDateChars ( calendar, date, buf ); string = buf.toString(); dateCache.put ( date, string ); } builder.addChars ( FastStringUtils.toCharArray( string ) ); } }
@Override public void hasKey(final Callback<Boolean> hasKeyCallback, final String key) { final CacheEntry cacheEntry = localCache.getSilent(key); if (cacheEntry == null) { hasKeyCallback.resolve(false); } else { if (cacheEntry.isExpired(time)) { localCache.remove(key); hasKeyCallback.resolve(false); } else { final Object value = cacheEntry.getValue(); if (value == null) { hasKeyCallback.resolve(false); } else { hasKeyCallback.resolve(true); } } } }
private void initCache() { logger.info("flushing cache"); cache = new SimpleLRUCache<>(this.cacheSize); }
@Override public void delete(final String key) { localCache.remove(key); }
public LowLevelLocalKeyValueStoreService(final Timer timer, final Reactor reactor, final int localCacheSize, final StatsCollector statsCollector, final Optional<Duration> flushCacheDuration, final boolean debug) { this.localCacheSize = localCacheSize; this.timer = timer; this.reactor = reactor; this.statsCollector = statsCollector; reactor.addServiceToFlush(statsCollector); if (flushCacheDuration.isPresent()) { reactor.addRepeatingTask(flushCacheDuration.get(), this::localCacheInit); } if (debug || logger.isDebugEnabled()) { reactor.addRepeatingTask(TEN_SECONDS, this::debug); } reactor.addRepeatingTask(FIVE_SECONDS, () -> statsCollector.recordLevel(CACHE_SIZE, localCache.size()) ); localCacheInit(); }
public final CharBuf addFloat( Float key ) { if (fcache == null) { fcache = new SimpleLRUCache<> ( 100 ); } char [] chars = fcache.get ( key ); if ( chars == null ) { String str = Float.toString ( key ); chars = FastStringUtils.toCharArray ( str ); fcache.put ( key, chars ); } add ( chars ); return this; }
@Override public final void serializeDate( JsonSerializerInternal jsonSerializer, Date date, CharBuf builder ) { String string = dateCache.get ( date ); if ( string == null) { CharBuf buf = CharBuf.create ( Dates.JSON_TIME_LENGTH ); Dates.jsonDateChars ( calendar, date, buf ); string = buf.toString(); dateCache.put ( date, string ); } builder.addChars ( FastStringUtils.toCharArray( string ) ); } }
@Override public void hasKey(final Callback<Boolean> hasKeyCallback, final String key) { final CacheEntry<T> cacheEntry = cache.getSilent(key); if (cacheEntry != null) { if (cacheEntry.isExpired(time)) { statsCollector.increment(statKey + "expire"); cache.remove(key); //do not return so we can look things up in the fallbackReader. } else { final T value = cacheEntry.getValue(); if (value == null) { hasKeyCallback.resolve(false); } else { hasKeyCallback.resolve(true); } return; } } final CallbackBuilder callbackBuilder = reactor.callbackBuilder(); if (debug) { callbackBuilder.wrapWithLogging(hasKeyCallback, logger, String.format("get %s", key)); } else { callbackBuilder.wrapWithLogging(hasKeyCallback, logger, "hasKey operation"); } fallbackReader.hasKey(callbackBuilder.build(), key); }
private void initCache() { logger.info("flushing cache"); cache = new SimpleLRUCache<>(this.cacheSize); }
@Override public void deleteWithConfirmation(final Callback<Boolean> confirmation, final String key) { localCache.remove(key); confirmation.accept(true); }
public LowLevelLocalKeyValueStoreService(final Timer timer, final Reactor reactor, final int localCacheSize, final StatsCollector statsCollector, final Optional<Duration> flushCacheDuration, final boolean debug) { this.localCacheSize = localCacheSize; this.timer = timer; this.reactor = reactor; this.statsCollector = statsCollector; reactor.addServiceToFlush(statsCollector); if (flushCacheDuration.isPresent()) { reactor.addRepeatingTask(flushCacheDuration.get(), this::localCacheInit); } if (debug || logger.isDebugEnabled()) { reactor.addRepeatingTask(TEN_SECONDS, this::debug); } reactor.addRepeatingTask(FIVE_SECONDS, () -> statsCollector.recordLevel(CACHE_SIZE, localCache.size()) ); localCacheInit(); }
public CharBuf addBigInteger( BigInteger key ) { if (bigICache == null) { bigICache = new SimpleLRUCache<> ( 100 ); } char [] chars = bigICache.get ( key ); if ( chars == null ) { String str = key.toString(); chars = FastStringUtils.toCharArray ( str ); bigICache.put ( key, chars ); } add ( chars ); return this; }