Tetap teratur dengan koleksi
Simpan dan kategorikan konten berdasarkan preferensi Anda.
Halaman ini menyediakan contoh kode dalam Java untuk menggunakan
Memcache API level rendah. Memcache
adalah sistem caching objek memori terdistribusi berperforma tinggi yang menyediakan
akses cepat ke data yang di-cache. Untuk mempelajari memcache lebih lanjut, baca
Ringkasan Memcache.
Penggunaan sinkron
Contoh API tingkat rendah yang menggunakan MemcacheService sinkron:
@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");}}
Penggunaan asinkron
Contoh API tingkat rendah yang menggunakan 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);}
Untuk informasi selengkapnya tentang API level rendah, lihat
Javadoc Memcache.
[[["Mudah dipahami","easyToUnderstand","thumb-up"],["Memecahkan masalah saya","solvedMyProblem","thumb-up"],["Lainnya","otherUp","thumb-up"]],[["Sulit dipahami","hardToUnderstand","thumb-down"],["Informasi atau kode contoh salah","incorrectInformationOrSampleCode","thumb-down"],["Informasi/contoh yang saya butuhkan tidak ada","missingTheInformationSamplesINeed","thumb-down"],["Masalah terjemahan","translationIssue","thumb-down"],["Lainnya","otherDown","thumb-down"]],["Terakhir diperbarui pada 2025-02-05 UTC."],[],[]]