123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170 |
- <?php
- namespace loyalsoft;
- /*
- * 操作内存数据库 Memcache工具类
- * version:
- * 2016.4.12 (gwang)改造出一个基类
- * 2016.4.12 以前的记录无
- */
- /**
- * Description of MemCacheUtil
- * CMem工具类[APHP三大操作单元之一]
- * @deprecated since version 2017.08.08
- * @author jgao,gwang
- */
- class CMemUtil extends CMemBase
- {
- //put your code here
- public $mem = 0;
- public $keyDic = null;
- public function conn($host, $port, $pwd = "")
- {
- $this->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);
- }
- }
|