将如下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();
}
}
}