whalin.Memcached-Java-Client 및 googlecode.xmemcached 라이브러리를 사용한 Memcached 자바 클라이언트 튜토리얼
게시 됨: 2019-09-01
이것은 Memcached which is distributed memory object caching server
에 대한 계속되는 자습서입니다. 이 튜토리얼을 진행하기 전에 이전 두 개의 튜토리얼이 있습니다.
- Memcached 설치 및 구성 방법
- net.spy.spymemcached 라이브러리를 사용한 Memcached 자바 클라이언트 튜토리얼
이 자습서에서는 아래 두 가지 접근 방식을 사용하여 Memcached Java 클라이언트를 살펴봅니다.
com.whalin.Memcached-자바 클라이언트 라이브러리
1 2 3 4 5 |
< dependency > < groupId > com . whalin < / groupId > < artifactId > Memcached - Java - Client < / artifactId > < version > 3.0.2 < / version > < / dependency > |
com.googlecode.xmemcached 라이브러리
1 2 3 4 5 |
< dependency > < groupId > com . googlecode . xmemcached < / groupId > < artifactId > xmemcached < / artifactId > < version > 2.4.5 < / version > < / dependency > |
이 두 가지 maven 종속성을 프로젝트의 pom.xml
파일에 추가하여 Eclipse 작업 공간에서 라이브러리를 가져오기만 하면 됩니다.
다음은 모든 세부 정보가 포함된 완전한 CrunchifyComWhalinXMemcachedClient.java 프로그램입니다.
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 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 |
package crunchify . java . tutorials ; import java . io . IOException ; import java . util . concurrent . TimeoutException ; import com . whalin . MemCached . MemCachedClient ; import com . whalin . MemCached . SockIOPool ; import net . rubyeye . xmemcached . XMemcachedClient ; import net . rubyeye . xmemcached . exception . MemcachedException ; /** * @author Crunchify.com * Version: 1.2.4 * Details: Use dependency com.whalin.Memcached-Java-Client and com.googlecode.xmemcached to retrieve, store Key Value pair * from MemCached Server */ public class CrunchifyComWhalinXMemcachedClient { public static void main ( String [ ] args ) { log ( "===================================== Approach-1: Using SpyMemcahed: https://crunchify.com/memcached-java-client-net-spy-spymemcached/ ===================================== \n" ) ; log ( "===================================== Approach-2: Using com.whalin.Memcached-Java-Client =====================================\n" ) ; implementWhalinMemcachedJavaClient ( ) ; log ( "===================================== Approach-3: Using com.googlecode.xmemcached Method =====================================\n" ) ; implementXMemCachedClient ( ) ; log ( "===================================== Program Completed ===================================== \n" ) ; } // Approach-1: Using SpyMemcahed: https://crunchify.com/memcached-java-client-net-spy-spymemcached/ // Approach-2: Using com.whalin.Memcached-Java-Client private static void implementWhalinMemcachedJavaClient ( ) { String [ ] servers = { "localhost:11211" } ; // This class is a connection pool for maintaning a pool of persistent connections to memcached servers. // The pool must be initialized prior to use. This should typically be early on in the lifecycle of the JVM instance. SockIOPool crunchfiyPool = SockIOPool . getInstance ( "Crunchify" ) ; // Sets the minimum number of spare connections to maintain in our available pool. crunchfiyPool . setMinConn ( 2 ) ; // Sets the maximum number of spare connections allowed in our available pool. crunchfiyPool . setMaxConn ( 20 ) ; // Sets the list of all cache servers. crunchfiyPool . setServers ( servers ) ; // Sets the failover flag for the pool. If this flag is set to true, and a socket fails to connect, the pool will attempt to return a socket from // another server if one exists. // If set to false, then getting a socket will return null if it fails to connect to the requested server. crunchfiyPool . setFailover ( true ) ; // Sets the initial number of connections per server in the available pool. crunchfiyPool . setInitConn ( 30 ) ; // Set the sleep time between runs of the pool maintenance thread. If set to 0, then the maint thread will not be started. crunchfiyPool . setMaintSleep ( 90 ) ; // Sets the socket timeout for reads. crunchfiyPool . setSocketTO ( 2000 ) ; // Sets the aliveCheck flag for the pool. When true, this will attempt to talk to the server on every connection checkout to make sure the connection is // still valid. crunchfiyPool . setAliveCheck ( true ) ; crunchfiyPool . initialize ( ) ; // Creates a new instance of MemCachedClient accepting a passed in pool name. MemCachedClient crunchifyWhalinClient = new MemCachedClient ( "Crunchify" ) ; // Adds data to the server; only the key and the value are specified. crunchifyWhalinClient . add ( "Java" , "Crunchify.com" ) ; crunchifyWhalinClient . add ( "WordPress" , "WordPress.com" ) ; crunchifyWhalinClient . add ( "Social" , "Facebook.com" ) ; log ( "==> Total 3 Records added to MemCached using com.whalin.Memcached-Java-Client Method\n" ) ; // Retrieve a key from the server, using a specific hash. // If the data was compressed or serialized when compressed, it will automatically be decompressed or serialized, as appropriate.. log ( "Key: Java, Value: " + crunchifyWhalinClient . get ( "Java" ) ) ; log ( "Key: WordPress, Value: " + crunchifyWhalinClient . get ( "WordPress" ) ) ; log ( "Key: Social, Value: " + crunchifyWhalinClient . get ( "Social" ) ) ; log ( "==> Total 3 Records Retrieved from MemCached using com.whalin.Memcached-Java-Client Method\n" ) ; // Deletes an object from cache given cache key. crunchifyWhalinClient . delete ( "Social" ) ; log ( "==> Record deleted using com.whalin.Memcached-Java-Client Method\n" ) ; log ( "Key: Social, Value: " + crunchifyWhalinClient . get ( "Social" ) ) ; log ( "==> Failure to get record Social as we deleted that before\n" ) ; } // Approach-3: Using com.googlecode.xmemcached private static void implementXMemCachedClient ( ) { String city = "New York" ; String city2 = "San Francisco" ; try { // XMemcached constructor,default weight is 1 XMemcachedClient xMemCachedclient = new XMemcachedClient ( "localhost" , 11211 ) ; // Set a value xMemCachedclient . set ( "Crunchify" , 3600 , city ) ; xMemCachedclient . set ( "Twitter" , 3600 , city2 ) ; // Get a value for specify key Object myCity = xMemCachedclient . get ( "Twitter" ) ; log ( "=====> Key: Twitter, Value: " + xMemCachedclient . get ( "Twitter" ) + "\n" ) ; // Get a value for specify key (set timeout of three seconds) myCity = xMemCachedclient . get ( "Crunchify" , 3000 ) ; log ( "=====> Key: Crunchify, Value: " + xMemCachedclient . get ( "Crunchify" , 3000 ) + "\n" ) ; // Set a new expiration time for an existing item,using default opTimeout second. xMemCachedclient . touch ( "key" , 30 ) ; xMemCachedclient . delete ( "Crunchify" ) ; log ( "=====> Key: Crunchify, Value: " + xMemCachedclient . get ( "Crunchify" ) + "\n" ) ; // delete value xMemCachedclient . delete ( "key" ) ; // let's catch all exceptions } catch ( IOException e ) { e . printStackTrace ( ) ; } catch ( MemcachedException e ) { e . printStackTrace ( ) ; } catch ( TimeoutException e ) { e . printStackTrace ( ) ; } catch ( InterruptedException e ) { e . printStackTrace ( ) ; } } // Simple log utility private static void log ( Object object ) { System . out . println ( object ) ; } } |
Memcached 서버가 실행 중인지 확인하십시오.
1 |
bash - 3.2 $ memcached - d - p 11211 |
Eclipse에서 위의 명령을 실행하면 아래 결과를 볼 수 있습니다.
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 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 |
===================================== Approach - 1 : Using SpyMemcahed : https : //crunchify.com/memcached-java-client-net-spy-spymemcached/ ===================================== ===================================== Approach - 2 : Using com . whalin . Memcached - Java - Client ===================================== SLF4J : The requested version 1.7.16 by your slf4j binding is not compatible with [ 1.6 ] SLF4J : See http : //www.slf4j.org/codes.html#version_mismatch for further details. == > Total 3 Records added to MemCached using com . whalin . Memcached - Java - Client Method Key : Java , Value : Crunchify . com Key : WordPress , Value : WordPress . com Key : Social , Value : Facebook . com == > Total 3 Records Retrieved from MemCached using com . whalin . Memcached - Java - Client Method 18 : 27 : 35.433 [ main ] DEBUG com . whalin . MemCached . MemCachedClient - ++++ deletion of key : Social from cache was a success == > Record deleted using com . whalin . Memcached - Java - Client Method Key : Social , Value : null == > Failure to get record Social as we deleted that before ===================================== Approach - 3 : Using com . googlecode . xmemcached Method ===================================== 18 : 27 : 35.456 [ main ] INFO net . rubyeye . xmemcached . XMemcachedClient - XMemcachedClient is using Text protocol 18 : 27 : 35.491 [ main ] INFO com . google . code . yanf4j . nio . impl . SelectorManager - Creating 8 reactors . . . 18 : 27 : 35.496 [ main ] INFO com . google . code . yanf4j . core . impl . AbstractController - The Controller started at localhost / 127.0.0.1 : 0 . . . 18 : 27 : 35.509 [ Xmemcached - Reactor - 0 ] DEBUG com . google . code . yanf4j . core . impl . AbstractSession - session started 18 : 27 : 35.510 [ Xmemcached - Reactor - 0 ] INFO com . google . code . yanf4j . core . impl . AbstractController - Add a session : 127.0.0.1 : 11211 18 : 27 : 35.513 [ main ] DEBUG com . google . code . yanf4j . core . impl . AbstractSession - After encodingset Crunchify 0 3600 8 New York 18 : 27 : 35.514 [ Xmemcached - Reactor - 2 ] DEBUG net . rubyeye . xmemcached . impl . Optimizer - Optimieze merge buffer : set Crunchify 0 3600 8 New York 18 : 27 : 35.514 [ Xmemcached - Reactor - 2 ] DEBUG com . google . code . yanf4j . core . impl . AbstractSession - send buffers : [ buffer : position = 0 , limit = 34 , capacity = 34 ] 18 : 27 : 35.515 [ Xmemcached - Reactor - 2 ] DEBUG com . google . code . yanf4j . core . impl . AbstractSession - read 8 bytes from channel 18 : 27 : 35.515 [ main ] DEBUG com . google . code . yanf4j . core . impl . AbstractSession - After encodingset Twitter 0 3600 13 San Francisco 18 : 27 : 35.515 [ Xmemcached - Reactor - 2 ] DEBUG net . rubyeye . xmemcached . impl . Optimizer - Optimieze merge buffer : set Twitter 0 3600 13 San Francisco 18 : 27 : 35.515 [ Xmemcached - Reactor - 2 ] DEBUG com . google . code . yanf4j . core . impl . AbstractSession - send buffers : [ buffer : position = 0 , limit = 38 , capacity = 38 ] 18 : 27 : 35.515 [ Xmemcached - Reactor - 2 ] DEBUG com . google . code . yanf4j . core . impl . AbstractSession - read 8 bytes from channel 18 : 27 : 35.516 [ main ] DEBUG com . google . code . yanf4j . core . impl . AbstractSession - After encodingget Twitter 18 : 27 : 35.517 [ Xmemcached - Reactor - 2 ] DEBUG net . rubyeye . xmemcached . impl . Optimizer - Optimieze merge buffer : get Twitter 18 : 27 : 35.517 [ Xmemcached - Reactor - 2 ] DEBUG com . google . code . yanf4j . core . impl . AbstractSession - send buffers : [ buffer : position = 0 , limit = 13 , capacity = 13 ] 18 : 27 : 35.518 [ main ] DEBUG com . google . code . yanf4j . core . impl . AbstractSession - After encodingget Twitter 18 : 27 : 35.518 [ Xmemcached - Reactor - 2 ] DEBUG com . google . code . yanf4j . core . impl . AbstractSession - read 40 bytes from channel 18 : 27 : 35.518 [ Xmemcached - Reactor - 2 ] DEBUG net . rubyeye . xmemcached . impl . Optimizer - Optimieze merge buffer : get Twitter 18 : 27 : 35.518 [ Xmemcached - Reactor - 2 ] DEBUG com . google . code . yanf4j . core . impl . AbstractSession - send buffers : [ buffer : position = 0 , limit = 13 , capacity = 13 ] ===== > Key : Twitter , Value : San Francisco 18 : 27 : 35.518 [ Xmemcached - Reactor - 2 ] DEBUG com . google . code . yanf4j . core . impl . AbstractSession - read 40 bytes from channel 18 : 27 : 35.519 [ main ] DEBUG com . google . code . yanf4j . core . impl . AbstractSession - After encodingget Crunchify 18 : 27 : 35.519 [ Xmemcached - Reactor - 2 ] DEBUG net . rubyeye . xmemcached . impl . Optimizer - Optimieze merge buffer : get Crunchify 18 : 27 : 35.519 [ Xmemcached - Reactor - 2 ] DEBUG com . google . code . yanf4j . core . impl . AbstractSession - send buffers : [ buffer : position = 0 , limit = 15 , capacity = 15 ] 18 : 27 : 35.519 [ Xmemcached - Reactor - 2 ] DEBUG com . google . code . yanf4j . core . impl . AbstractSession - read 36 bytes from channel 18 : 27 : 35.519 [ main ] DEBUG com . google . code . yanf4j . core . impl . AbstractSession - After encodingget Crunchify 18 : 27 : 35.519 [ Xmemcached - Reactor - 2 ] DEBUG net . rubyeye . xmemcached . impl . Optimizer - Optimieze merge buffer : get Crunchify 18 : 27 : 35.519 [ Xmemcached - Reactor - 2 ] DEBUG com . google . code . yanf4j . core . impl . AbstractSession - send buffers : [ buffer : position = 0 , limit = 15 , capacity = 15 ] ===== > Key : Crunchify , Value : New York 18 : 27 : 35.520 [ Xmemcached - Reactor - 2 ] DEBUG com . google . code . yanf4j . core . impl . AbstractSession - read 36 bytes from channel 18 : 27 : 35.520 [ main ] DEBUG com . google . code . yanf4j . core . impl . AbstractSession - After encodingtouch key 30 18 : 27 : 35.520 [ Xmemcached - Reactor - 2 ] DEBUG net . rubyeye . xmemcached . impl . Optimizer - Optimieze merge buffer : touch key 30 18 : 27 : 35.520 [ Xmemcached - Reactor - 2 ] DEBUG com . google . code . yanf4j . core . impl . AbstractSession - send buffers : [ buffer : position = 0 , limit = 14 , capacity = 14 ] 18 : 27 : 35.520 [ Xmemcached - Reactor - 2 ] DEBUG com . google . code . yanf4j . core . impl . AbstractSession - read 11 bytes from channel 18 : 27 : 35.520 [ main ] DEBUG com . google . code . yanf4j . core . impl . AbstractSession - After encodingdelete Crunchify 18 : 27 : 35.520 [ Xmemcached - Reactor - 2 ] DEBUG net . rubyeye . xmemcached . impl . Optimizer - Optimieze merge buffer : delete Crunchify 18 : 27 : 35.521 [ Xmemcached - Reactor - 2 ] DEBUG com . google . code . yanf4j . core . impl . AbstractSession - send buffers : [ buffer : position = 0 , limit = 18 , capacity = 18 ] 18 : 27 : 35.521 [ Xmemcached - Reactor - 2 ] DEBUG com . google . code . yanf4j . core . impl . AbstractSession - read 9 bytes from channel 18 : 27 : 35.521 [ main ] DEBUG com . google . code . yanf4j . core . impl . AbstractSession - After encodingget Crunchify 18 : 27 : 35.521 [ Xmemcached - Reactor - 2 ] DEBUG net . rubyeye . xmemcached . impl . Optimizer - Optimieze merge buffer : get Crunchify 18 : 27 : 35.521 [ Xmemcached - Reactor - 2 ] DEBUG com . google . code . yanf4j . core . impl . AbstractSession - send buffers : [ buffer : position = 0 , limit = 15 , capacity = 15 ] ===== > Key : Crunchify , Value : null 18 : 27 : 35.521 [ Xmemcached - Reactor - 2 ] DEBUG com . google . code . yanf4j . core . impl . AbstractSession - read 5 bytes from channel 18 : 27 : 35.521 [ main ] DEBUG com . google . code . yanf4j . core . impl . AbstractSession - After encodingdelete key 18 : 27 : 35.521 [ Xmemcached - Reactor - 2 ] DEBUG net . rubyeye . xmemcached . impl . Optimizer - Optimieze merge buffer : delete key 18 : 27 : 35.521 [ Xmemcached - Reactor - 2 ] DEBUG com . google . code . yanf4j . core . impl . AbstractSession - send buffers : [ buffer : position = 0 , limit = 12 , capacity = 12 ] ===================================== Program Completed ===================================== 18 : 27 : 35.522 [ Xmemcached - Reactor - 2 ] DEBUG com . google . code . yanf4j . core . impl . AbstractSession - read 11 bytes from channel |
위의 결과에서 볼 수 있듯이 가치를 검색하고 저장하기 쉽고 프로덕션 Java 애플리케이션을 위한 중앙 캐싱 솔루션을 갖는 가장 빠른 방법입니다.
