`

MembaseManager

 
阅读更多
/**
* Created by Administrator on 13-11-7.
*/
public class MembaseManager {
    private static Logger logger = LoggerFactory.getLogger ("MembaseManager");
    private static MemcachedClient membase = null;
    public static  final  int membaseRwTimeout = 200;

    public final static boolean membaseSynchronized = false;
    private MembaseManager() {
        // 单例
    }

    public static boolean isInitialized() {
        return membase != null;
    }

    /**
     *
     * @param key
     * @param exp �?
     * @param val
     * @return
     */
    public static void set(String key, int exp, Object val) {
        try {
            if (membaseSynchronized) {
                synchronized (MembaseManager.class) {
                    membase.set(key, exp, val);
                }
            } else {
                membase.set(key, exp, val);
            }
        } catch (Exception e) {
            e.printStackTrace();
           logger.error( "set membase err");
        }
    }

    public static void incr(String key, int by, long def, int exp) {
        try {
            if (membaseSynchronized) {
                synchronized (MembaseManager.class) {
                    membase.incr(key, by, def, exp);
                }
            } else {
                membase.incr(key, by, def, exp);
            }
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("incr membase err");
        }
    }

    public static void decr(String key, int by, long def, int exp) {
        try {
            if (membaseSynchronized) {
                synchronized (MembaseManager.class) {
                    membase.decr(key, by, def, exp);
                }
            } else {
                membase.decr(key, by, def, exp);
            }
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("decr");
        }
    }

    public static void del(String key) {
        try {
            if (membaseSynchronized) {
                synchronized (MembaseManager.class) {
                    membase.delete(key);
                }
            } else {
                membase.delete(key);
            }
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("delete");
        }
    }

    public static Object get(String key) {
        Object val = null;
        Future<Object> f = null;
        try {
            if (membaseSynchronized) {
                synchronized (MembaseManager.class) {
                    f = membase.asyncGet(key);
                    val = f.get(membaseRwTimeout, TimeUnit.SECONDS);
                }
            } else {
                f = membase.asyncGet(key);
                val = f.get(membaseRwTimeout, TimeUnit.SECONDS);
            }
            return (null == val) ? null : val;
        } catch (TimeoutException eto) {
            eto.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (f != null) {
                f.cancel(false);
            }
            f = null;
        }
        return null;
    }

    public static String get(String key, String def) {
        Object val = get(key);
        return (null == val) ? def : val.toString();
    }

    public static void main(String[] args) {
        if (isInitialized()) {
            MembaseManager.set("key1", 3600, new Date());
            System.out.println("key1=" + MembaseManager.get("key1"));
        }
    }

    static {
        MemcachedClient client = null;
        try {
            client = new MemcachedClient(//ip,//port);
        } catch (Exception e) {
            client = null;
            e.printStackTrace();
            logger.error( "membaseAddresses="
                    + null);
        }
        membase = client;
        logger.info("缓存服务接口初始化:addr=" + ""
                + "; membase=" + membase);
    }
}
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics