memcache缓存


Memcached的安装
在1.4.5版本之前,memcached可以被安装成一个服务,但之后的版本中该功能被移除了。因此memcached的安装可以分为两类,第一类是1.4.5之前的版本,另一类是1.4.5之后的版本。

这里以1.4.5也就是之后的版本为例
1.将下载的文件解压到任意目录。
2.点开运行cmd,一直进入到下载的文件中,最后输入Memcached-x86 –p 11211 –m 128 –vv
  选项 说明
  -p 使用的TCP端口,默认为11211
  -m 最大内存大小默认为64m
  -vv 用very verbose模式启动,调试信息和错误输出到控制台
  -d 作为daemon在后台启动
3.将下载文件中的jar包导入到项目
4.添加一个辅助类
import java.util.Date;
import com.danga.MemCached.MemCachedClient;
import com.danga.MemCached.SockIOPool;

public class MemcachedUtil {

/**
     * memcached客户端单例
     */
    private static MemCachedClient cachedClient = new MemCachedClient();
    
    /**
     * 初始化连接池
     */
    static {
        //获取连接池的实例
        SockIOPool pool = SockIOPool.getInstance();
        
        //服务器列表及其权重
        String[] servers = {"127.0.0.1:11211"};
        Integer[] weights = {3};
        
        //设置服务器信息
        pool.setServers(servers);
        pool.setWeights(weights);
        
        //设置初始连接数、最小连接数、最大连接数、最大处理时间
        pool.setInitConn(10);
        pool.setMinConn(10);
        pool.setMaxConn(1000);
        pool.setMaxIdle(1000*60*60);
        
        //设置连接池守护线程的睡眠时间
        pool.setMaintSleep(60);
        
        //设置TCP参数,连接超时
        pool.setNagle(false);
        pool.setSocketTO(60);
        pool.setSocketConnectTO(0);
        
        //初始化并启动连接池
        pool.initialize();
        
        //压缩设置,超过指定大小的都压缩
//      cachedClient.setCompressEnable(true);
//      cachedClient.setCompressThreshold(1024*1024);
    }
    
    private MemcachedUtil(){
    }
    
    public static boolean add(String key, Object value) {
        return cachedClient.add(key, value);
    }
    
    public static boolean add(String key, Object value, Date expireDate) {
        return cachedClient.add(key, value, expireDate);
    }
   
    public static boolean add(String key, Object value, Integer millSeconds){
    return cachedClient.add(key, value, new Date(new Date().getTime()+millSeconds));
    }
    
    public static boolean put(String key, Object value) {
        return cachedClient.set(key, value);
    }
    
    public static boolean put(String key, Object value, Date expireDate) {
        return cachedClient.set(key, value, expireDate);
    }
   
    public static boolean put(String key, Object value, Integer millSeconds){
    return cachedClient.set(key, value, new Date(new Date().getTime()+millSeconds));
    }
    
    public static boolean replace(String key, Object value) {
        return cachedClient.replace(key, value);
    }
    
    public static boolean replace(String key, Object value, Date expireDate) {
        return cachedClient.replace(key, value, expireDate);
    }
   
    public static boolean replace(String key, Object value, Integer millSeconds){
    return cachedClient.replace(key, value, new Date(new Date().getTime()+millSeconds));
    }
    
    public static Object get(String key) {
        return cachedClient.get(key);
    }
   
   public static Object delete(String key) {
       return cachedClient.delete(key);
   }
}
5.被添加的缓存的类(实体类)必须实现序列化Serializable
6.自己写的方法中就可以类似这样写
                List<Goods> list=null;

String key=String.format("ALL_GOOD_%s",gname);
                //先从缓存中取值
list=(List<Goods>) MemcachedUtil.get(key);
if(list!=null){
System.out.println("在缓存中取到值了");
}else{
System.out.println("数据库取到的值");
list=goodService.getGoodlist(gname);
//向缓存中添加这个值(第三个参数是缓存的有效时间)
MemcachedUtil.add(key, list, 20000);
}
7.如果修改数据 然后 返回到查询的页面时 ,应该把缓存删除,调用MemcachedUtil.delete(key)方法

猜你喜欢

转载自abao1.iteye.com/blog/2358443