MemcachedService

package com.yulong.memcached.service;

import java.io.InputStream;
import java.net.URL;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Properties;

import org.apache.log4j.Logger;

import com.danga.MemCached.MemCachedClient;
import com.danga.MemCached.SockIOPool;

public class MemcachedService
{
   
    static Logger logger = Logger.getLogger(MemcachedService.class);
    static
    {
        init();
    }
    private static MemCachedClient mcc = new MemCachedClient();
   
    public static void init()
    {
        String[] servers = {};
       
        long maxBusyTime = 1000 * 60 * 2;
        int initConn = 10;
        int minConn = 5;
        int maxConn = 250;
        long maintSleep = 30;
        int hashingAlg = 3;
       
        try
        {
            URL url = MemcachedService.class.getResource("/memcached/servers.properties");
            InputStream is = null;
            if (url != null)
            {
                is = url.openStream();
            }
            if (is == null)
                is = MemcachedService.class.getResourceAsStream("/memcached/servers.properties");
            Properties prop = new Properties();
            prop.load(is);
           
            servers = prop.getProperty("servers").split(",");
           
            maxBusyTime = Long.valueOf(prop.getProperty("maxBusyTime") == null ? "" : prop.getProperty("maxBusyTime"));
            initConn = Integer.parseInt(prop.getProperty("initConn") == null ? "" : prop.getProperty("initConn"));
            minConn = Integer.parseInt(prop.getProperty("minConn") == null ? "" : prop.getProperty("minConn"));
            maxConn = Integer.parseInt(prop.getProperty("maxConn") == null ? "" : prop.getProperty("maxConn"));
            maintSleep = Long.parseLong(prop.getProperty("maintSleep") == null ? "" : prop.getProperty("maintSleep"));
            hashingAlg = Integer.parseInt(prop.getProperty("hashingAlg") == null ? "" : prop.getProperty("hashingAlg"));
           
            logger.info("memcached servers:" + prop.getProperty("servers"));
           
            SockIOPool pool = SockIOPool.getInstance();
            pool.setServers(servers);
            pool.setMaxBusyTime(maxBusyTime);
            pool.setInitConn(initConn);
            pool.setMinConn(minConn);
            pool.setMaxConn(maxConn);
            pool.setMaintSleep(maintSleep);
            pool.setHashingAlg(hashingAlg);
           
            pool.setFailover(true);
            pool.setNagle(false);
            pool.setSocketTO(3000);
            pool.setAliveCheck(false);
           
            pool.initialize();
        }
        catch (Exception e)
        {
            logger.error(e.getMessage());
            for (StackTraceElement element : e.getStackTrace())
            {
                logger.error(element);
            }
        }
       
    }
    /**
     * 系统专用
     * @param key
     * @param value
     * @return
     */
    public static Object setParam(Object key, Object value)
    {
        return set("P" + (String) key, value);
    }
   
    /**
     * 获得memcached服务器的状态
     */
//    public static String getServerStatus()
//    {
//        mch = new MccErrorHandler();
//        mch.handleErrorOnStats(mcc, null);
////        mcc.setErrorHandler(mch);
//        mcc.set("test", "test");
//        mcc.replace("test", "test");
//        if (mch.getStatus().equals("ok"))
//        {
//            return "ok";
//        }
//        else
//        {
//            return "outOfService";
//        }
//    }
   
    /**
     * 系统专用
     * @param key
     * @param value
     * @return
     */
    public static Object setParam(String key, Object value)
    {
       
        return set("P" + key, value);
    }
   
    public static Object set(Object key, Object value)
    {
       
        return set((String) key, value);
    }
   
    public static Object set(String key, Object value)
    {
//        mch = new MccErrorHandler();
//        mcc.setErrorHandler(mch);
        mcc.set(key, value);
        mcc.replace(key, value);
//        if (mch.getStatus().equals("ok"))
//        {
            return value;
//        }
//        else
//        {
//            return "error";
//        }
    }
   
    public static void putAll(Map<? extends String, ? extends Object> m)
    {
        for (String key : m.keySet())
        {
            set(key, m.get(key));
        }
    }
   
    public static Object removeGroup(Object key)
    {
        return remove("G" + (String) key);
    }
   
    public static Object removeParam(Object key)
    {
        return null;// 系统不可移除
    }
   
    public static Object remove(Object key)
    {
        return remove((String) key);
    }
   
    public static Object remove(String key)
    {
//        mch = new MccErrorHandler();
//        mcc.setErrorHandler(mch);
        Object value = mcc.get(key);
        mcc.delete(key);
//        if (mch.getStatus().equals("ok"))
//        {
            return value;
//        }
//        else
//        {
//            return "error";
//        }
    }
   
    public static boolean replace(String key, Object value)
    {
        boolean result = false;
//        mch = new MccErrorHandler();
//        mcc.setErrorHandler(mch);
        result = mcc.replace(key, value);
//        if (mch.getStatus().equals("ok"))
//        {
            return result;
//        }
//        else
//        {
//            return false;
//        }
       
    }
   
    public static Object get(Object key)
    {
        return get((String) key);
    }
   
    public static Object get(String key)
    {
//        mch = new MccErrorHandler();
//        mcc.setErrorHandler(mch);
        Object t = mcc.get(key);
//        if (mch.getStatus().equals("ok"))
//        {
            return t;
//        }
//        else
//        {
//            return "error";
//        }
    }
   
    /**
     * 系统专用
     * @param key
     * @return
     */
    public static Object getParam(Object key)
    {
        Object t = get("P" + (String) key);
        if (t != null && t.toString().equals("error"))
        {
            // TODO 获得对应的值
            return "error";
        }
        else
        {
            return t;
        }
    }
   
    /**
     * 系统专用
     * @param key
     * @return
     */
    public static Object getParam(String key)
    {
        return get("P" + key);
    }
   
    /**
     * 系统专用
     * @param groupName
     * @return
     */
    public static LinkedHashMap<String, Object> getGroup(String groupName)
    {
        Object group = get("G" + groupName);
        if (group != null && group.toString().equals("error"))
        {
            // 数据库存取
            return null;
        }
        else if (group == null)
        {
            return null;
        }
        else
        {
            return (LinkedHashMap<String, Object>) group;
        }
    }
   
    /**
     * 系统专用
     * @param groupName
     * @param group
     * @return
     */
    public static LinkedHashMap<String, Object> setGroup(String groupName, LinkedHashMap<String, Object> group)
    {
        String t = set("G" + groupName, group).toString();
        if (t.equals("error"))
        {
            return null;
        }
        replace("G" + groupName, group);
        return group;
    }
   
    public static boolean set(String key, Object value, Date expiry)
    {
        boolean result = false;
//        mch = new MccErrorHandler();
//        mcc.setErrorHandler(mch);
        result = mcc.set(key, value, expiry);
        result = mcc.replace(key, value, expiry);
        return result;
    }
   
    /**
     *
    * @Title: getAll
    * @Description: 批量获取数据
    * @param  key
    * @return boolean
    * @throws
     */
    public static Map<String,Object> getAll(String[] key){
        return mcc.getMulti(key);

    }
   
    /**
     *
    * @Title: getAllParam
    * @Description: TODO批量获取系统参数数据
    * @param keys
    * @return Map<String,Object>
    * @throws
     */
    public static Map<String,Object> getAllParam(String[] keys){
    for (int i=0;i<keys.length;i++){
    keys[i]="P"+keys[i];
        }
        return mcc.getMulti(keys);

    }
   
    /**
     *
    * @Title: getAllGroup
    * @Description: TODO批量获取系统代码数据
    * @param keys
    * @return Map<String,Object>
    * @throws
     */
    public static Map<String,Object> getAllGroup(String[] keys){
    for (int i=0;i<keys.length;i++){
    keys[i]="G"+keys[i];
        }
        return mcc.getMulti(keys);

    }
   
    public static boolean keyExists(String key)
    {
        boolean result = false;
//        mch = new MccErrorHandler();
//        mcc.setErrorHandler(mch);
        result = mcc.keyExists(key);
        return result;
    }
}

你可能感兴趣的:(memcached)