Search in sources :

Example 6 with MemcacheService

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");
}
Also used : Entity(com.google.appengine.api.datastore.Entity) MemcacheService(com.google.appengine.api.memcache.MemcacheService) StringWriter(java.io.StringWriter) Key(com.google.appengine.api.datastore.Key) PrintWriter(java.io.PrintWriter) Test(org.junit.Test)

Example 7 with MemcacheService

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"));
}
Also used : MemcacheService(com.google.appengine.api.memcache.MemcacheService)

Example 8 with MemcacheService

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);
}
Also used : MemcacheService(com.google.appengine.api.memcache.MemcacheService)

Example 9 with MemcacheService

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);
            }
        }
    }
}
Also used : ServletException(javax.servlet.ServletException) MemcacheService(com.google.appengine.api.memcache.MemcacheService) IdentifiableValue(com.google.appengine.api.memcache.MemcacheService.IdentifiableValue) BigInteger(java.math.BigInteger)

Example 10 with MemcacheService

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");
}
Also used : MemcacheService(com.google.appengine.api.memcache.MemcacheService) BigInteger(java.math.BigInteger)

Aggregations

MemcacheService (com.google.appengine.api.memcache.MemcacheService)10 BigInteger (java.math.BigInteger)4 IdentifiableValue (com.google.appengine.api.memcache.MemcacheService.IdentifiableValue)2 PrintWriter (java.io.PrintWriter)2 ServletException (javax.servlet.ServletException)2 Entity (com.google.appengine.api.datastore.Entity)1 Key (com.google.appengine.api.datastore.Key)1 Index (com.google.appengine.api.search.Index)1 IndexSpec (com.google.appengine.api.search.IndexSpec)1 SearchService (com.google.appengine.api.search.SearchService)1 SearchServiceConfig (com.google.appengine.api.search.SearchServiceConfig)1 StringWriter (java.io.StringWriter)1 Test (org.junit.Test)1