keyDic = new \stdClass(); $this->mem = new Memcache(); return $this->mem->connect($host, $port); } /** * * @param string $key * @return any */ public function get($key) { $cas_token = ""; if ($GLOBALS['mem_version'] == "3.0") { $result = memcache_get($this->mem, $key, null, $cas_token); } else { $result = memcache_get($this->mem, $key); } if (!$result || $result == "" || $result == null) { return null; } if ($GLOBALS['mem_version'] == "3.0") { $this->keyDic->$key = $cas_token; } return JsonUtil::decode($result); } public function set($key, $value, $ts = 0) { if ($value == null || $value == "") { return false; } return memcache_set($this->mem, $key, JsonUtil::encode($value), MEMCACHE_COMPRESSED, $ts); } /** * 取没有进行json编码的数据 * @param type $key * @return string */ public function getWithoutJson($key) { $cas_token = ""; if ($GLOBALS['mem_version'] == "3.0") { $result = memcache_get($this->mem, $key, null, $cas_token); } else { $result = memcache_get($this->mem, $key); } if (!$result || $result == "" || $result == null) { return null; } if ($GLOBALS['mem_version'] == "3.0") { $this->keyDic->$key = $cas_token; } return $result; } /** * 设置值,内部不加 json_encode * @param string $key * @param string $value * @param int $ts expirets Ps.当ts的值大于一个月的时候将被视为时间戳 * @return boolean */ public function setWithoutJson($key, $value, $ts = 0) { if ($value == null || $value == "") { return false; } return memcache_set($this->mem, $key, JsonUtil::encode($value), MEMCACHE_COMPRESSED, $ts); } public function add($key, $value, $ts = 0) { if ($value == null || $value == "") { return false; } return memcache_add($this->mem, $key, JsonUtil::encode($value), MEMCACHE_COMPRESSED, $ts); } public function replace($key, $value, $ts = 0) { if ($value == null || $value == "") { return false; } return memcache_replace($this->mem, $key, JsonUtil::encode($value), MEMCACHE_COMPRESSED, $ts); } public function delete($key) { return memcache_delete($this->mem, $key); } public function increment($key) { return memcache_increment($this->mem, $key); } public function getMulti($keys) { $retArray = array(); foreach ($keys as $key) { $result = $this->get($key); if ($result == null) { $retArray[] = null; } else { $retArray[] = $result; } } return $retArray; } public function setMutlti($dict, $expireTs = 0) { ; } public function cas($key, $value, $ts) { if ($GLOBALS['mem_version'] == "3.0") { if ($value == null || $value == "") { return false; } if (!property_exists($this->keyDic, $key)) { return memcache_set($this->mem, $key, JsonUtil::encode($value), MEMCACHE_COMPRESSED, $ts); } $cas_token = $this->keyDic->$key; return memcache_cas($this->mem, $key, JsonUtil::encode($value), MEMCACHE_COMPRESSED, $ts, $cas_token); } else { return $this->set($key, $value, $ts); } } public function copy($surKey, $desKey) { return $this->set($desKey, $this->get($surKey)); } public function close() { return memcache_close($this->mem); } }