首页 > 编程语言 > 详细

java + memcached安装使用

时间:2014-05-14 21:16:47      阅读:364      评论:0      收藏:0      [点我收藏+]


一:安装

(暂时先拿上手的windows做实验)
1.下载memcached.exe , 放在F:\memcached\ 下
2.在CMD下输入 "F:\memcached\memcached.exe -d install" 安装.
3.再输入:"F:\memcached\memcached.exe -d start" 启动。以后memcached将作为windows的一个服务每次开机时自动启动。这样服务器端已经安装完毕了。




二:java引包

以下的包都是网上找的, 传说是阿里某高人封的
alisoft-xplatform-asf-cache-2.5.1.jar
hessian-3.0.1.jar
stax-api-1.0.1.jar
wstx-asl-2.0.2.jar




三:测试文件

package com.xxx.util;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;


import com.alisoft.xplatform.asf.cache.memcached.client.MemCachedClient;
import com.alisoft.xplatform.asf.cache.memcached.client.SockIOPool;
import com.xxx.entity.attrGrid.Records;


public class MemcachedManager
{
    //创建MemCachedClient全局对象
    private static MemCachedClient mcc = new MemCachedClient();
    
    static
    {
       //创建服务器列表及其权重
       String[] servers = {"127.0.0.1:11211"};
       Integer[] weights = {3};
       
       //创建Socket连接池对象
       SockIOPool pool = SockIOPool.getInstance();
       
       //设置服务器信息
       pool.setServers(servers);
       pool.setWeights(weights);
       pool.setFailover(true);
       
       //设置初始连接数、最小和最大连接数以及最大处理时间
       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.setAliveCheck(true);
       
       //初始化连接池
       pool.initialize();
       
       //压缩设置,超过指定大小(单位为K)的数据都会被压缩
       mcc.setCompressEnable(true);
       mcc.setCompressThreshold(64 * 1024);
    }
    
    /**
     * 无参构造
     */
    protected MemcachedManager ()
    { 
    }
    
    protected static MemcachedManager instance = new MemcachedManager();//单例
    
    /**
     * 为受保护的对象提供一个公共的访问方法
     */
    public static MemcachedManager getInstance () 
    {
       return instance;
    }
    
    /**
     * 添加对象到缓存中,构成方法重载
     * @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);
    }
    public boolean replace (String key,Object value) 
    {
       return mcc.replace(key, value);
    }
    public boolean replace (String key,Object value,Date expiry)
    {
       return mcc.replace(key, value, expiry);
    }
    
    /**
     * 根据指定的关键字获取对象
     */
    public Object get(String key) {
       return mcc.get(key);
    }
    
    /**
     * 利用MemCached对象将集合存入缓存,并从缓存中取出
     */
    public static void main(String[] args)
    {
       //得到MemcachedManager实例
       MemcachedManager cache = MemcachedManager.getInstance();
       
       //1插入
       List<Records> recordsList = new ArrayList<Records>();
       for( int i =0 ; i<5 ; i++ )
       {
    	   Records records = new Records();
    	   records.setPageSize(i);
    	   
    	   recordsList.add(records);
    	   cache.add("SELECT aaa FROM bbb WHERE "+i,records);
       }
       System.out.println("插入完成==>");
       
       //2获取
       for( int i =0 ; i<9 ; i++ )
       {
    	   Records records = (Records)cache.get("SELECT aaa FROM bbb WHERE "+i);
    	   
    	   if( null == records  )
    	   {
    		   System.out.println("dao层 再去查"); 
    	   }
    	   else
    	   {   
    		   String str = records.getPageSize().toString();
    		   System.out.println("从缓存中取得的集合为:" + str);
    	   }
       }
    }
}



四:现象

2014 14:30:31 INFO  com.alisoft.xplatform.asf.cache.impl.DefaultCacheImpl - DefaultCache CheckService is start!
2014 14:30:31 INFO  com.alisoft.xplatform.asf.cache.memcached.client.MemCachedClient - ++++ serializing for key: SELECT+aaa+FROM+bbb+WHERE+0 for class: com.tianditu.entity.attrGrid.Records
2014 14:30:31 INFO  com.alisoft.xplatform.asf.cache.memcached.client.MemCachedClient - ++++ memcache cmd (result code): add SELECT+aaa+FROM+bbb+WHERE+0 8 0 451
 (STORED)
2014 14:30:31 INFO  com.alisoft.xplatform.asf.cache.memcached.client.MemCachedClient - ++++ data successfully stored for key: SELECT+aaa+FROM+bbb+WHERE+0
2014 14:30:31 INFO  com.alisoft.xplatform.asf.cache.memcached.client.MemCachedClient - ++++ serializing for key: SELECT+aaa+FROM+bbb+WHERE+1 for class: com.tianditu.entity.attrGrid.Records
2014 14:30:31 INFO  com.alisoft.xplatform.asf.cache.memcached.client.MemCachedClient - ++++ memcache cmd (result code): add SELECT+aaa+FROM+bbb+WHERE+1 8 0 451
 (STORED)
2014 14:30:31 INFO  com.alisoft.xplatform.asf.cache.memcached.client.MemCachedClient - ++++ data successfully stored for key: SELECT+aaa+FROM+bbb+WHERE+1
2014 14:30:31 INFO  com.alisoft.xplatform.asf.cache.memcached.client.MemCachedClient - ++++ serializing for key: SELECT+aaa+FROM+bbb+WHERE+2 for class: com.tianditu.entity.attrGrid.Records
2014 14:30:31 INFO  com.alisoft.xplatform.asf.cache.memcached.client.MemCachedClient - ++++ memcache cmd (result code): add SELECT+aaa+FROM+bbb+WHERE+2 8 0 451
 (STORED)
2014 14:30:31 INFO  com.alisoft.xplatform.asf.cache.memcached.client.MemCachedClient - ++++ data successfully stored for key: SELECT+aaa+FROM+bbb+WHERE+2
2014 14:30:31 INFO  com.alisoft.xplatform.asf.cache.memcached.client.MemCachedClient - ++++ serializing for key: SELECT+aaa+FROM+bbb+WHERE+3 for class: com.tianditu.entity.attrGrid.Records
2014 14:30:31 INFO  com.alisoft.xplatform.asf.cache.memcached.client.MemCachedClient - ++++ memcache cmd (result code): add SELECT+aaa+FROM+bbb+WHERE+3 8 0 451
 (STORED)
2014 14:30:31 INFO  com.alisoft.xplatform.asf.cache.memcached.client.MemCachedClient - ++++ data successfully stored for key: SELECT+aaa+FROM+bbb+WHERE+3
2014 14:30:31 INFO  com.alisoft.xplatform.asf.cache.memcached.client.MemCachedClient - ++++ serializing for key: SELECT+aaa+FROM+bbb+WHERE+4 for class: com.tianditu.entity.attrGrid.Records
2014 14:30:31 INFO  com.alisoft.xplatform.asf.cache.memcached.client.MemCachedClient - ++++ memcache cmd (result code): add SELECT+aaa+FROM+bbb+WHERE+4 8 0 451
 (STORED)
2014 14:30:31 INFO  com.alisoft.xplatform.asf.cache.memcached.client.MemCachedClient - ++++ data successfully stored for key: SELECT+aaa+FROM+bbb+WHERE+4
插入完成==>
2014 14:30:31 INFO  com.alisoft.xplatform.asf.cache.memcached.client.MemCachedClient - ++++ deserializing class com.tianditu.entity.attrGrid.Records
从缓存中取得的集合为:0
2014 14:30:31 INFO  com.alisoft.xplatform.asf.cache.memcached.client.MemCachedClient - ++++ deserializing class com.tianditu.entity.attrGrid.Records
从缓存中取得的集合为:1
2014 14:30:31 INFO  com.alisoft.xplatform.asf.cache.memcached.client.MemCachedClient - ++++ deserializing class com.tianditu.entity.attrGrid.Records
从缓存中取得的集合为:2
2014 14:30:31 INFO  com.alisoft.xplatform.asf.cache.memcached.client.MemCachedClient - ++++ deserializing class com.tianditu.entity.attrGrid.Records
从缓存中取得的集合为:3
2014 14:30:31 INFO  com.alisoft.xplatform.asf.cache.memcached.client.MemCachedClient - ++++ deserializing class com.tianditu.entity.attrGrid.Records
从缓存中取得的集合为:4
dao层 再去查
dao层 再去查
dao层 再去查
dao层 再去查


java + memcached安装使用,布布扣,bubuko.com

java + memcached安装使用

原文:http://blog.csdn.net/null____/article/details/25797717

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!