memcache java client_Memcache的客户端连接系列(一) Java

将如下Java代码复制到Eclipse Project中并修改代码。

密码模式代码示例

其中ip or domain name:port需要修改为1获取的缓存实例IP地址/域名和端口。userName和password需要修改为缓存实例的用户名和密码。

//java 连接加密的Memcached代码

import java.io.IOException;

import java.util.concurrent.ExecutionException;

import net.spy.memcached.AddrUtil;

import net.spy.memcached.ConnectionFactoryBuilder;

import net.spy.memcached.ConnectionFactoryBuilder.Protocol;

import net.spy.memcached.MemcachedClient;

import net.spy.memcached.auth.AuthDescriptor;

import net.spy.memcached.auth.PlainCallbackHandler;

import net.spy.memcached.internal.OperationFuture;

public class ConnectMemcached1

{

public static void main(String[] args)

{

final String connectionaddress = "ip or domain name:port";//"ip or domain name:port"

final String username = "userName";//用户名

final String password = "password";//密码

MemcachedClient client = null;

try

{

AuthDescriptor authDescriptor =

new AuthDescriptor(new String[] {"PLAIN"}, new PlainCallbackHandler(username,

password));

client = new MemcachedClient(

new ConnectionFactoryBuilder().setProtocol(Protocol.BINARY)

.setAuthDescriptor(authDescriptor)

.build(),

AddrUtil.getAddresses(connectionaddress));

String key = "memcached";//向Memcached中存一个key为"memcached"的数据

String value = "Hello World";//value为Hello World

int expireTime = 5; // 过期时间,单位s; 从写入时刻开始计时,超过expireTime s后,该数据过期失效,无法再读出;

doExcute(client, key, value, expireTime);//执行操作

}

catch (IOException e)

{

e.printStackTrace();

}

}

/**

*向Memcached写数据方法

*/

private static void doExcute(MemcachedClient client, String key, String value, int expireTime)

{

try

{

OperationFuture future = client.set(key, expireTime, value);

future.get();// spymemcached set()是异步的,future.get() 等待cache.set()操作结束,也可以不等待,用户根据自己需求选择;

System.out.println("Set操作成功");

System.out.println("Get操作:" + client.get(key));

Thread.sleep(6000);//等待6000毫秒,即6秒,该数据将会过期失效,无法再读出

System.out.println("6秒后再执行Get操作:" + client.get(key));

}

catch (InterruptedException e)

{

e.printStackTrace();

}

catch (ExecutionException e)

{

e.printStackTrace();

}

if (client != null)

{

client.shutdown();

}

}

}

免密模式代码示例

其中ip or domain name:port需要修改为1获取的缓存实例IP地址/域名和端口。

//java 连接免密的Memcached代码

import java.io.IOException;

import java.util.concurrent.ExecutionException;

import net.spy.memcached.AddrUtil;

import net.spy.memcached.BinaryConnectionFactory;

import net.spy.memcached.MemcachedClient;

import net.spy.memcached.internal.OperationFuture;

public class ConnectMemcached

{

public static void main(String[] args)

{

final String connectionaddress = "ip or domain name:port";//"ip or domain name:port"

MemcachedClient client = null;

try

{

client = new MemcachedClient(new BinaryConnectionFactory(), AddrUtil.getAddresses(connectionaddress));

String key = "memcached";//向Memcached中存一个key为"memcached"的数据

String value = "Hello World";//value为Hello World

int expireTime = 5; // 过期时间,单位s; 从写入时刻开始计时,超过 expireTime s后,该数据过期失效,无法再读出;

doExcute(client, key, value, expireTime);//执行操作

}

catch (IOException e)

{

e.printStackTrace();

}

}

/**

*向Memcached写数据方法

*/

private static void doExcute(MemcachedClient client, String key, String value, int expireTime)

{

try

{

OperationFuture future = client.set(key, expireTime, value);

future.get();// spymemcached set()是异步的,future.get() 等待cache.set()操作结束,也可以不等待,用户根据自己需求选择;

System.out.println("Set操作成功");

System.out.println("Get操作:" + client.get(key));

Thread.sleep(6000);//等待6000毫秒,即6秒,该数据将会过期失效,无法再读出

System.out.println("6秒后再执行Get操作:" + client.get(key));

}

catch (InterruptedException e)

{

e.printStackTrace();

}

catch (ExecutionException e)

{

e.printStackTrace();

}

if (client != null)

{

client.shutdown();

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值