无线&移动互联网技术研发

换位思考·····
posts - 19, comments - 53, trackbacks - 0, articles - 283
  BlogJava :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理

java MemCache client application Model

Posted on 2009-11-16 17:12 Gavin.lee 阅读(661) 评论(0)  编辑  收藏 所属分类: MemCache
这段时间解决系统的瓶颈,首要是对memcache的优化,今天得空将目前在用的memcache整理了一下,贴个客户端实体类-----这里设置的参数都是通过配置文件的。道理很简单,以戒后用
(注:http://code.google.com/p/memcache-client-forjava/downloads/list 本次使用的是来自这里的封装包,网上结论说alisoft memcache 性能不高,经高压下使用,个人感觉确实不妥。
可考虑使用 http://www.whalin.com/memcached/#download
package yixun.wap.cache;

/**
 * 
@deprecated mem-cache缓存类 备用类
 * 
@author Administrator
 * @date 2009-11-15 14:08:02 更新
 
*/

import java.util.Date;
import com.alisoft.xplatform.asf.cache.memcached.client.MemCachedClient;
import com.alisoft.xplatform.asf.cache.memcached.client.SockIOPool;
import yixun.wap.resourcesload.MemcacheConfig;

public class MemCache {
    
private static MemCachedClient mcc = null;
    
static {
        mcc 
= new MemCachedClient();
        Boolean nagle 
= false;
        Boolean ce 
= true;
        
//         获取socke连接池的实例对象
        SockIOPool pool = SockIOPool.getInstance();
        
//         设置服务器信息
        pool.setServers(MemcacheConfig.getServers().split(";"));
        String[] weights 
= MemcacheConfig.getWeights().split(";");
        Integer[] wei 
= new Integer[weights.length];
        
for (int i = 0; i < weights.length; i++{
            wei[i] 
= Integer.parseInt(weights[i]);
        }

        pool.setWeights(wei);
        
        
//         设置初始连接数、最小和最大连接数以及最大处理时间
        pool.setInitConn(Integer.parseInt(MemcacheConfig.getInitconn()));
        pool.setMinConn(Integer.parseInt(MemcacheConfig.getMinconn()));
        pool.setMaxConn(Integer.parseInt(MemcacheConfig.getMaxconn()));
        pool.setMaxIdle(Long.parseLong(MemcacheConfig.getMaxidle()));
        
//         设置主线程的睡眠时间
        pool.setMaintSleep(Long.parseLong(MemcacheConfig.getMaintsleep()));
        
if (!MemcacheConfig.getNagle().equals("false")) {
            nagle 
= true;
        }

        
//         设置TCP的参数,连接超时等
        pool.setNagle(nagle);
        pool.setSocketTO(Integer.parseInt(MemcacheConfig.getSocketTO()));
        pool.setSocketConnectTO(Integer.parseInt(MemcacheConfig.getSocketConnectTO()));
        
//         初始化连接池
        pool.initialize();
        
//         压缩设置,超过指定大小(单位为K)的数据都会被压缩
        if (MemcacheConfig.getCompressEnable().equals("false")) {
            ce 
= false;
        }
        
        mcc.setCompressEnable(ce);
        mcc.setCompressThreshold(Long.parseLong(MemcacheConfig.getCompressThreshold()));
        
    }

    
private MemCache(){
        
    }

    
private static MemCache instance;
    
public synchronized  static MemCache getInstance() {
        
if(instance == null{
            
return new MemCache();
        }

        
return instance;
    }

    
    
//    在memcache中对数据进行存、取、删、更新
    public boolean set(String key, Object value) {
        
return mcc.set(key, value);
    }


    
public boolean set(String key, Object value, String time) {
        
return mcc.set(key, value, new Date(Long.parseLong(time)));
    }


    
public Object get(String key) 
        
return mcc.get(key);
    }


    
public boolean delete(String key) {
        
return mcc.delete(key);
    }

    
    
public boolean update(String key, Object obj) {
        
return mcc.replace(key, obj);
    }

    
    
public boolean update(String key, Object value, Date expiry) {
        
return mcc.replace(key, value, expiry);
    }

    
    
public void flush_all() {
        mcc.flushAll();
    }



    
public MemCachedClient getMcc() {
        
return mcc;
    }



    
public void setMcc(MemCachedClient mcc) {
        
this.mcc = mcc;
    }


}


只有注册用户登录后才能发表评论。


网站导航: