Skip to content

Instantly share code, notes, and snippets.

@so
Created December 18, 2009 10:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save so/259419 to your computer and use it in GitHub Desktop.
Save so/259419 to your computer and use it in GitHub Desktop.
package com.google.sitebricks.binding;
import com.google.inject.Inject;
import com.google.inject.Provider;
import com.google.inject.servlet.SessionScoped;
import net.jcip.annotations.ThreadSafe;
import javax.servlet.http.HttpSession;
import java.io.Serializable;
import com.google.appengine.api.memcache.MemcacheService;
import com.google.appengine.api.memcache.MemcacheServiceFactory;
import com.google.appengine.api.memcache.Expiration;
/**
* Used to store binding (or forwarding) information between successive requests. This
* cache is an alternative to the default {@linkplain com.google.sitebricks.binding.HttpSessionFlashCache} in that it
* explicitly uses the appengine Memcache API.
*
* @author Dhanji R. Prasanna (dhanji@gmail.com)
* (modified by so)
*/
@ThreadSafe @SessionScoped
public class GaeMemcache implements FlashCache {
//public final class GaeFlashCache implements FlashCache {
private final Provider<HttpSession> session;
private final MemcacheService memcache = MemcacheServiceFactory.getMemcacheService();
@Inject
GaeMemcache(Provider<HttpSession> session) {
this.session = session;
memcache.setNamespace(session.get().getId());
}
@SuppressWarnings("unchecked")
public <T> T get(String key) {
return (T) memcache.get(key);
}
public <T> T remove(String key) {
@SuppressWarnings("unchecked")
T previous = (T) get(key);
memcache.delete(key);
return previous;
}
public <T> void put(String key, T t) {
Expiration x = Expiration.byDeltaSeconds(session.get().getMaxInactiveInterval());
memcache.put(key, t, x);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment