Restez organisé à l'aide des collections
Enregistrez et classez les contenus selon vos préférences.
Cette page fournit des exemples de code en Java pour l'utilisation de l'API Memcache de bas niveau. Memcache est un système très performant de mise en cache des objets dans une mémoire distribuée. Il fournit un accès rapide aux données en cache. Pour en savoir plus sur Memcache, consultez la page Présentation de Memcache.
Utilisation synchrone
Exemple d'API de bas niveau utilisant le service MemcacheService synchrone :
@SuppressWarnings("serial")// With @WebServlet annotation the webapp/WEB-INF/web.xml is no longer required.@WebServlet(name="MemcacheSync",description="Memcache: Synchronous",urlPatterns="/memcache/sync")publicclassMemcacheSyncCacheServletextendsHttpServlet{@OverridepublicvoiddoGet(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 valuecount=newBigInteger(value).longValue();count++;value=BigInteger.valueOf(count).toByteArray();// Put back in cachesyncCache.put(key,value);}// Output contentresp.setContentType("text/plain");resp.getWriter().print("Value is "+count+"\n");}}
Utilisation asynchrone
Exemple d'API de bas niveau avec 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 valuecount=newBigInteger(value).longValue();count++;value=BigInteger.valueOf(count).toByteArray();// Put back in cacheasyncCache.put(key,value);}}catch(InterruptedException|ExecutionExceptione){thrownewServletException("Error when waiting for future value",e);}
Pour plus d'informations sur l'API de bas niveau, voir Javadoc Memcache.
Sauf indication contraire, le contenu de cette page est régi par une licence Creative Commons Attribution 4.0, et les échantillons de code sont régis par une licence Apache 2.0. Pour en savoir plus, consultez les Règles du site Google Developers. Java est une marque déposée d'Oracle et/ou de ses sociétés affiliées.
Dernière mise à jour le 2025/02/06 (UTC).
[[["Facile à comprendre","easyToUnderstand","thumb-up"],["J'ai pu résoudre mon problème","solvedMyProblem","thumb-up"],["Autre","otherUp","thumb-up"]],[["Difficile à comprendre","hardToUnderstand","thumb-down"],["Informations ou exemple de code incorrects","incorrectInformationOrSampleCode","thumb-down"],["Il n'y a pas l'information/les exemples dont j'ai besoin","missingTheInformationSamplesINeed","thumb-down"],["Problème de traduction","translationIssue","thumb-down"],["Autre","otherDown","thumb-down"]],["Dernière mise à jour le 2025/02/06 (UTC)."],[],[]]