Created
March 5, 2011 20:18
-
-
Save ts-3156/856681 to your computer and use it in GitHub Desktop.
sample code to use memcached in google app engine.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package util; | |
import java.util.Collections; | |
import java.util.logging.Logger; | |
import javax.cache.Cache; | |
import javax.cache.CacheException; | |
import javax.cache.CacheFactory; | |
import javax.cache.CacheManager; | |
import com.google.appengine.api.memcache.InvalidValueException; | |
import com.google.appengine.api.memcache.stdimpl.GCacheException; | |
public class CacheUtil { | |
static final Logger log = Logger.getLogger(CacheUtil.class.getName()); | |
private CacheUtil(){ | |
} | |
private static Cache getCache() { | |
Cache cache = null; | |
try { | |
CacheFactory cacheFactory = CacheManager.getInstance().getCacheFactory(); | |
cache = cacheFactory.createCache(Collections.emptyMap()); | |
} catch (CacheException e) { | |
log.warning(e.getClass().getSimpleName() + ": " + e.getMessage()); | |
return null; | |
} | |
return cache; | |
} | |
public static <T> boolean isCached(String key, T type){ | |
Cache cache = getCache(); | |
if(cache == null){ | |
log.warning("cache is null."); | |
return false; | |
} | |
if(!cache.containsKey(key)){ | |
log.warning("containsKey()がfalse"); | |
return false; | |
} | |
Object object = cache.get(key); | |
if(object == null){ | |
log.warning("object is null."); | |
return false; | |
} | |
if(!object.getClass().getSimpleName().equals(type.getClass().getSimpleName())) { | |
log.warning("getSimpleName()が一致していない"); | |
return false; | |
} | |
return true; | |
} | |
/** | |
* | |
* @param key | |
* @param value | |
* @return | |
*/ | |
@SuppressWarnings("unchecked") | |
public static boolean put(String key, Object value){ | |
boolean isPutted = true; | |
// long start = 0, end = 0; | |
// start = System.currentTimeMillis(); | |
try { | |
getCache().put(key, value); | |
} catch (GCacheException e) { | |
log.warning(e.getClass().getSimpleName() + ": " + key + "," + value + ", " + e.getMessage()); | |
isPutted = false; | |
} | |
// end = System.currentTimeMillis(); | |
// | |
// if(isPutted) | |
// log.info(value.getClass().getSimpleName() + " is putted: " + (double)(end - start)/1000 + "秒, " + key + ", " + value); | |
// else | |
// log.info(value.getClass().getSimpleName() + " is NOT putted: " + (double)(end - start)/1000 + "秒, " + key + ", " + value); | |
return isPutted; | |
} | |
/** | |
* | |
* @param <T> | |
* @param key | |
* @param type 取り出すオブジェクトの型。これが一致しないとエクセプションが起きる | |
* @param init trueにしておくと、typeと違うオブジェクトが格納されていた場合に、値を初期化する | |
* @return | |
*/ | |
@SuppressWarnings("unchecked") | |
public static <T> T get(String key, T type, boolean init){ | |
// long start = 0, end = 0; | |
// start = System.currentTimeMillis(); | |
if(init){ | |
if(!isCached(key, type)){ | |
put(key, type); | |
log.warning("initialized, key=" + key + ", type=" + type.getClass().getSimpleName() + ", object=" + getCache().get(key).getClass().getSimpleName()); | |
} | |
} | |
T dataToReturn = null; | |
try { | |
dataToReturn = (T) getCache().get(key); | |
} catch (InvalidValueException e) { | |
log.warning(e.getClass().getSimpleName() + ": " + key + ", " + e.getMessage()); | |
return null; | |
} | |
// ClassCastExceptionはこのメソッドの呼び出し元で起こる | |
// end = System.currentTimeMillis(); | |
// log.info(key + ", " + (double) (end - start)/1000 + "秒"); | |
return dataToReturn; | |
} | |
/** | |
* 明示的に削除しなくても容量が圧迫されると自動的に削除される | |
* @param key | |
*/ | |
public static void remove(String key){ | |
getCache().remove(key); | |
} | |
public static void removeAll(){ | |
getCache().clear(); | |
} | |
public static int size(){ | |
return getCache().size(); | |
} | |
public static int getCacheHits(){ | |
return getCache().getCacheStatistics().getCacheHits(); | |
} | |
public static int getCacheMisses(){ | |
return getCache().getCacheStatistics().getCacheMisses(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks for this file...
How can authorize my application with *.p12 file for memcache ?