2019独角兽企业重金招聘Python工程师标准>>>
今天捣鼓了下mencached 还不错
依赖库
commons-pool-1.5.6
java_memcached-release_2.6.6
slf4j-api-1.6.1
slf4j-simple-1.6.1
mencached
第三方维护的win32版
http://splinedancer.com/memcached-win32/
package com.Mencached;
import com.danga.MemCached.MemCachedClient;
import com.danga.MemCached.SockIOPool;
import java.util.Date;
/**
* Created with IntelliJ IDEA.
* User: CHENLEI
* Date: 12-10-25
* Time: 下午2:15
* To change this template use File | Settings | File Templates.
* java数据提交到mencached 测试
*/
public class MenCached {
private static MemCachedClient mcc = new MemCachedClient();
// set up connection pool once at class load
static {
// server list and weights
String[] servers =
{
"127.0.0.1:11211",
// "server2.mydomain.com:1624",
// "server3.mydomain.com:1624"
};
Integer[] weights = { 3 };
// SockIOPool
SockIOPool pool = SockIOPool.getInstance();
// set the servers and the weights
pool.setServers( servers );
pool.setWeights( weights );
// initialize、最小和最大连接数以及最大处理时间
pool.setInitConn( 5 );
pool.setMinConn( 5 );
pool.setMaxConn( 250 );
pool.setMaxIdle( 1000 * 60 * 60 * 6 );
pool.setMaintSleep(10);//thread sleep
pool.setNagle( false );
pool.setSocketTO( 3000 ); //timeout read 3secs
pool.setSocketConnectTO( 0 );
// initialize the connection pool
pool.initialize();
}
private MenCached(){}
//获取MenCached操作引用
public static MenCached getInstance()
{
return singlton.mencached;
}
private static final class singlton{
private static final MenCached mencached=new MenCached();
}
/**
* 添加一个指定的值到缓存中.
* @param key
* @param value
* @return
*/
public boolean add(String key, Object value)
{
return mcc.add(key, value);
}
public boolean add(String key, Object value, Date expiry)
{
return mcc.add(key, value, expiry);
}
/**
* 替换一个指定的值到缓存中.
* @param key
* @param value
* @return
*/
public boolean replace(String key, Object value)
{
return mcc.replace(key, value);
}
/**
*
* @param key
* @param value
* @param expiry 过期
* @return
*/
public boolean replace(String key, Object value, Date expiry)
{
return mcc.replace(key, value, expiry);
}
/**
* 删除一个指定的值到缓存中.
* @param key
* @param
* @return
*/
public boolean delete(String key)
{
return mcc.delete(key);
}
/**
* 根据指定的关键字获取对象.
* @param key
* @return
*/
public Object get(String key)
{
return mcc.get(key);
}
public static void main(String[] args)
{
MenCached cache = MenCached.getInstance();
long t1=System.currentTimeMillis();
for (int i=0;i<10;i++){
cache.add(i+"",i) ;
System.out.println("value:"+cache.get(i+""));
}
System.out.println(System.currentTimeMillis()-t1);
// System.out.println("get value : " + cache.get("key"));
}
}
对于redis ,你需要下载redis,这里我用的是window版本,配置好redis.conf后 start redis-server.exe redis.conf启动redis.
测试,你需要https://github.com/xetorthio/jedis/downloads
jedis
java客户端
使用简单,但是相关的文献较少,要想掌握,看api,或则看看源码咯,也不是很多
package com.Mencached;
import redis.clients.jedis.Jedis;
/**
* Created with IntelliJ IDEA.
* User: CHENLEI
* Date: 12-10-27
* Time: 上午11:34
* To change this template use File | Settings | File Templates.
* redis test
*/
public class redisDeamo {
//在redis.conf中配置hosts prot
private static final Jedis jedis = new Jedis("127.0.0.1",9090);
public static redisDeamo getInstance(){
return sigtlon.redis;
}
private redisDeamo(){
}
private static final class sigtlon{
private static final redisDeamo redis=new redisDeamo();
}
public void setJedis(java.lang.String key, java.lang.String value){
jedis.set(key, value);
}
public Object getJedis(java.lang.String key){
return jedis.get(key);
}
//test
public static void main(String[]args){
redisDeamo.getInstance().setJedis("foo","909090");
String value = (String) redisDeamo.getInstance().getJedis("foo");
System.out.println("获取的值:"+ value);
}
}
实际的应用 还得深入的去考究