java使用memcached缓存

服务器端安装,部署,启动:

1.下载libevent,安装libevent
cd /tmp
wget https://github.com/downloads/libevent/libevent/libevent-2.0.21-stable.tar.gz
tar -zxvf libevent-2.0.21-stable.tar.gz
cd libevent-2.0.21-stable
./configure --prefix=/usr/local/libevent
make && make install

2.下载memcached,安装memcached
wget http://memcached.org/latest 
tar -zxvf memcached-1.4.17.tar.gz
cd memcached-1.4.17
./configure --prefix=/usr/local/memcache --with-libevent=/usr/local/libevent/
make &&  make install
cd /usr/local/memcached/bin
./memcached -d -m 64 -u root -p 55001 -c 2048 

查看memcached的状态
ps -ax|grep memcache

链接到memcache
telnet cache.expai.com 55001
查看状态
stats

查看缓存占了多少空间
cat /proc/id/statm
启动参数
./memcached -d -m 1024 -u root -p 55001 -c 2048

memcached启动参数

-p 指定端口号(默认11211)  
-U <num> UDP监听端口 (默认: 11211, 0 时关闭) 
-s <file>     用于监听的UNIX套接字路径(禁用网络支持)
-a <mask>     UNIX套接字访问掩码,八进制数字(默认:0700)
-m 指定最大使用内存大小(默认64MB)   
-t 线程数(默认4)

-l <ip_addr> 绑定地址 (默认:所有都允许,无论内外网或者本机更换IP,有安全隐患,若设置为127.0.0.1就只能本机访问) 
-d start 启动memcached服务   
-d restart 重起memcached服务   
-d stop|shutdown 关闭正在运行的memcached服务   
-u <username> 绑定使用指定用于运行进程 <username>  (只有root用户可以使用这个参数)
-P <file> 将PID写入文件<file>,这样可以使得后边进行快速进程终止, 需要与 -d 一起使用
-m 最大内存使用,单位MB。默认64MB     www.2cto.com  

-M 内存耗尽时返回错误,而不是删除项   
-c 最大同时连接数,默认是1024 
-f 块大小增长因子,默认是1.25 
-n <bytes>最小分配空间,key+value+flags默认是48
-k锁定所有内存页。注意你可以锁定的内存上限。

试图分配更多内存会失败的,所以留意启动守护进程时所用的用户可分配的内存上限。
(不是前面的 -u <username> 参数;在sh下,使用命令"ulimit -S -l NUM_KB"来设置。)
-v 提示信息(在事件循环中打印错误/警告信息。)
-vv 详细信息(还打印客户端命令/响应)
-vvv 超详细信息(还打印内部状态的变化)
-h 打印这个帮助信息并退出。
-i  打印memcached和libevent的许可。
-L 尝试使用大内存页(如果可用的话)。提高内存页尺寸可以减少"页表缓冲(TLB)"丢失次数,提高运行效率。  www.2cto.com  

              为了从操作系统获得大内存页,memcached会把全部数据项分配到一个大区块。
-D <char>     使用 <char> 作为前缀和ID的分隔符。
              这个用于按前缀获得状态报告。默认是":"(冒号)。
              如果指定了这个参数,则状态收集会自动开启;如果没指定,则需要用命令"stats detail on"来开启。
-t <num>      使用的线程数(默认:4)
-R 每个连接可处理的最大请求数。
-C 禁用CAS。
-b 设置后台日志队列的长度(默认:1024)
-B 绑定协议 - 可能值:ascii,binary,auto(默认)
-I 重写每个数据页尺寸。调整数据项最大尺寸。

默认memcache会监听11221端口,如果想清空服务器上memecache的缓存,大家一般使用的是:
telnet localhost 11211
flush_all

同样也可以使用:
echo "flush_all" | nc localhost 11211

使用flush_all 后并不是删除memcache上的key,而是置为过期

java客户端使用:

package com.expai.utils;

import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.List;

import org.apache.ibatis.session.SqlSession;
import org.apache.log4j.Logger;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoOperations;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import com.danga.MemCached.MemCachedClient;
import com.danga.MemCached.SockIOPool;
import com.expai.mapper.advertise.AdverTiseMapper;
import com.expai.mapper.user.UserMapper;
import com.expai.model.hotimage.HotImage;
import com.expai.model.ir.Advertise;
import com.expai.model.user.User;
import com.mongodb.BasicDBList;
import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.DBObject;

/**
 * 
 * Description:
 * DateTime 2014年1月21日 下午4:19:28
 * @author louyj
 * @company expai
 * @version 1.0
 *
 */
public class MemCachedUtil {

	private static final Logger logger = Logger.getLogger(MemCachedUtil.class);

	 //构建缓存客户端
	 private static MemCachedClient  cachedClient;
	// 单例模式实现客户端管理类  
     private static MemCachedUtil INSTANCE = new MemCachedUtil();
     private MemCachedUtil() {  
         cachedClient = new MemCachedClient();  
         cachedClient.setPrimitiveAsString(true);
         // cachedClient.setSanitizeKeys(arg0)
         //获取连接池实例  
         SockIOPool pool = SockIOPool.getInstance();  
         // http://:/
         //设置缓存服务器地址,可以设置多个实现分布式缓存  服务器列表和其权重
         // pool.setServers(new String[]{"115.29.251.56:55001,115.29.251.56:55002,115.29.251.56:55003"});  
         //pool.setServers(new String[]{"10.162.57.11:55001"});  

        pool.setServers(new String[]{"s4.expai.com:55001"});  
        //  pool.setServers(new String[]{"192.168.81.137:55001"});  

        Integer[] weights = {3};
        //,"10.162.57.11:55001","10.162.57.11:55002","10.162.57.11:55003"
        pool.setWeights( weights );
         //pool.setServers(new String[]{"192.168.4.39:55001"});  
         //设置初始连接5  
         pool.setInitConn(5);  
         //设置最小连接5  
         pool.setMinConn(5);  
         //设置最大连接2048  
         pool.setMaxConn(2048);  
         //设置每个连接最大空闲时间3个小时  
         pool.setMaxIdle(1000 * 60 * 60 * 3);  

         pool.setMaintSleep(30);  

         pool.setNagle(false);  
         pool.setSocketTO(3000);  
         pool.setSocketConnectTO(0);  
         pool.initialize(); 
     } 

     /** 
      * 获取缓存管理器唯一实例 
      * @return 
      */  
     public static MemCachedUtil getInstance() {  
         return INSTANCE;  
     } 

     public void add(String key, Object value) {  
         cachedClient.add(key, value);  
     }  

       //int设置缓存失效时间,相对时间毫秒
     public void add(String key, Object value, int milliseconds) {  
         cachedClient.add(key, value, milliseconds);  
     }  
     public void set(String key, Object value) {  
    	 cachedClient.set(key, value);  
     }  

     //int设置缓存失效时间,相对时间毫秒
     public void set(String key, Object value, int milliseconds) {  
    	 cachedClient.set(key, value, milliseconds);  
     }  

     public void remove(String key) {  
         cachedClient.delete(key);  
         cachedClient.flushAll();
     }  

     @SuppressWarnings("deprecation")
	public void remove(String key, int milliseconds) {  
         cachedClient.delete(key, milliseconds, new Date());  
         cachedClient.flushAll();
     }  

     public void update(String key, Object value, int milliseconds) {  
         cachedClient.replace(key, value, milliseconds);  
     }  

     public void update(String key, Object value) {  
         cachedClient.replace(key, value);  
     }  

     public Object get(String key) {  
         return cachedClient.get(key);  
     }  
     public void getAll(){
      logger.debug(cachedClient);
     }

     /**
      * 初始化识别信息缓存
      */
     public static void initMemcachedIdenData(){
     	SqlSession session = null;
 		session = SessionFactory.getSession();
 	    AdverTiseMapper adMap = null;
 	    adMap = session.getMapper(AdverTiseMapper.class);
 	    List<Advertise> advertiseList = adMap.getAdvertises();
 	    if(advertiseList!=null&&advertiseList.size()>0){
 	    	for(Advertise advertise:advertiseList){
 	    		if(null!=advertise.getUrl()&&!advertise.getUrl().equals("")){
 	    			logger.debug("Advertise"+advertise.getId()+"的识别结果为:"+advertise.getUrl());
 	    			MemCachedUtil.getInstance().set("adveriden"+advertise.getId(), advertise.getUrl());
 	    		}else{
 	    			logger.debug("Advertise"+advertise.getId()+"的识别结果为:"+advertise.getUrl());
 	    			MemCachedUtil.getInstance().set("adveriden"+advertise.getId(), "url is null");
 	    		}
 	    		if(null!=advertise.getKeyword()&&!advertise.getKeyword().equals("")){
 	    			logger.debug("Advertise"+advertise.getId()+"的关键字为:"+advertise.getKeyword());
 	    			MemCachedUtil.getInstance().set("adverkey"+advertise.getId(), advertise.getKeyword());
 	    		}else{
 	    			logger.debug("Advertise"+advertise.getId()+"的关键字为:"+advertise.getKeyword());
 	    			MemCachedUtil.getInstance().set("adverkey"+advertise.getId(), "key is null");
 	    		}
 	    		if(null!=advertise.getImage()&&!advertise.getImage().equals("")){
 	    			logger.debug("Advertise"+advertise.getId()+"的图片地址为:"+advertise.getImage());
 	    			MemCachedUtil.getInstance().set("adverimage"+advertise.getId(), advertise.getImage());
 	    		}else{
 	    			logger.debug("Advertise"+advertise.getId()+"的图片地址为:"+advertise.getImage());
 	    			MemCachedUtil.getInstance().set("adverimage"+advertise.getId(), "image is null");
 	    		}
 	    	}
 	    }
 	    //初始化tb_newspaper里的识别信息
 	   List<Advertise> newspaperList = adMap.getNewspapers();
	    if(newspaperList!=null&&newspaperList.size()>0){
	    	for(Advertise advertise:newspaperList){
	    		if(null!=advertise.getUrl()&&!advertise.getUrl().equals("")){
	    			logger.debug("Advertise"+advertise.getId()+"的识别结果为:"+advertise.getUrl());
	    			MemCachedUtil.getInstance().set("newsiden"+advertise.getId(), advertise.getUrl());
	    		}else{
	    			logger.debug("Advertise"+advertise.getId()+"的识别结果为:"+advertise.getUrl());
	    			MemCachedUtil.getInstance().set("newsiden"+advertise.getId(), "url is null");
	    		}
	    		if(null!=advertise.getKeyword()&&!advertise.getKeyword().equals("")){
 	    			logger.debug("Advertise"+advertise.getId()+"的关键字为:"+advertise.getKeyword());
 	    			MemCachedUtil.getInstance().set("newskey"+advertise.getId(), advertise.getKeyword());
 	    		}else{
 	    			logger.debug("Advertise"+advertise.getId()+"的关键字为:"+advertise.getKeyword());
 	    			MemCachedUtil.getInstance().set("newskey"+advertise.getId(), "key is null");
 	    		}
	    		if(null!=advertise.getImage()&&!advertise.getImage().equals("")){
 	    			logger.debug("Advertise"+advertise.getId()+"的图片地址为:"+advertise.getImage());
 	    			MemCachedUtil.getInstance().set("newsimage"+advertise.getId(), advertise.getImage());
 	    		}else{
 	    			logger.debug("Advertise"+advertise.getId()+"的图片地址为:"+advertise.getImage());
 	    			MemCachedUtil.getInstance().set("newsimage"+advertise.getId(), "image is null");
 	    		}
	    	}
	    }
     }

     */
     /**
      * 初始化广告信息tb_embedAdvertise
      * @throws UnsupportedEncodingException 
      */
     public static void initMemcachedEmbedDate() throws UnsupportedEncodingException{
    	 long st = System.currentTimeMillis();
     	DB db = MongdbUtil.startMongoDBConn();
     	DBCollection inputCollection =db.getCollection("tb_embedAdvertise"); 
     	DBObject ref = new BasicDBObject();
     	DBCursor cursor = inputCollection.find(ref);
 		List<DBObject> list = cursor.toArray();
 		long et = System.currentTimeMillis();
 		long diff = et-st;
 		logger.info("query all advertise expand time : " + diff);
 		MemCachedUtil instance = MemCachedUtil.getInstance();
  		if(list!=null&&list.size()>0){
  			logger.info("all advertise total number : " + list.size());
  			for(int i =0, n = list.size(); i < n; i++){
  				DBObject object = list.get(i);
  				Object url = object.get("url");
  				Object loc = object.get("loc");
  				String str = new String(object.toString().getBytes("UTF-8"));
 				if(null!=url && !url.toString().equals("")){
 					logger.debug("The is cache Advertise url======"+"em" + new MD5().getMD5ofStr(url.toString()));
 					instance.add("em" + new MD5().getMD5ofStr(url.toString()), str);
 				}

 				if(null != loc && !loc.toString().equals("")){
 					logger.debug("The is cache Advertise loc======"+"em" + new MD5().getMD5ofStr(loc.toString()));
 					instance.add("em" + new MD5().getMD5ofStr(loc.toString()), str);
 				}
  				Utils.writeText(str, "/data/memcached.txt");
  			}
  			logger.info("memcached add complete");
  		}
  		MongdbUtil.stopMondoDBConn();
     }

     public static void initMemcachedImageAll(){
    	 	DB db = MongdbUtil.startMongoDBConn();
	    	DBCollection inputCollection =db.getCollection("tb_hotimage_failure"); 
	    	DBCursor cursor = inputCollection.find();
	    	List<DBObject> list = cursor.toArray();
	    	for(int i =0, n = list.size(); i < n; i++){
				DBObject object = list.get(i);
				Object shortlocation = object.get("shortlocation");
				if(null!=shortlocation && !shortlocation.toString().equals("")){
					logger.debug("add tb_hotimage_failure======================"+shortlocation);
					MemCachedUtil.getInstance().set(new MD5().getMD5ofStr(String.valueOf(shortlocation)),"failure");
				}
			}

	    	DBCollection inputCollection1 =db.getCollection("tb_hotimage"); 
	    		BasicDBObject obj = new BasicDBObject();
	    		BasicDBObject query = new BasicDBObject();
	    		BasicDBList values = new BasicDBList();  
	    		values.add("sdchina.com");
	    		values.add("gywb.cn");
	    		values.add("nen.com");
	    		values.add("yunying");
	    		values.add("shangdu.com");
	    		values.add("voc.com");
	    		values.add("0554news.com");
	    		query.append("$in", values);
	    		obj.append("categoryId", query);
	    	DBCursor cursor1 = inputCollection1.find(obj);
	    	List<DBObject> list1 = cursor1.toArray();
	    	for(int i =0, n = list1.size(); i < n; i++){
				DBObject object1 = list1.get(i);
				Object shortlocation = object1.get("shortlocation");
				if(null!=shortlocation && !shortlocation.toString().equals("")){
					logger.debug("add tb_hotimage======================"+shortlocation);
					MemCachedUtil.getInstance().set(new MD5().getMD5ofStr(String.valueOf(shortlocation)),"success");
				}
			}
	    	MongdbUtil.stopMondoDBConn();
     }
	public static void main(String[] args) {
		initMemcachedImageAll();
	}
}

memcached jar包:

        <dependency>
  	   <groupId>memcached</groupId>
  	   <artifactId>java_memcached-release</artifactId>
  	   <version>2.6.6</version>
        </dependency>

Tagged: ,

Comments are closed.