/** * Caches {@code value} for {@code key}. The value is moved to the head of * the queue. * @param key the key. * @param value the value. * @return the previous value mapped by {@code key}. */ public final V put(K key, V value) { if (key == null || value == null) { throw new NullPointerException("key == null || value == null"); } V previous; synchronized (this) { putCount++; size += safeSizeOf(key, value); previous = map.put(key, value); if (previous != null) { size -= safeSizeOf(key, previous); } } if (previous != null) { entryRemoved(false, key, previous, value); } trimToSize(maxSize); return previous; }
private int safeSizeOf(K key, V value) { int result = sizeOf(key, value); if (result < 0) { throw new IllegalStateException("Negative size: " + key + "=" + value); } return result; }
/** * Removes the entry for {@code key} if it exists. * @param key the key * @return the previous value mapped by {@code key}. */ public final V remove(K key) { if (key == null) { throw new NullPointerException("key == null"); } V previous; synchronized (this) { previous = map.remove(key); if (previous != null) { size -= safeSizeOf(key, previous); } } if (previous != null) { entryRemoved(false, key, previous, null); } return previous; }
V createdValue = create(key); if (createdValue == null) { return null; size += safeSizeOf(key, createdValue); entryRemoved(false, key, createdValue, mapValue); return mapValue; } else { trimToSize(maxSize); return createdValue;
/** * Clear the cache, calling {@link #entryRemoved} on each removed entry. */ public final void evictAll() { trimToSize(-1); // -1 will evict 0-sized elements }
private DataSourceWrapper(String name, HikariDataSource service, DatabaseType dbType) { this.service = Args.notNull(service, "service"); this.databaseType = Args.notNull(dbType, "dbType"); this.name = name; this.sqlErrorCodes = SqlErrorCodes.newInstance(dbType); this.sqlStateCodes = SqlStateCodes.newInstance(dbType); this.cacheSeqNameSqls = new LruCache<>(100); }
V createdValue = create(key); if (createdValue == null) { return null; size += safeSizeOf(key, createdValue); entryRemoved(false, key, createdValue, mapValue); return mapValue; } else { trimToSize(maxSize); return createdValue;
/** * Removes the entry for {@code key} if it exists. * @param key the key * @return the previous value mapped by {@code key}. */ public final V remove(K key) { if (key == null) { throw new NullPointerException("key == null"); } V previous; synchronized (this) { previous = map.remove(key); if (previous != null) { size -= safeSizeOf(key, previous); } } if (previous != null) { entryRemoved(false, key, previous, null); } return previous; }
/** * Clear the cache, calling {@link #entryRemoved} on each removed entry. */ public final void evictAll() { trimToSize(-1); // -1 will evict 0-sized elements }
private String getSqlDeltaCrlCacheIds(int numEntries) { String sql = cacheSqlDeltaCrlCacheIds.get(numEntries); if (sql == null) { sql = datasource.buildSelectFirstSql(numEntries, "ID ASC", "ID FROM DELTACRL_CACHE WHERE ID>? AND CA_ID=?"); cacheSqlDeltaCrlCacheIds.put(numEntries, sql); } return sql; }
/** * Caches {@code value} for {@code key}. The value is moved to the head of * the queue. * @param key the key. * @param value the value. * @return the previous value mapped by {@code key}. */ public final V put(K key, V value) { if (key == null || value == null) { throw new NullPointerException("key == null || value == null"); } V previous; synchronized (this) { putCount++; size += safeSizeOf(key, value); previous = map.put(key, value); if (previous != null) { size -= safeSizeOf(key, previous); } } if (previous != null) { entryRemoved(false, key, previous, value); } trimToSize(maxSize); return previous; }
/** * Remove the eldest entries until the total of remaining entries is at or * below the requested size. * * @param maxSize the maximum size of the cache before returning. Could be -1 * to evict even 0-sized elements. */ public void trimToSize(int maxSize) { while (true) { K key; V value; synchronized (this) { if (size < 0 || (map.isEmpty() && size != 0)) { throw new IllegalStateException(getClass().getName() + ".sizeOf() is reporting inconsistent results!"); } if (size <= maxSize || map.isEmpty()) { break; } Map.Entry<K, V> toEvict = map.entrySet().iterator().next(); key = toEvict.getKey(); value = toEvict.getValue(); map.remove(key); size -= safeSizeOf(key, value); evictionCount++; } entryRemoved(true, key, value, null); } }
/** * Sets the size of the cache. * * @param maxSize the new maximum size. */ public void resize(int maxSize) { synchronized (this) { this.maxSize = Args.positive(maxSize, "maxSize"); } trimToSize(maxSize); }
private int safeSizeOf(K key, V value) { int result = sizeOf(key, value); if (result < 0) { throw new IllegalStateException("Negative size: " + key + "=" + value); } return result; }
private String getSqlCidFromPublishQueue(int numEntries) { String sql = cacheSqlCidFromPublishQueue.get(numEntries); if (sql == null) { sql = datasource.buildSelectFirstSql(numEntries, "CID ASC", "CID FROM PUBLISHQUEUE WHERE PID=? AND CA_ID=?"); cacheSqlCidFromPublishQueue.put(numEntries, sql); } return sql; }
/** * Remove the eldest entries until the total of remaining entries is at or * below the requested size. * * @param maxSize the maximum size of the cache before returning. Could be -1 * to evict even 0-sized elements. */ public void trimToSize(int maxSize) { while (true) { K key; V value; synchronized (this) { if (size < 0 || (map.isEmpty() && size != 0)) { throw new IllegalStateException(getClass().getName() + ".sizeOf() is reporting inconsistent results!"); } if (size <= maxSize || map.isEmpty()) { break; } Map.Entry<K, V> toEvict = map.entrySet().iterator().next(); key = toEvict.getKey(); value = toEvict.getValue(); map.remove(key); size -= safeSizeOf(key, value); evictionCount++; } entryRemoved(true, key, value, null); } }
/** * Sets the size of the cache. * * @param maxSize the new maximum size. */ public void resize(int maxSize) { synchronized (this) { this.maxSize = Args.positive(maxSize, "maxSize"); } trimToSize(maxSize); }