Skip to content

Instantly share code, notes, and snippets.

@blangel
Created October 17, 2013 18:47
Show Gist options
  • Save blangel/7030135 to your computer and use it in GitHub Desktop.
Save blangel/7030135 to your computer and use it in GitHub Desktop.
LoadingCache impl for memcache
package com.dashlabs.dash.cache;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.CacheStats;
import com.google.common.cache.LoadingCache;
import com.google.common.collect.ImmutableMap;
import javax.annotation.Nullable;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.Callable;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
/**
* User: blangel
* Date: 10/17/13
* Time: 2:14 PM
*/
public class LoadingMemcache<K, V> implements LoadingCache<K, V> {
public static interface Memcache<K, V> {
V get(K key);
void set(K key, V value);
void remove(K key);
}
private final Memcache<K, V> cache;
private final CacheLoader<K, V> cacheLoader;
private final long duration;
private final TimeUnit timeUnit;
public LoadingMemcache(Memcache<K, V> cache, CacheLoader<K, V> cacheLoader, long duration, TimeUnit timeUnit) {
this.cache = cache;
this.cacheLoader = cacheLoader;
this.duration = duration;
this.timeUnit = timeUnit;
}
@Override public V get(final K key) throws ExecutionException {
try {
V value = cache.get(key);
if (value == null) {
value = cacheLoader.load(key);
cache.set(key, value);
}
return value;
} catch (Exception e) {
throw new ExecutionException(e);
}
}
@Override public V getUnchecked(K key) {
try {
return get(key);
} catch (ExecutionException ee) {
throw new RuntimeException(ee);
}
}
@Override public ImmutableMap<K, V> getAll(Iterable<? extends K> keys) throws ExecutionException {
Map<K, V> values = new HashMap<K, V>();
for (K key : keys) {
V value = get(key);
values.put(key, value);
}
return ImmutableMap.copyOf(values);
}
@Override public V apply(K key) {
throw new UnsupportedOperationException();
}
@Override public void refresh(K key) {
try {
V value = cacheLoader.load(key);
cache.set(key, value);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
@Override public ConcurrentMap<K, V> asMap() {
throw new UnsupportedOperationException();
}
@SuppressWarnings("unchecked")
@Nullable @Override public V getIfPresent(Object key) {
return cache.get((K) key);
}
@Override public V get(K key, Callable<? extends V> valueLoader) throws ExecutionException {
throw new UnsupportedOperationException();
}
@SuppressWarnings("unchecked")
@Override public ImmutableMap<K, V> getAllPresent(Iterable<?> keys) {
Map<K, V> values = new HashMap<K, V>();
for (K key : (Iterable<K>) keys) {
V value = getIfPresent(key);
values.put(key, value);
}
return ImmutableMap.copyOf(values);
}
@Override public void put(K key, V value) {
cache.set(key, value);
}
@Override public void putAll(Map<? extends K, ? extends V> map) {
for (K key : map.keySet()) {
put(key, map.get(key));
}
}
@SuppressWarnings("unchecked")
@Override public void invalidate(Object key) {
cache.remove((K) key);
}
@SuppressWarnings("unchecked")
@Override public void invalidateAll(Iterable<?> keys) {
for (K key : (Iterable<K>) keys) {
invalidate(key);
}
}
@Override public void invalidateAll() {
throw new UnsupportedOperationException();
}
@Override public long size() {
throw new UnsupportedOperationException();
}
@Override public CacheStats stats() {
throw new UnsupportedOperationException();
}
@Override public void cleanUp() {
throw new UnsupportedOperationException();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment