java + memcached安装「建议收藏」

2022-07-06 20:48:37 浏览数 (1)

大家好,又见面了,我是全栈君。

一:安装

(临时获取上手windows实验) 1.下载memcached.exe , 上F:memcached 下 2.在CMD在输入 “F:memcachedmemcached.exe -d install” 安装. 3.再输入:”F:memcachedmemcached.exe -d start” 启动。以后memcached将作为windows的一个服务每次开机时自己主动启动。

这样server端已经安装完成了。

二: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

三:測试文件

代码语言:javascript复制
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);
    }
    
    /**
     * 依据指定的keyword获取对象
     */
    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层 去检查

发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/116654.html原文链接:https://javaforall.cn

0 人点赞