Java 8 has reached end of support and will be deprecated on January 31, 2026. After deprecation, you won't be able to deploy Java 8 applications, even if your organization previously used an organization policy to re-enable deployments of legacy runtimes. Your existing Java 8 applications will continue to run and receive traffic after their deprecation date. We recommend that you migrate to the latest supported version of Java.

Memcache Examples

This page provides code examples in Java for using the low-level Memcache API. Memcache is a high-performance, distributed memory object caching system that provides fast access to cached data. To learn more about memcache, read the Memcache Overview.

Synchronous usage

Low-level API example using the synchronous MemcacheService:

@SuppressWarnings("serial")
// With @WebServlet annotation the webapp/WEB-INF/web.xml is no longer required.
@WebServlet(name="MemcacheSync",description="Memcache: Synchronous",
urlPatterns="/memcache/sync")
publicclass MemcacheSyncCacheServletextendsHttpServlet{
@Override
publicvoiddoGet(HttpServletRequestreq,HttpServletResponseresp)throwsIOException,
ServletException{
Stringpath=req.getRequestURI();
if(path.startsWith("/favicon.ico")){
return;// ignore the request for favicon.ico
}
MemcacheServicesyncCache=MemcacheServiceFactory.getMemcacheService();
syncCache.setErrorHandler(ErrorHandlers.getConsistentLogAndContinue(Level.INFO));
Stringkey="count-sync";
byte[]value;
longcount=1;
value=(byte[])syncCache.get(key);
if(value==null){
value=BigInteger.valueOf(count).toByteArray();
syncCache.put(key,value);
}else{
// Increment value
count=newBigInteger(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");
}
}

Asynchronous usage

Low-level API example using AsyncMemcacheService:

AsyncMemcacheServiceasyncCache=MemcacheServiceFactory.getAsyncMemcacheService();
asyncCache.setErrorHandler(ErrorHandlers.getConsistentLogAndContinue(Level.INFO));
Stringkey="count-async";
byte[]value;
longcount=1;
Future<Object>futureValue=asyncCache.get(key);// Read from cache.
// ... Do other work in parallel to cache retrieval.
try{
value=(byte[])futureValue.get();
if(value==null){
value=BigInteger.valueOf(count).toByteArray();
asyncCache.put(key,value);
}else{
// Increment value
count=newBigInteger(value).longValue();
count++;
value=BigInteger.valueOf(count).toByteArray();
// Put back in cache
asyncCache.put(key,value);
}
}catch(InterruptedException|ExecutionExceptione){
thrownewServletException("Error when waiting for future value",e);
}

For more information on the low-level API, see the Memcache Javadoc.

Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.

Last updated 2025年12月09日 UTC.