memcachedDemo
来源:互联网 发布:python 最大似然估计 编辑:程序博客网 时间:2024/04/29 07:56
package
com.bennytian.memcached;
002
003
import
java.io.Serializable;
004
import
java.util.Date;
005
006
import
com.danga.MemCached.MemCachedClient;
007
import
com.danga.MemCached.SockIOPool;
008
009
@SuppressWarnings
(
"serial"
)
010
public
class
MemCached
implements
Serializable {
011
012
private
static
MemCachedClient mcc =
new
MemCachedClient();
013
private
static
MemCached memCached =
new
MemCached();
014
015
//服务区群
016
private
static
final
String [] services = {
"127.0.0.1:11211"
};
017
//服务器群各自对应的权重
018
private
static
final
Integer[] weights = {
1
};
019
//初始化连接数
020
private
static
final
Integer init_connection =
5
;
021
//最小连接数
022
private
static
final
Integer min_connection =
5
;
023
//最大连接数
024
private
static
final
Integer max_connection =
250
;
025
//最大处理时间 60*60*6
026
private
static
final
Long max_idle = 21600L ;
027
//主程序睡眠时间
028
private
static
final
Long maint_sleep = 10L;
029
030
static
{
031
// 获取socke连接池的实例对象
032
SockIOPool pool = SockIOPool.getInstance();
033
pool.setServers(services);
034
pool.setWeights(weights);
035
pool.setInitConn(init_connection);
036
pool.setMinConn(min_connection);
037
pool.setMaxConn(max_connection);
038
pool.setMaxIdle(max_idle);
039
pool.setMaintSleep(maint_sleep);
040
041
// 设置TCP的参数,连接超时等
042
pool.setNagle(
false
);
043
pool.setSocketTO(
3000
);
044
pool.setSocketConnectTO(
0
);
045
046
// 初始化连接池
047
pool.initialize();
048
049
//超过指定大小就压缩
050
mcc.setCompressEnable(
true
);
051
mcc.setCompressThreshold(65536L);
052
}
053
054
private
MemCached(){}
055
056
public
static
MemCached getInstance(){
057
return
memCached;
058
}
059
060
public
boolean
add(String key,Object value){
061
return
mcc.add(key, value);
062
}
063
064
public
boolean
add(String key,Object value,Date expiry){
065
return
mcc.add(key, value, expiry);
066
}
067
068
public
boolean
replace(String key, Object value){
069
return
mcc.replace(key, value);
070
}
071
072
public
boolean
replace(String key, Object value, Date expiry){
073
return
mcc.replace(key, value, expiry);
074
}
075
076
public
Object get(String key){
077
return
mcc.get(key);
078
}
079
080
public
static
void
main(String[] args) {
081
MemCached cache = MemCached.getInstance();
082
Date d =
new
Date();
083
cache.add(
"11"
, cache);
084
cache.add(
"12"
,
new
String(
"1234"
));
085
/*for (int i = 0; i < 1000; i++) {
086
cache.add(i+"", cache);
087
}*/
088
System.out.println(cache.get("11"));
089
System.out.println(cache.get("12"));
090
System.out.println(System.currentTimeMillis()-d.getTime());
091
//System.out.print("get value : " + cache.get("hello"));
092
//System.out.println(cache.get("100"));
093
}
094
/*
095
安装命令
096
c:>memcached.exe -d install
097
启动兼分配内存
098
c:>memcached.exe -l 127.0.0.1 -m 32 -d start
099
100
*/
101
}
0 0