Java Code Examples for com.google.appengine.api.memcache.MemcacheService#increment()

The following examples show how to use com.google.appengine.api.memcache.MemcacheService#increment() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MemcacheBestPracticeServlet.java    From java-docs-samples with Apache License 2.0 6 votes vote down vote up
@Override
public void doGet(HttpServletRequest req, HttpServletResponse resp)
    throws IOException, ServletException {
  String path = req.getRequestURI();
  if (path.startsWith("/favicon.ico")) {
    return; // ignore the request for favicon.ico
  }

  MemcacheService syncCache = MemcacheServiceFactory.getMemcacheService();
  syncCache.setErrorHandler(ErrorHandlers.getConsistentLogAndContinue(Level.INFO));

  byte[] whoKey = "who".getBytes();
  byte[] countKey = "count".getBytes();

  byte[] who = (byte[]) syncCache.get(whoKey);
  String whoString = who == null ? "nobody" : new String(who);
  resp.getWriter().print("Previously incremented by " + whoString + "\n");
  syncCache.put(whoKey, "Java".getBytes());
  Long count = syncCache.increment(countKey, 1L, 0L);
  resp.getWriter().print("Count incremented by Java = " + count + "\n");
}
 
Example 2
Source File: Mutex.java    From yawp with MIT License 6 votes vote down vote up
public boolean aquire() {
    MemcacheService memcache = MemcacheServiceFactory.getMemcacheService(ns);
    long start = System.currentTimeMillis();
    while (true) {
        if (memcache.increment(key, 1L, 0L) == 1L) {
            return true;
        }
        if (System.currentTimeMillis() - start > maxWait) {
            return false;
        }
        try {
            Thread.sleep(100L);
        } catch (InterruptedException e) {
        }
    }
}