Cache cache;
HashMap<String, Object> object;
try {
// log.log(level, "1");
CacheFactory cacheFactory = CacheManager.getInstance()
.getCacheFactory();
// log.log(level, "2");
cache = cacheFactory.createCache(Collections.emptyMap());
// log.log(level, "3");
// Get the value from the cache.
object = (HashMap<String, Object>) cache.get(clazz
+ Long.toString(id));
// log.log(level, "4");
if (object != null) {
// log.log(level, "found " + clazz + " " + id +
// " in memcache");
return object;
}
// log.info("did not find " + clazz + " " + id +
// " in memcache");
} catch (CacheException e) {
e.printStackTrace();
// log.log(Level.WARNING, e.getMessage());
}
DatastoreService datastore = DatastoreServiceFactory
.getDatastoreService();
object = new HashMap<String, Object>();
try {
Entity e = datastore.get(KeyFactory.createKey(clazz, id));
// properties
for (Entry<String, Object> entry : e.getProperties().entrySet()) {
Object value = entry.getValue();
if (value instanceof Text) {
object.put(entry.getKey(), ((Text) value).getValue());
} else if (value instanceof String) {
object.put(entry.getKey(), ((String) value));
} else {
object.put(entry.getKey(), value);
}
}
object.put("id", Long.toString(e.getKey().getId()));
try {
// log.log(level, "1");
CacheFactory cacheFactory = CacheManager.getInstance()
.getCacheFactory();
// log.log(level, "2");
cache = cacheFactory.createCache(Collections.emptyMap());
// log.log(level, "3");
cache.put(clazz + id, object);
// log.info("put " + clazz + " " + id + " in memcache");
} catch (CacheException ce) {
// exception handling