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.jb51.cc.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实例时使用连接的名称

相关文章

显卡天梯图2024最新版,显卡是电脑进行图形处理的重要设备,...
初始化电脑时出现问题怎么办,可以使用win系统的安装介质,连...
todesk远程开机怎么设置,两台电脑要在同一局域网内,然后需...
油猴谷歌插件怎么安装,可以通过谷歌应用商店进行安装,需要...
虚拟内存这个名词想必很多人都听说过,我们在使用电脑的时候...