/**
   * MemCachedClient get method enhance.
   *
   * @param key
   * @return
   */
  public Object get(String key) {
    logger.debug("get from memcache. key=" + key);

    // Get block count from cache.
    int blockCount = 0;
    try {
      String blockCountKey = key + BLOCK_COUNT;
      blockCount = (Integer) memcachedClient.get(blockCountKey, blockCountKey.hashCode());
    } catch (Exception e) {
      logger.warn("get blockCount from memcache. blockCount=" + blockCount);
    }

    logger.debug("get blockCount from memcache. blockCount=" + blockCount);

    // No blocks, get cache by origin key directly.
    if (blockCount <= 0) {
      logger.debug("get from memcache by origin key directly. key=" + key);
      return memcachedClient.get(key, key.hashCode());
    }

    // Has blocks, Rebuild Object from cached-block-data.
    logger.debug("get from memcache, Rebuild Object from cached-block-data. key=" + key);

    ByteArrayOutputStream bos = null;
    ObjectInputStream ois = null;
    try {
      // Write all block data into memory
      bos = new ByteArrayOutputStream();
      for (int i = 1; i <= blockCount; i++) {
        String blockKey = key + BLOCK + i;
        Object obj = memcachedClient.get(blockKey, blockKey.hashCode());

        if (obj != null && (obj instanceof byte[])) {
          bos.write((byte[]) obj);
        }
      }
      bos.flush();

      // Read object from mem
      ois = new ObjectInputStream(new ByteArrayInputStream(bos.toByteArray()));
      Object obj = ois.readObject();
      logger.debug(
          "readObject from memory, obj info=" + (obj == null ? "null" : obj.getClass().getName()));
      return obj;
    } catch (Exception e) {
      logger.error("get from memcache. key=" + key + " exception.", e);
      return null;
    } finally {
      try {
        if (bos != null) bos.close();
      } catch (Exception e1) {
        /* nothing can do. */
      }
      try {
        if (ois != null) ois.close();
      } catch (Exception e2) {
        /* nothing can do. */
      }
    }
  }
예제 #2
0
 @Override
 public byte[] get(Node.Item id) {
   if (l.isDebugEnabled()) {
     l.debug(_name + ":" + _config + " get " + id);
   }
   return (byte[]) client.get(getKey(id));
 }
예제 #3
0
 public void add(Node.Item id, byte[] data) {
   /*
    * if (l.isDebugEnabled()) { l.debug(name_ + ":" + config_ + " add " +
    * getKey(id) + " data size: " + data.length); }
    */
   if (data == null) return;
   client.set(getKey(id), data);
 }
예제 #4
0
 @Override
 public boolean internalSet(Node.Item id, byte[] data) {
   if (l.isDebugEnabled()) {
     l.debug(_name + ":" + _config + " set " + getKey(id) + " data size: " + data.length);
   }
   if (data == null) return false;
   // client.set(getKey(id), data, new Date(new Date().getTime()+EXPIRE));
   return client.set(getKey(id), data);
 }
예제 #5
0
 @Override
 public Map<SocketAddress, Map<String, String>> getStats() {
   Map<String, Map<String, String>> map = client.stats();
   Map<SocketAddress, Map<String, String>> result =
       new HashMap<SocketAddress, Map<String, String>>();
   for (Entry<String, Map<String, String>> entry : map.entrySet()) {
     String[] address = entry.getKey().split(":");
     result.put(new InetSocketAddress(address[0], Integer.parseInt(address[1])), entry.getValue());
   }
   return result;
 }
예제 #6
0
 //
 // ------------------------------------------------------------------------------------------------
 // 描述:
 // 设计: Skyline(2001.12.29)
 // 实现: Skyline
 // 修改:
 // ------------------------------------------------------------------------------------------------
 public static synchronized int getTmpTableCouont() {
   int iRes = 1;
   String key = "CalculateTmpTableCount";
   Object tmpObj;
   MemCachedClient memCC = MemCachedManager.getDefault().getMemCached();
   if (memCC != null) {
     tmpObj = memCC.get(key);
     if (tmpObj == null) {
       memCC.add(key, new Integer(100));
       iRes = 100;
     } else {
       iRes = (Integer) tmpObj;
       memCC.incr(key);
       return iRes;
     }
   } else {
     return TmpTableCount++;
   }
   return iRes;
 }
예제 #7
0
 @Override
 public Map<Item, byte[]> get(List<Item> id) {
   List<String> keys = new ArrayList<String>(id.size());
   for (Item i : id) {
     keys.add(getKey(i));
   }
   Map<Item, byte[]> result = new HashMap<Item, byte[]>();
   Map<String, Object> ret = client.getMulti(keys.toArray(new String[keys.size()]));
   for (Entry<String, Object> entry : ret.entrySet()) {
     result.put(getKey(entry.getKey()), (byte[]) (entry.getValue()));
   }
   return result;
 }
예제 #8
0
 public boolean casSet(Node.Item id, byte[] data) {
   if (l.isDebugEnabled()) {
     l.debug(
         _name
             + ":"
             + _config
             + " set "
             + getKey(id)
             + " data size: "
             + data.length
             + " cas "
             + id.cas);
   }
   if (data == null) return false;
   return client.cas(getKey(id), data, id.cas);
 }
예제 #9
0
 @RequestMapping("/flush/mc")
 @AopLogModule(name = C.LOG_MODULE_PCRM, layer = C.LOG_LAYER_CONTROLLER)
 public @ResponseBody boolean flushMc() {
   System.out.println("flush mc");
   return mc.flushAll();
 }
  /**
   * MemCachedClient set method enhance.
   *
   * @param key
   * @param value
   * @param expiry
   * @return
   */
  public boolean set(String key, Object value, Date expiry) {
    logger.debug(
        "set memcached. key=" + key + ", value=" + value.getClass() + ", expiry=" + expiry);

    if (value == null) return false;

    ByteArrayOutputStream bos = null;
    ObjectOutputStream oos = null;
    try {
      // Read value into mem
      bos = new ByteArrayOutputStream();
      oos = new ObjectOutputStream(bos);
      oos.writeObject(value);
      oos.flush();
      bos.flush();

      // Some declarations.
      byte[] data = bos.toByteArray();
      int dataLen = data.length;
      int blockCount = 0, mod = 0;

      // When dataLen more than specified block size,
      // will split big-data into small-blocks, and cached them.
      // If dataLen less then specified block size, will cached the value
      // directly.
      logger.debug(
          "set memcached. key="
              + key
              + ", data length="
              + dataLen
              + ", DefBlockSize="
              + BLOCK_SIZE);

      if (dataLen > BLOCK_SIZE) {
        logger.debug("Cache data by blocks. key=" + key);

        blockCount = dataLen / BLOCK_SIZE;
        mod = dataLen % BLOCK_SIZE;
        if (mod != 0) {
          blockCount += 1;
        }

        for (int i = 1; i <= blockCount; i++) {
          int begin = (i - 1) * BLOCK_SIZE;
          int end = i * BLOCK_SIZE;
          if (end >= dataLen) end = dataLen;

          // Cache block data
          String blockKey = key + BLOCK + i;
          memcachedClient.set(
              blockKey, getBlockData(data, begin, end), expiry, blockKey.hashCode());
        }

        // Cache Block-count
        String blockCountKey = key + BLOCK_COUNT;
        return memcachedClient.set(blockCountKey, blockCount, expiry, blockCountKey.hashCode());
      } else {
        // Cache origin value directly
        logger.debug("Cache origin value directly. key=" + key);
        return memcachedClient.set(key, value, expiry, key.hashCode());
      }
    } catch (Exception e) {
      logger.error("set memcached exception.", e);
      return false;
    } finally {
      try {
        if (bos != null) bos.close();
      } catch (Exception e1) {
        /* nothing can do. */
      }
      try {
        if (oos != null) oos.close();
      } catch (Exception e2) {
        /* nothing can do. */
      }
    }
  }