一:memcached缓存
1:下载memcached 以及memcached的源码 本文说的是在windows下的memcached
2:解压之后打开cmd窗口
输入memcached.exe -d install 安装memcached服务
输入memcached.exe -d start 启动mamcache服务
输入memcached.exe -d stop 停止服务
3:把下载过来的源码拷入项目
4:写一个MemcachedFactory 配置一些信息
package com.xxx.memcached;
import java.util.Date;
public class MemcachedFactory {
private static MemCachedClient mc = new MemCachedClient();
private static MemcachedFactory factory = new MemcachedFactory();
//设置服务器连接池
static{
//服务器列表和其权重
String[] servers = {"127.0.0.1:11211"};
Integer[] weights = { 3 };
//获取socke连接池的实例对象
SockIOPool pool = SockIOPool.getInstance();
//设置服务器信息
pool.setServers(servers);
pool.setWeights(weights);
//设置初始连接数 最小连接数 最大连接数以及最大处理时间
pool.setInitConn(5);
pool.setMinConn(5);
pool.setMaxConn(250);
pool.setMaxIdle(1000*60*60*6);
//设置主线程的睡眠时间
pool.setMaintSleep(30);
//设置tcp参数 连接超时等
pool.setNagle(false);
pool.setSocketTO(3000);
pool.setSocketConnectTO(0);
//初始化连接
pool.initialize();
//压缩设置 设置指定大小(单位K)的数据被压缩处理
mc.setCompressEnable(true);
mc.setCompressThreshold(64*1024);
}
public static MemcachedFactory getInstance(){
return factory;
}
public boolean set(String key,Object value){
return mc.add(key, value);
}
public boolean set(String key,Object value,Date mil){
return mc.add(key, value,mil);
}
public Object get(String key){
return mc.get(key);
}
public boolean del(String key){
return mc.delete(key);
}
}
5:使用
package com.xxx.test;
import com.xxx.memcached.MemcachedFactory;
public class TestMem {
public static void main(String[] args){
MemcachedFactory factory = MemcachedFactory.getInstance();
//factory.set("aa", 12333);
//factory.del("aa");
// System.out.println(factory.get("aa"));
Xxx x = new Xxx();
x.setName("xxx");
x.setAge(22);
factory.set("1", x);
Xxx l1 = (Xxx)factory.get("1");
System.out.println("姓名:"+l1.getName()+"年龄:"+l1.getAge());
}
}
二:redis
1:下载windows版本redis,解压
2:打开cmd窗口
输入redis-server.exe 启动redis服务 关闭cmd窗口则关闭
3:再开一个cmd窗口
输入 redis-cli.exe -h 172.0.0.1 -p 6379 配置信息
4:导入redis相关jar包
5:写一个RedisFactory
package com.xxx.redis;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
public class JedisFactory {
private static JedisPoolConfig config;
private static JedisPool pool;
private static String HOST = "127.0.0.1";
private static Integer PORT = 6379;
private static Integer TIME_OUT = 30 * 1000;
private static JedisFactory factory = null;
static{
config = new JedisPoolConfig();
config.setMaxActive(100);
config.setMaxIdle(20);
config.setMaxWait(1000);
config.setTestOnBorrow(false);
pool = new JedisPool(config, HOST,PORT,TIME_OUT);
}
public static JedisFactory getInstance(){
if(factory != null){
return factory;
}else{
factory = new JedisFactory();
return factory;
}
}
public Jedis getJedis(){
Jedis jedis = null;
jedis = pool.getResource();
jedis.connect();
return jedis;
}
public void set(String key ,String value){
Jedis jedis = getJedis();
jedis.set(key, value);
}
public String get(String key){
Jedis jedis = getJedis();
return jedis.get(key);
}
}
6:使用
package com.xxx.test;
import com.xxx.redis.JedisFactory;
public class TestRedis {
public static void main(String[] args){
JedisFactory factory = JedisFactory.getInstance();
factory.set("aaa", "eeeeeeeee");
System.out.println(factory.get("aaa"));
}
}