Java에서 net.spy.spymemcached 라이브러리를 사용하여 Memcached 클라이언트를 구현하는 방법은 무엇입니까?
게시 됨: 2020-12-27
며칠 전에 몇 가지 간단한 단계를 통해 Mac OS에서 로컬로 MemCached Server를 설정하고 시작하는 방법에 대한 기사를 작성했습니다.
Memcached Java 클라이언트를 생성하는 세 가지 방법이 있습니다.
- net.spy.spymemcached 메서드
- com.whalin.Memcached – 자바 – 클라이언트 메소드
- com.googlecode.xmemcached 메소드
이 튜토리얼에서는 net.spy.spymemcached
라이브러리를 사용하여 간단한 Java 클라이언트를 작성하는 방법을 살펴보겠습니다. Eclipse 환경에 Maven 프로젝트가 있는 경우 pom.xml
파일에 아래 종속성을 추가하십시오.
set(), get(), delete() 작업을 살펴보십시오.
1 2 3 4 5 |
< dependency > < groupId > net . spy < / groupId > < artifactId > spymemcached < / artifactId > < version > 2.12.3 < / version > < / dependency > |
pom.xml 파일을 업데이트한 다음 Java 프로젝트를 업데이트해 보십시오. 다음은 간단한 Java 프로그램을 작성하는 것입니다.
구현SpyMemcached.java
라이브러리는 매우 간단한 set()
, get()
, delete()
작업과 함께 제공됩니다. 다음은 완전한 예입니다.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
package crunchify . java . tutorials ; import net . spy . memcached . MemcachedClient ; import java . io . IOException ; import java . net . InetSocketAddress ; /** * @author Crunchify.com * Version: 1.0.0 * Details: Use dependency net.spy.spymemcached to retrieve, store Key Value pair from MemCached Server * */ public class CrunchifySpyMemcachedClient { public static void main ( String [ ] args ) { implementCrunchifySpyMemcached ( ) ; } // Approach-1: net.spy.spymemcached // Use dependency net.spy.spymemcached to retrieve, store Key Value pair from MemCached Server private static void implementCrunchifySpyMemcached ( ) { // Get a memcache client operating on the specified memcached locations. MemcachedClient crunchifySpyMemCached ; try { crunchifySpyMemCached = new MemcachedClient ( new InetSocketAddress ( "127.0.0.1" , 11211 ) ) ; log ( "=====> Approach-1: SpyMemcached <=====\n" ) ; log ( "==> Connected to Crunchify's Local Server Successfully." + " Host: localhost, Port: 11211" ) ; // Set an object in the cache (using the default transcoder) regardless of any existing value. // The exp value is passed along to memcached exactly as given, and will be processed per the memcached protocol specification: // The actual value sent may either be Unix time (number of seconds since January 1, 1970, as a 32-bit value), or a number of seconds starting from // current time. In the latter case, this number of seconds may not exceed 60*60*24*30 (number of seconds in 30 days); if the number sent by a // client is larger than that, the server will consider it to be real Unix time value rather than an offset from current time. crunchifySpyMemCached . set ( "Crunchify" , 2000 , "New York" ) ; crunchifySpyMemCached . set ( "Google" , 2000 , "Mountain View" ) ; crunchifySpyMemCached . set ( "PayPal" , 2000 , "San Jose" ) ; crunchifySpyMemCached . set ( "Twitter" , 2000 , "San Francisco" ) ; crunchifySpyMemCached . set ( "Amazon" , 2000 , "Seattle" ) ; log ( "==> Total 5 Records added to MemCached using net.spy.spymemcached method\n" ) ; // Get with a single key and decode using the default transcoder. log ( "Key: Google, Value: " + crunchifySpyMemCached . get ( "Google" ) ) ; log ( "Key: PayPal, Value: " + crunchifySpyMemCached . get ( "PayPal" ) ) ; log ( "Key: Twitter, Value: " + crunchifySpyMemCached . get ( "Twitter" ) ) ; log ( "Key: Amazon, Value: " + crunchifySpyMemCached . get ( "Amazon" ) ) ; log ( "Key: Crunchify, Value: " + crunchifySpyMemCached . get ( "Crunchify" ) ) ; log ( "==> Total 5 Records Retrieved from MemCached using net.spy.spymemcached method\n" ) ; // Delete the given key from the cache. crunchifySpyMemCached . delete ( "Crunchify" ) ; log ( "==> Key:Crunchify deleted successfully\n" ) ; log ( "Key: Crunchify, Value: " + crunchifySpyMemCached . get ( "Crunchify" ) ) ; log ( "==> If no record found, it returns NULL\n" ) ; } catch ( IOException e ) { // Prints this throwable and its backtrace to the standard error stream e . printStackTrace ( ) ; } } private static void log ( Object object ) { System . out . println ( object ) ; } } |
로컬에서 MemCached 서버 시작
터미널 창으로 이동하여 memcached -d -p 11211
명령을 입력합니다.

자습서: Mac OS X에서 Memcached Process/Server를 설치 및 구성하는 방법은 무엇입니까?
Eclipse 콘솔 출력:
위의 프로그램을 Java 응용 프로그램으로 실행하기만 하면 다음과 같은 결과가 표시됩니다.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
2018 - 12 - 07 16 : 25 : 45.437 INFO net . spy . memcached . MemcachedConnection : Setting retryQueueSize to - 1 2018 - 12 - 07 16 : 25 : 45.459 INFO net . spy . memcached . MemcachedConnection : Added { QA sa =/ 127.0.0.1 : 11211 , #Rops=0, #Wops=0, #iq=0, topRop=null, topWop=null, toWrite=0, interested=0} to connect queue ===== > Approach - 1 : SpyMemcached < ===== == > Connected to Crunchify ' s Local Server Sucessfully . Host : localhost , Port : 11211 == > Total 5 Records added to MemCached using net . spy . spymemcached method Key : Google , Value : Mountain View Key : PayPal , Value : San Jose Key : Twitter , Value : San Francisco Key : Amazon , Value : Seattle Key : Crunchify , Value : New York == > Total 5 Records Retrieved from MemCached using net . spy . spymemcached method == > Key : Crunchify deleted successfully Key : Crunchify , Value : null == > If no record found , it returns NULL |
프로그램 실행에 문제가 있으면 알려주십시오. 다음 튜토리얼에서는 Memcached 데이터를 설정하고 가져오기 위해 we will go over another libraries
.
다음은?
두 개의 라이브러리가 더 있는 Memcached Java 클라이언트 자습서를 사용해 보세요.