使用 memcache java 客户端示例
使用memcachejava客户端示例
原文出处:http://blog.chenlb.com/2008/12/use-memcache-java-client-demo.html
具说大数据量,用这个不错。
memcachejava客户端api。地址:http://www.danga.com/memcached/apis.bml,有种客户端,我这使用DustinSallings版的(已经在googlecode了http://code.google.com/p/spymemcached/)。
spymemcached的下载地址:http://spymemcached.googlecode.com/files/memcached-2.2.jar它依赖spy.jar,spy.jar的下载地址:http://bleu.west.spy.net/~dustin/repo/spy/jars/spy-2.4.jar
把两个jar放到classpath下,然后写代码试用下:写入
package com.chenlb.use; import java.io.IOException; import java.io.Serializable; import java.net.InetSocketAddress; import java.util.Date; import net.spy.memcached.MemcachedClient; public class MemcacheUse { private static class MyData implements Serializable { private static final long serialVersionUID = 1L; private long d = new Date().getTime(); public String toString() { return "my data ["+d+"]"; } } public static void main(String[] args) throws IOException { MyData myData = new MyData(); MemcachedClient c=new MemcachedClient(new InetSocketAddress("localhost", 11211)); // Store a value (async) for one hour c.set("someKey", 3600, myData); // Retrieve a value (synchronously). Object myObject=c.get("someKey"); c.shutdown(); System.out.println(myObject); } }
读:主要是验证下,一小时后读是否有效。
package com.chenlb.use; import java.io.IOException; import java.net.InetSocketAddress; import net.spy.memcached.MemcachedClient; public class MemcacheGetUse { public static void main(String[] args) throws IOException { MemcachedClient c=new MemcachedClient(new InetSocketAddress("172.16.249.220", 11211)); // Retrieve a value (synchronously). Object myObject=c.get("someKey"); System.out.println(myObject); c.shutdown(); } }
相关推荐
郗瑞强 2020-08-16
85590296 2020-07-22
jkzyx 2020-06-29
luotuo 2020-06-26
LinuxJob 2020-06-26
ol0 2020-06-26
清溪算法君老号 2020-06-25
86251043 2020-06-13
CSDN0BLOG 2020-06-09
ol0 2020-05-26
andyhuabing 2020-05-22
程序员俱乐部 2020-05-06
83530391 2020-05-05
ol0 2020-05-02
83530391 2020-04-09
85590296 2020-03-25
carolAnn 2020-03-07
大脸猫脸大 2020-03-03
ol0 2020-02-18