标签:
public static void main(String[] args) throws IOException, TimeoutException, InterruptedException, MemcachedException
{
MemcachedClientBuilder builder = new XMemcachedClientBuilder(AddrUtil.getAddresses("192.168.110.100:11211"));
MemcachedClient client = builder.build();
// set 命令用于将 value(数据值) 存储在指定的 key(键) 中
System.out.println("set :" + client.set("key001", 3600, "value001"));
// add 命令用于将 value(数据值) 存储在指定的 key(键) 中
System.out.println("add :" + client.add("key002", 3600, "value002"));
// replace 命令用于替换已存在的 key(键) 的 value(数据值)
System.out.println("replace :" + client.replace("key002", 3600, "value003"));
// append 命令用于向已存在 key(键) 的 value(数据值) 后面追加数据
System.out.println("append :" + client.append("key002", "value004"));
// prepend 命令用于向已存在 key(键) 的 value(数据值) 前面追加数据
System.out.println("prepend :" + client.prepend("key002", "value005"));
// CAS 命令用于执行一个"检查并设置"的操作它仅在当前客户端最后一次取值后,该key 对应的值没有被其他客户端修改的情况下, 才能够将值写入
GetsResponse<String> result = client.gets("key002");
long cas = result.getCas();
System.out.println("cas :" + client.cas("key002", 3600, "value002", cas));
// get 命令获取存储在 key(键) 中的 value(数据值) ,如果 key 不存在,则返回空
System.out.println("get :" + client.get("key001"));
// gets 命令获取带有 CAS 令牌存 的 value(数据值) ,如果 key 不存在,则返回空
System.out.println("gets :" + client.gets("key002"));
// delete 命令用于删除已存在的 key(键)
System.out.println("delete :" + client.delete("key002"));
// incr 与 decr 命令用于对已存在的 key(键) 的数字值进行自增或自减操作
System.out.println("incr :" + client.incr("key002", 1L));
// stats 命令用于返回统计信息例如 PID(进程号)、版本号、连接数等
System.out.println("stats :" + client.stats(new InetSocketAddress("192.168.110.100", 11211)));
System.out.println("getStats :" + client.getStats());
// stats items 命令用于显示各个 slab 中 item 的数目和存储时长(最后一次访问距离现在的秒数)
System.out.println("getStatsByItem items :" + client.getStatsByItem("items"));
// stats slabs 命令用于显示各个slab的信息,包括chunk的大小、数目、使用情况等
System.out.println("getStatsByItem slabs :" + client.getStatsByItem("slabs"));
// stats sizes 命令用于显示所有item的大小和个数
System.out.println("getStatsByItem sizes :" + client.getStatsByItem("sizes"));
// flush_all 命令用于用于清理缓存中的所有 key=>value(键=>值) 对
client.flushAll();
System.out.println("OK!!!");
client.shutdown();
}
public static void main(String[] args) throws IOException, TimeoutException, InterruptedException, MemcachedException
{
// 集群服务器地址
String server = "192.168.110.100:11211 192.168.110.101:11211 192.168.110.102:11211 192.168.110.103:11211";
// 设置权重
int[] weights = new int[] { 1, 2, 3, 4 };
MemcachedClientBuilder builder = new XMemcachedClientBuilder(AddrUtil.getAddresses(server), weights);
// 客户端集群算法:一致性哈希。默认是:按照key的哈希值模以连接数得到的余数
builder.setSessionLocator(new KetamaMemcachedSessionLocator());
MemcachedClient client = builder.build();
// 返回可用的memcached服务器列表
System.out.println(client.getAvailableServers());
for (int i = 0; i < 100; i++)
{
System.out.print(client.set("key-" + i, 3600, "value-" + i) + "-");
}
System.out.println("\nOK!!!");
client.shutdown();
}
public static void main(String[] args) throws IOException, InterruptedException
{
MemcachedClientBuilder builder = new XMemcachedClientBuilder();
MemcachedClient client = builder.build();
System.out.println(client.getAvailableServers());
client.addServer("192.168.110.100", 11211, 1);
client.addServer("192.168.110.101", 11211, 2);
client.addServer("192.168.110.102", 11211, 3);
client.addServer("192.168.110.103", 11211, 4);
System.out.println(client.getAvailableServers());
client.removeServer("192.168.110.102:11211 192.168.110.103:11211");
Thread.sleep(100); // 由于xmemcached是多线程操作,所以休眠一段时间才能看到效果
System.out.println(client.getAvailableServers());
System.out.println("OK!!!");
client.shutdown();
}
public static void main(String[] args) throws IOException
{
MemcachedClientBuilder builder = new XMemcachedClientBuilder(AddrUtil.getAddresses("192.168.110.100:11211"));
// 设置客户端集群的算法实现
builder.setSessionLocator(new KetamaMemcachedSessionLocator());
// 使用二进制协议
builder.setCommandFactory(new BinaryCommandFactory());
// Nio连接池
builder.setConnectionPoolSize(5);
// Failure模式和standby节点
builder.setFailureMode(true);
// 与tokyotyrant交互
builder.setTranscoder(new TokyoTyrantTranscoder());
MemcachedClient memcachedClient = builder.build();
memcachedClient.shutdown();
}
MemcachedClient builder=new XmemcachedClientBuilder(AddrUtil.getAddressMap("localhost:11211,localhost:11212 host2:11211,host2:11212"));
<!-- Memcached客户端工厂配置 -->
<bean name="memcachedClient" class="net.rubyeye.xmemcached.utils.XMemcachedClientFactoryBean" destroy-method="shutdown">
<!-- Memcached的服务器集群 -->
<property name="servers">
<!-- <value>host1:port1 host2:port2 host3:port3</value> -->
<value>${memcached.host1}</value>
</property>
<!-- 客户端节点权重设置,对应上面的servers配置 -->
<property name="weights">
<list>
<value>${memcached.host1.weight}</value>
</list>
</property>
<!-- 设置连接池的大小 -->
<property name="connectionPoolSize" value="2"></property>
<!-- 使用二进制协议通讯,默认为TextCommandFactory -->
<property name="commandFactory">
<bean class="net.rubyeye.xmemcached.command.BinaryCommandFactory"></bean>
</property>
<!-- 分布式策略 -->
<property name="sessionLocator">
<bean class="net.rubyeye.xmemcached.impl.KetamaMemcachedSessionLocator"></bean>
</property>
<!-- 序列化转码器 -->
<property name="transcoder">
<bean class="net.rubyeye.xmemcached.transcoders.SerializingTranscoder" />
</property>
<!-- 缓冲区分配器 -->
<property name="bufferAllocator">
<bean class="net.rubyeye.xmemcached.buffer.SimpleBufferAllocator"></bean>
</property>
<!-- 设置验证信息 -->
<!--
<property name="authInfoMap">
<map>
<entry key-ref="server1">
<bean class="net.rubyeye.xmemcached.auth.AuthInfo" factory-method="typical">
<constructor-arg index="0">
<value>index</value>
</constructor-arg>
<constructor-arg index="1">
<value>index-pd</value>
</constructor-arg>
</bean>
</entry>
</map>
</property>
-->
</bean>
@Autowired
@Qualifier("memcachedClient")
private MemcachedClient memcachedClient;
标签:
原文地址:http://www.cnblogs.com/LiZhiW/p/4853894.html