123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453 |
- <?php
- namespace KIF\Cache;
- use Memcached as MemcachedResource;
- use KIF\Debug\Debug;
- use KIF\Verify;
- use KIF\Exception\ParamsException;
- use KIF\Core\Config;
- class Memcached {
-
- static private $objMemcacheds = array();
-
- private $servers;
-
- private $objMemcached;
- public function __construct($cluster_flag = 'default') {
- $memcachedConfig = Config::getInstance()->get('memcached');
- if (!$memcachedConfig || !isset($memcachedConfig[$cluster_flag])) {
- throw new ParamsException("不存在的memcache集群标志 {$cluster_flag}");
- }
- $this->servers = $memcachedConfig[$cluster_flag];
- if (!isset(self::$objMemcacheds[$cluster_flag])) {
- $objMemcached = new MemcachedResource();
-
- $objMemcached->setOption(MemcachedResource::OPT_CONNECT_TIMEOUT, 1000);
-
- $objMemcached->setOption(MemcachedResource::OPT_TCP_NODELAY, true);
-
- $objMemcached->setOption(MemcachedResource::OPT_DISTRIBUTION, MemcachedResource::DISTRIBUTION_CONSISTENT);
- $objMemcached->setOption(MemcachedResource::OPT_LIBKETAMA_COMPATIBLE, true);
- $tmpResult = $objMemcached->addServers($this->servers);
- if (!$tmpResult) {
- return false;
- }
- self::$objMemcacheds[$cluster_flag] = $objMemcached;
- }
- $this->objMemcached = self::$objMemcacheds[$cluster_flag];
- }
-
- public function set($key, $value, $expiration = 0) {
- $begin_microtime = Debug::getTime();
- $tmpResult = $this->objMemcached->set($key, $value, $expiration);
- if ($this->getResultCode() == MemcachedResource::RES_SUCCESS) {
- Debug::cache($this->servers, $key, Debug::getTime() - $begin_microtime, $tmpResult, 'set');
- } else {
- Debug::cache($this->servers, $key, Debug::getTime() - $begin_microtime, 'false'.'::'.$this->getResultCode().'::'.$this->getResultMessage(), 'set');
- }
- return $tmpResult;
- }
-
- public function sets(array $items, $expiration = 0) {
- $begin_microtime = Debug::getTime();
- if (empty($items)) {
- throw new ParamsException('sets方法的参数items不能为空数组');
- }
- $tmpResult = $this->objMemcached->setMulti($items, $expiration);
- Debug::cache($this->servers, print_r($items, true), Debug::getTime() - $begin_microtime, $tmpResult, 'sets');
- return $tmpResult;
- }
-
- public function get($key, $cache_cb = null, & $cas_token = null) {
- $begin_microtime = Debug::getTime();
- if (func_num_args() == 3 && !is_null($cache_cb)) {
- $cb_success = null;
- $tmpResult = $this->objMemcached->get($key, function ($objMemcached, $cacheKey, & $ref_value) use ($cache_cb, & $cb_success) {
- $cb_success = $cache_cb($objMemcached, $cacheKey, $ref_value);
- return $cb_success;
- }, $cas_token);
- if ($cb_success) {
- $this->objMemcached->get($key, null, $cas_token);
- }
- } else {
- $tmpResult = $this->objMemcached->get($key, $cache_cb, $cas_token);
- }
- if ($this->getResultCode() == MemcachedResource::RES_SUCCESS) {
- Debug::cache($this->servers, $key, Debug::getTime() - $begin_microtime, $tmpResult, 'get');
- return $tmpResult;
- }
- if (MemcachedResource::RES_NOTFOUND == $this->getResultCode()) {
- Debug::cache($this->servers, $key, Debug::getTime() - $begin_microtime, null, 'get');
- return null;
- } else {
- Debug::cache($this->servers, $key, Debug::getTime() - $begin_microtime, 'false'.'::'.$this->getResultCode().'::'.$this->getResultMessage(), 'get');
- return false;
- }
- }
-
- public function gets(array $keys, array & $cas_tokens = null) {
- if (empty($keys)) {
- return array();
- }
- $begin_microtime = Debug::getTime();
- $tmpResult = $this->objMemcached->getMulti($keys, $cas_tokens, MemcachedResource::GET_PRESERVE_ORDER);
- Debug::cache($this->servers, print_r($keys, true), Debug::getTime() - $begin_microtime, $tmpResult, 'gets');
- return $tmpResult;
- }
-
- public function cas($cas_token, $key, $value, $expiration = 0) {
- $begin_microtime = Debug::getTime();
- $tmpResult = $this->objMemcached->cas($cas_token, $key, $value, $expiration);
- if ($tmpResult) {
- Debug::cache($this->servers, "{$cas_token}::{$key}::{$value}", Debug::getTime() - $begin_microtime, $tmpResult, 'cas');
- return true;
- }
- if ($this->getResultCode() == MemcachedResource::RES_DATA_EXISTS) {
- Debug::cache($this->servers, "{$cas_token}::{$key}::{$value}", Debug::getTime() - $begin_microtime, null, 'cas');
- return null;
- }
- Debug::cache($this->servers, "{$cas_token}::{$key}::{$value}", Debug::getTime() - $begin_microtime, false, 'cas');
- return false;
- }
-
- public function delete($key, $time = 0) {
- $begin_microtime = Debug::getTime();
- $tmpResult = $this->objMemcached->delete($key, $time);
- if ($tmpResult) {
- Debug::cache($this->servers, $key, Debug::getTime() - $begin_microtime, true, 'delete');
- return true;
- }
- if (MemcachedResource::RES_NOTFOUND == $this->getResultCode()) {
- Debug::cache($this->servers, $key, Debug::getTime() - $begin_microtime, null, 'delete');
- return null;
- }
- Debug::cache($this->servers, $key, Debug::getTime() - $begin_microtime, false, 'delete');
- return false;
- }
-
-
- public function deletes(array $keys, $time = 0) {
- $begin_microtime = Debug::getTime();
- $tmpResult = $this->objMemcached->deleteMulti($keys, $time);
- if ($tmpResult) {
- Debug::cache($this->servers, print_r($keys, true), Debug::getTime() - $begin_microtime, true, 'deletes');
- return true;
- }
- if (MemcachedResource::RES_NOTFOUND == $this->getResultCode()) {
- Debug::cache($this->servers, print_r($keys, true), Debug::getTime() - $begin_microtime, null, 'deletes');
- return null;
- }
- Debug::cache($this->servers, print_r($keys, true), Debug::getTime() - $begin_microtime, false, 'deletes');
- return false;
- }
-
- public function add($key, $value, $expiration = 0) {
- $begin_microtime = Debug::getTime();
- $tmpResult = $this->objMemcached->add($key, $value, $expiration);
- if ($tmpResult) {
- Debug::cache($this->servers, "{$key}::{$value}", Debug::getTime() - $begin_microtime, true, 'add');
- return true;
- }
- if (MemcachedResource::RES_NOTSTORED == $this->getResultCode()) {
- Debug::cache($this->servers, "{$key}::{$value}", Debug::getTime() - $begin_microtime, null, 'add');
- return null;
- }
- Debug::cache($this->servers, "{$key}::{$value}", Debug::getTime() - $begin_microtime, false, 'add');
- return false;
- }
-
- public function replace($key, $value, $expiration = 0) {
- $begin_microtime = Debug::getTime();
- $tmpResult = $this->objMemcached->replace($key, $value, $expiration);
- if ($tmpResult) {
- Debug::cache($this->servers, "{$key}::{$value}", Debug::getTime() - $begin_microtime, true, 'replace');
- return true;
- }
- if (MemcachedResource::RES_NOTSTORED == $this->getResultCode()) {
- Debug::cache($this->servers, "{$key}::{$value}", Debug::getTime() - $begin_microtime, null, 'replace');
- return null;
- }
- Debug::cache($this->servers, "{$key}::{$value}", Debug::getTime() - $begin_microtime, false, 'replace');
- return false;
- }
-
- public function increment($key, $offset = 1) {
- $begin_microtime = Debug::getTime();
- $tmpGetResult = $this->get($key);
- if (is_null($tmpGetResult)) {
- Debug::cache($this->servers, "{$key}::{$offset}", Debug::getTime() - $begin_microtime, null, 'increment');
- return null;
- }
- if ($this->getResultCode() != MemcachedResource::RES_SUCCESS ) {
- Debug::cache($this->servers, "{$key}::{$offset}", Debug::getTime() - $begin_microtime, false, 'increment');
- return false;
- }
- if (!Verify::naturalNumber($tmpGetResult)) {
- Debug::cache($this->servers, "{$key}::{$offset}", Debug::getTime() - $begin_microtime, false, 'increment');
- return false;
- }
- $tmpResult = $this->objMemcached->increment($key, $offset);
- if (Verify::unsignedInt($tmpResult)) {
- Debug::cache($this->servers, "{$key}::{$offset}", Debug::getTime() - $begin_microtime, $tmpResult, 'increment');
- return $tmpResult;
- }
- Debug::cache($this->servers, "{$key}::{$offset}", Debug::getTime() - $begin_microtime, false, 'increment');
- return false;
- }
-
- public function decrement($key, $offset = 1) {
- $begin_microtime = Debug::getTime();
- $tmpGetResult = $this->get($key);
- if (is_null($tmpGetResult)) {
- Debug::cache($this->servers, "{$key}::{$offset}", Debug::getTime() - $begin_microtime, null, 'decrement');
- return null;
- }
- if ($this->getResultCode() != MemcachedResource::RES_SUCCESS ) {
- Debug::cache($this->servers, "{$key}::{$offset}", Debug::getTime() - $begin_microtime, false, 'decrement');
- return false;
- }
- if (!Verify::naturalNumber($tmpGetResult)) {
- Debug::cache($this->servers, "{$key}::{$offset}", Debug::getTime() - $begin_microtime, false, 'decrement');
- return false;
- }
- $tmpResult = $this->objMemcached->decrement($key, $offset);
- if (Verify::naturalNumber($tmpResult)) {
- Debug::cache($this->servers, "{$key}::{$offset}", Debug::getTime() - $begin_microtime, $tmpResult, 'decrement');
- return $tmpResult;
- }
- Debug::cache($this->servers, "{$key}::{$offset}", Debug::getTime() - $begin_microtime, false, 'decrement');
- return false;
- }
-
- public function getResultCode() {
- return $this->objMemcached->getResultCode();
- }
-
- public function getResultMessage() {
- return $this->objMemcached->getResultMessage();
- }
- }
|