/** * 给memcache存数据 * * @param string/array $key 支持多存 * @param $data Value 多存时此项可空 * @param $lifetime 有效期,默认3600,即1小时,0表示最大值30天(2592000) * @return boolean */ public function set($key, $value = null, $lifetime = 3600) { $this->_connect(); \Core::debug()->info($key, 'memcache set key'); if (static::$_memcached_mode) { // memcached if (\is_array($key)) { return $this->_memcache->setMulti($key, $lifetime); } return $this->_memcache->set($key, $value, $lifetime); } else { // memcache if (\is_array($key)) { $return = true; foreach ($key as $k => $v) { $s = $this->set($k, $v, $lifetime); if (false === $s) { $return = false; } } return $return; } return $this->_memcache->set($key, $value, $this->_get_flag($value), $lifetime); } }
/** * Write many cache entries to the cache at once * * @param array $data An array of data to be stored in the cache * @return array of bools for each key provided, true if the data was successfully cached, false on failure */ public function writeMany($data) { $cacheData = array(); foreach ($data as $key => $value) { $cacheData[$this->_key($key)] = $value; } $success = $this->_Memcached->setMulti($cacheData); $return = array(); foreach (array_keys($data) as $key) { $return[$key] = $success; } return $return; }
/** * 给memcache存数据 * * @param string/array $key 支持多存 * @param $data Value 多存时此项可空 * @param $lifetime 有效期,默认3600,即1小时,0表示最大值30天(2592000) * @return boolean */ public function set($key, $value = null, $lifetime = 3600) { $this->_connect(); $is_array_key = is_array($key); # 加前缀 if ($this->prefix) { if ($is_array_key) { $new_data = array(); foreach ($key as $k => $v) { $new_data[$this->prefix . $k] = $v; } $key = $new_data; unset($new_data); } else { $key = $this->prefix . $key; } } if (Cache_Driver_Memcache::$_memcached_mode) { // memcached if ($is_array_key) { $rs = $this->_memcache->setMulti($key, $lifetime); } else { $rs = $this->_memcache->set($key, $value, $lifetime); } } else { // memcache if ($is_array_key) { $rs = true; foreach ($key as $k => $v) { $s = $this->_memcache->set($k, $v, $this->_get_flag($v), $lifetime); if (false === $s) { $rs = false; } } } else { $rs = $this->_memcache->set($key, $value, $this->_get_flag($value), $lifetime); } } if (IS_DEBUG) { if (is_array($key)) { Core::debug()->info(array_keys($key), 'memcache set key'); } else { Core::debug()->info($key, 'memcache set key'); } } return $rs; }
/** * Write data to cache * * @param array $keys array of key/value data to store * @param int $expire expiration time * @return boolean true on success */ protected function write(array $keys, $expire = null) { if ($this->memcacheType == GitPHP_CacheResource_Memcache::Memcache) { foreach ($keys as $key => $value) { $this->memcacheObj->set(sha1($key), $value, 0, $expire); } return true; } else { if ($this->memcacheType == GitPHP_CacheResource_Memcache::Memcached) { $mapped = array(); foreach ($keys as $key => $value) { $mapped[sha1($key)] = $value; } $this->memcacheObj->setMulti($mapped, $expire); return true; } } return false; }