
net.anthavio.cache.impl.SpyMemcache Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of hatatitla Show documentation
Show all versions of hatatitla Show documentation
Compact but tweakable REST client library you have been dreaming of
The newest version!
package net.anthavio.cache.impl;
import java.io.IOException;
import java.io.Serializable;
import java.net.InetSocketAddress;
import java.util.List;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import net.anthavio.cache.CacheBase;
import net.anthavio.cache.CacheEntry;
import net.anthavio.cache.CacheKeyProvider;
import net.anthavio.httl.util.Cutils;
import net.spy.memcached.ConnectionFactory;
import net.spy.memcached.MemcachedClient;
import net.spy.memcached.internal.CheckedOperationTimeoutException;
/**
* SpyMemcached implementation
*
* TODO constructor is too lenghty - introduce Builder
*
* @author martin.vanek
*
*/
public class SpyMemcache extends CacheBase {
public static final int MINUTE = 60; //seconds
public static final int HOUR = 60 * MINUTE;//seconds
public static final int DAY = 24 * HOUR;//seconds
public static final int MONTH = 30 * DAY; //Memcached maximum value as interval. Greater value is considered as epoch time (seconds after 1.1.1970T00:00:00)
private static final int MaxKeyLength = 250; //Memcached limit for key
private static final String VERSION_IN = "1"; //Must be String "1" - number cannot be incremented - Memcached is ASSHOLE
private final MemcachedClient client;
private final boolean clientCreated;
private final boolean namespaceVersioning;
private long operationTimeout = 5000; //Memcached call timeout in milliseconds
public SpyMemcache(String name, CacheKeyProvider keyProvider, ConnectionFactory connectionFactory,
List addrs, boolean namespaceVersioning) throws IOException {
super(name, keyProvider);
if (Cutils.isBlank(name)) {
throw new IllegalArgumentException("SpyRequestCache name must not be blank");
}
this.client = new MemcachedClient(connectionFactory, addrs);
this.clientCreated = true;
this.operationTimeout = connectionFactory.getOperationTimeout();
this.namespaceVersioning = namespaceVersioning;
}
public SpyMemcache(String name, CacheKeyProvider keyProvider, MemcachedClient client, long operationTimeout,
TimeUnit unitOfTimeout) {
this(name, keyProvider, client, operationTimeout, unitOfTimeout, false);
}
public SpyMemcache(String name, CacheKeyProvider keyProvider, MemcachedClient client) {
this(name, keyProvider, client, 5, TimeUnit.SECONDS, false);
}
public SpyMemcache(String name, CacheKeyProvider keyProvider, MemcachedClient client, boolean namespaceVersioning) {
this(name, keyProvider, client, 5, TimeUnit.SECONDS, namespaceVersioning);
}
public SpyMemcache(String name, CacheKeyProvider keyProvider, MemcachedClient client, long operationTimeout,
TimeUnit unitOfTimeout, boolean namespaceVersioning) {
super(name, keyProvider);
if (Cutils.isBlank(name)) {
throw new IllegalArgumentException("SpyRequestCache name must not be blank");
}
this.client = client;
this.clientCreated = false;
this.operationTimeout = unitOfTimeout.toMillis(operationTimeout);
this.namespaceVersioning = namespaceVersioning;
}
/**
* @return Spy Memcached client
*/
public MemcachedClient getClient() {
return client;
}
/**
* @return Memcached timeout for operation
*/
public long getOperationTimeout() {
return operationTimeout;
}
public void setOperationTimeout(long timeout, TimeUnit unit) {
this.operationTimeout = unit.toMillis(timeout);
if (this.operationTimeout < 1000) {
throw new IllegalArgumentException("Operation timeout " + operationTimeout + " must be >= 1 second");
}
}
@Override
protected CacheEntry doGet(String cacheKey) throws Exception {
if (cacheKey.length() > MaxKeyLength) {
throw new IllegalArgumentException("Key length exceded maximum " + MaxKeyLength);
}
Future
© 2015 - 2025 Weber Informatics LLC | Privacy Policy