ftsr 发表于 2018-12-25 08:46:41

ibatis加memcached缓存

package com.ibatis.sqlmap.engine.cache.memcached;  import java.io.Serializable;

  public>  private static Logger logger = Logger.getLogger(MemcachedManager.class);
  private static String memcachedDomain = "IBATIS_CACHED"; // memcached 域名
  private String serverlist;
  private static MemCachedClient mcc = null;
  private SockIOPool pool = null;
  private int initConn = 5;
  private int minConn = 5;
  private int maxConn = 50;
  public void init() {
  if (mcc != null)
  return;
  logger.info("Initializing ibatis memcached start.");
  if (pool == null) {
  try {
  pool = SockIOPool.getInstance(memcachedDomain);
  pool.setServers(serverlist.split(","));
  if (!pool.isInitialized()) {
  pool.setInitConn(initConn);
  pool.setMinConn(minConn);
  pool.setMaxConn(maxConn);
  pool.setMaintSleep(30);
  pool.setNagle(false);
  pool.setSocketTO(60 * 60);
  pool.setSocketConnectTO(0);
  pool.initialize();
  }
  } catch (Exception ex) {
  logger.error(ex.getMessage());
  }
  }
  if (mcc == null) {
  mcc = new MemCachedClient(memcachedDomain);
  mcc.setCompressEnable(false);
  mcc.setCompressThreshold(0);
  }
  logger.info("Initializing youxigu Memcached ok!");
  }
  public void closePool() {
  pool.shutDown();
  mcc = null;
  pool = null;
  logger.info("Ibatis memcached pool closed");
  }
  public static boolean set(Object key, Serializable obj) {
  logger.debug("set key:" + getKey(key) + ", value:" + obj);
  try {
  return mcc.set(getKey(key), obj);
  } catch (Exception e) {
  logger.error("Pool set error!");
  e.printStackTrace();
  }
  return false;
  }
  public static boolean set(Object key, Serializable obj, long cacheTime) {
  try {
  return mcc.set(getKey(key), obj, new Date(cacheTime));
  } catch (Exception e) {
  logger.error("Pool set error!");
  e.printStackTrace();
  }
  return false;
  }
  public static void replace(int key, Serializable value, long cacheTime) {
  try {
  mcc.replace(getKey(key), value, new Date(cacheTime));
  } catch (Exception e) {
  logger.error(" pool set error!");
  }
  }
  public static Object get(Object key) {
  Object result = null;
  String realkey = getKey(key);
  try {
  result = mcc.get(realkey);
  } catch (Exception e) {
  e.printStackTrace();
  }
  logger.debug("get key:" + getKey(key) + ", value:" + result);
  return result;
  }
  public static void setCounter(Object key, long count) {
  try {
  mcc.storeCounter(getCountKey(key), count);
  } catch (Exception e) {
  logger.error("Pool setCounter error!");
  }
  }
  public static void addCounter(Object key) {
  if (mcc.get(getCountKey(key)) == null) {
  mcc.storeCounter(getCountKey(key), 0);
  }
  try {
  mcc.incr(getCountKey(key));
  } catch (Exception e) {
  logger.error("Pool setCounter error!");
  }
  }
  public static void decreaseCounter(Object key) {
  try {
  mcc.decr(getCountKey(key));
  } catch (Exception e) {
  logger.error("Pool setCounter error!");
  }
  }
  public static void addCounter(Object key, long addValue) {
  try {
  mcc.incr(getCountKey(key), addValue);
  } catch (Exception e) {
  logger.error(" pool setCounter error!");
  }
  }
  public static long getCounter(Object key) {
  long result = 0;
  try {
  result = mcc.getCounter(getCountKey(key));
  } catch (Exception e) {
  logger.error(e.getMessage());
  }
  return result;
  }
  public static boolean delete(Object key) {
  try {
  return mcc.delete(getKey(key));
  } catch (Exception e) {
  logger.error(e.getMessage());
  }
  return false;
  }
  public static long deleteCounter(Object key) {
  try {
  return mcc.decr(getCountKey(key));
  } catch (Exception e) {
  logger.error(" pool setCounter error!");
  }
  return 0;
  }
  public static void flushAll() {
  mcc.flushAll();
  }
  @SuppressWarnings("unchecked")

  public static long>
  long>  Map status = mcc.statsItems();
  Collection values = status.values();
  for (Map state : values) {
  String num = state.get("items:1:number");
  if (num == null)
  continue;

  >  }

  return>  }
  private static String getKey(Object key) {
  return memcachedDomain + "@" + key;
  }
  private static String getCountKey(Object key) {
  return memcachedDomain + "@" + key + "_count";
  }
  public void setServerlist(String serverlist) {
  this.serverlist = serverlist;
  }
  public void setInitConn(int initConn) {
  this.initConn = initConn;
  }
  public void setMinConn(int minConn) {
  this.minConn = minConn;
  }
  public void setMaxConn(int maxConn) {
  this.maxConn = maxConn;
  }
  public void setMemcachedDomain(String memcachedKey) {
  MemcachedManager.memcachedDomain = memcachedKey;
  }
  }

页: [1]
查看完整版本: ibatis加memcached缓存