Sunday, January 29, 2017

Jcache vs Memcache

Jcache

import javax.cache.Cache;
import javax.cache.CacheException;
import javax.cache.CacheFactory;
import javax.cache.CacheManager;

try {
    CacheFactory cacheFactory = CacheManager.getInstance().getCacheFactory();
    Cache cache = cacheFactory.createCache(Collections.emptyMap());
    key = "p";
    if (cache.containsKey(key)) {
        pList = (List<p>) cache.get(key);
    } else {
        pList = ofy().load().type(p.class).list();
        cache.put(key, pList);
    }
} catch (CacheException e) {
}

Memcache

import com.google.appengine.api.memcache.ErrorHandlers;
import com.google.appengine.api.memcache.MemcacheService;
import com.google.appengine.api.memcache.MemcacheServiceFactory;

MemcacheService cache = MemcacheServiceFactory.getMemcacheService();
cache.setErrorHandler(ErrorHandlers.getConsistentLogAndContinue(Level.INFO));
key = "p";
if (cache.contains(key)) {
    pList = (List<p>) cache.get(key);
} else {
    pList = ofy().load().type(p.class).list();
    cache.put(key, pList);
}