use of com.google.appengine.api.memcache.MemcacheService in project java-docs-samples by GoogleCloudPlatform.
the class MetadataEntityGroupTest method entityGroupCount_printsCount.
// [END entity_group_2]
@Test
public void entityGroupCount_printsCount() throws Exception {
StringWriter responseWriter = new StringWriter();
MemcacheService cache = MemcacheServiceFactory.getMemcacheService();
Entity entity1 = new Entity("Simple");
Key key1 = datastore.put(entity1);
Key entityGroupKey = Entities.createEntityGroupKey(key1);
EntityGroupCount groupCount = new EntityGroupCount(0, 0);
groupCount.showEntityGroupCount(datastore, cache, new PrintWriter(responseWriter), entityGroupKey);
assertThat(responseWriter.toString()).contains(" entities");
}
use of com.google.appengine.api.memcache.MemcacheService in project java-docs-samples by GoogleCloudPlatform.
the class LocalMemcacheTest method doTest.
// Run this test twice to prove we're not leaking any state across tests.
// [START doTest]
private void doTest() {
MemcacheService ms = MemcacheServiceFactory.getMemcacheService();
assertFalse(ms.contains("yar"));
ms.put("yar", "foo");
assertTrue(ms.contains("yar"));
}
use of com.google.appengine.api.memcache.MemcacheService in project Cached-Datastore by Emperorlou.
the class CachedDatastoreService method setSaferMemcacheValue.
// /////////////////////////////////////
// Safer memcache stuff
// The purpose of this mechanism is to store memcache values in multiple locations so that clearing happens less often
public void setSaferMemcacheValue(String key, Serializable value, int backups) {
if (key == null)
throw new IllegalArgumentException("key cannot be null.");
if (backups < 0 || backups >= 5)
throw new IllegalArgumentException("Backup count must be between 0 and 4.");
MemcacheService mc = getMC();
SaferMCValueWrapper wrappedValue = new SaferMCValueWrapper(value);
mc.put(key, wrappedValue);
for (int i = 0; i < backups; i++) mc.put(key + "-backup#" + i, wrappedValue);
}
use of com.google.appengine.api.memcache.MemcacheService in project community by GoogleCloudPlatform.
the class MemcacheConcurrentServlet method doGet.
@Override
public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
String path = req.getRequestURI();
if (path.startsWith("/favicon.ico")) {
// ignore the request for favicon.ico
return;
}
String key = "count-concurrent";
// Using the synchronous cache.
MemcacheService syncCache = MemcacheServiceFactory.getMemcacheService();
// Write this value to cache using getIdentifiable and putIfUntouched.
for (long delayMs = 1; delayMs < 1000; delayMs *= 2) {
IdentifiableValue oldValue = syncCache.getIdentifiable(key);
byte[] newValue = oldValue == null ? BigInteger.valueOf(0).toByteArray() : // newValue depends on old value
increment((byte[]) oldValue.getValue());
resp.setContentType("text/plain");
resp.getWriter().print("Value is " + new BigInteger(newValue).intValue() + "\n");
if (oldValue == null) {
// Key doesn't exist. We can safely put it in cache.
syncCache.put(key, newValue);
break;
} else if (syncCache.putIfUntouched(key, oldValue, newValue)) {
// newValue has been successfully put into cache.
break;
} else {
// Wait a while before trying again, waiting longer on successive loops.
try {
Thread.sleep(delayMs);
} catch (InterruptedException e) {
throw new ServletException("Error when sleeping", e);
}
}
}
}
use of com.google.appengine.api.memcache.MemcacheService in project community by GoogleCloudPlatform.
the class MemcacheSyncCacheServlet method doGet.
@Override
public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
String path = req.getRequestURI();
if (path.startsWith("/favicon.ico")) {
// ignore the request for favicon.ico
return;
}
MemcacheService syncCache = MemcacheServiceFactory.getMemcacheService();
syncCache.setErrorHandler(ErrorHandlers.getConsistentLogAndContinue(Level.INFO));
String key = "count-sync";
byte[] value;
long count = 1;
value = (byte[]) syncCache.get(key);
if (value == null) {
value = BigInteger.valueOf(count).toByteArray();
syncCache.put(key, value);
} else {
// Increment value
count = new BigInteger(value).longValue();
count++;
value = BigInteger.valueOf(count).toByteArray();
// Put back in cache
syncCache.put(key, value);
}
// Output content
resp.setContentType("text/plain");
resp.getWriter().print("Value is " + count + "\n");
}
Aggregations