Memcached Java客户端示例

很多时候我们想通过其他一些编程语言连接到memcached服务器,比如java和php。

Memcached Java客户端

有三种最常用的memcached java客户端API。

  • xmemcached
  • spymemcached
  • gwhalin memcached客户端

Memcached Java示例

在本示例使用了Greg Whalin memcached客户端,它易于理解和使用。 它提供了线程池的所有基本功能。下面是maven依赖项,将其包含在您的项目中。

<dependency>     <groupId>com.whalin</groupId>     <artifactId>Memcached-Java-Client</artifactId>     <version>3.0.2</version> </dependency> 

为了帮助您快速入门,这里提供了一个示例程序来展示可以使用memcached服务器执行的基本功能的使用。

 package com.yiibai.memcached.test;  import java.util.HashMap; import com.whalin.MemCached.MemCachedClient; import com.whalin.MemCached.SockIOPool;  public class MemcachedJavaClient {      /**      * MemcachedJavaClient program to show the usage of different functions      * that can be performed on Memcached server with Java Client      * @param args      */     public static void main(String[] args) {         //initialize the SockIOPool that maintains the Memcached Server Connection Pool         String[] servers = {"localhost:11111"};         SockIOPool pool = SockIOPool.getInstance("Test1");         pool.setServers( servers );         pool.setFailover( true );         pool.setInitConn( 10 );         pool.setMinConn( 5 );         pool.setMaxConn( 250 );         pool.setMaintSleep( 30 );         pool.setNagle( false );         pool.setSocketTO( 3000 );         pool.setAliveCheck( true );         pool.initialize();         //Get the Memcached Client from SockIOPool named Test1         MemCachedClient mcc = new MemCachedClient("Test1");         //add some value in cache         System.out.println("add status:"+mcc.add("1", "Original"));         //Get value from cache         System.out.println("Get from Cache:"+mcc.get("1"));          System.out.println("add status:"+mcc.add("1", "Modified"));         System.out.println("Get from Cache:"+mcc.get("1"));          //use set function to add/update value, use replace to update and not add         System.out.println("set status:"+mcc.set("1","Modified"));         System.out.println("Get from Cache after set:"+mcc.get("1"));          //use delete function to delete key from cache         System.out.println("remove status:"+mcc.delete("1"));         System.out.println("Get from Cache after delete:"+mcc.get("1"));          //Use getMulti function to retrieve multiple keys values in one function         // Its helpful in reducing network calls to 1         mcc.set("2", "2");         mcc.set("3", "3");         mcc.set("4", "4");         mcc.set("5", "5");         String [] keys = {"1", "2","3","INVALID","5"};         HashMap<String,Object> hm = (HashMap<String, Object>) mcc.getMulti(keys);          for(String key : hm.keySet()){             System.out.println("KEY:"+key+" VALUE:"+hm.get(key));         }     }  } 

上面的memcache java客户端程序的输出是:

 add status:true Get from Cache:Original add status:false Get from Cache:Original set status:true Get from Cache after set:Modified remove status:true Get from Cache after delete:null KEY:3 VALUE:3 KEY:2 VALUE:2 KEY:1 VALUE:null KEY:INVALID VALUE:null KEY:5 VALUE:5 

如果要连接到多个memcached服务器,则必须创建多个SockIOPool实例,然后在获取MemcacheClient实例时使用连接的名称。

原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/264079.html

(0)
上一篇 2022年6月6日
下一篇 2022年6月6日

相关推荐

发表回复

登录后才能评论