com.scalar.database.storage.cassandra.StatementCache Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of scalardb Show documentation
Show all versions of scalardb Show documentation
A library that provides a distributed storage abstraction and client-coordinated distributed transaction manager on the storage.
package com.scalar.database.storage.cassandra;
import com.datastax.driver.core.PreparedStatement;
import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
import javax.annotation.concurrent.ThreadSafe;
/**
* A cache for statements. The cache will try to evict entries that haven't been used recently when
* it reaches or is approaching the size limit.
*
* @author Hiroyuki Yamada
*/
@ThreadSafe
public class StatementCache {
private static final int DEFAULT_CACHE_SIZE = 128;
private Cache cache;
/** Constructs a cache with default cache size (= 128). */
public StatementCache() {
this(DEFAULT_CACHE_SIZE);
}
public StatementCache(int maxSize) {
cache = CacheBuilder.newBuilder().maximumSize(maxSize).build();
}
public PreparedStatement get(String cacheKey) {
return cache.getIfPresent(cacheKey);
}
public void put(String cacheKey, PreparedStatement statement) {
cache.put(cacheKey, statement);
}
}