class memcachedInit { private $memcache; /** * Memcache緩衝-設定緩衝 * 設定緩衝key,value和緩衝時間 * @param string $key KEY值 * @param string $value 值 * @param string $time 緩衝時間 */ public function set_cache($key, $value, $time = 0) { return $this->memcache->set($key, $value, false, $time); } /** * Memcache緩衝-擷取緩衝 * 通過KEY擷取快取資料 * @param string $key KEY值 */ public function get_cache($key) { return $this->memcache->get($key); } /** * Memcache緩衝-清除一個緩衝 * 從memcache中刪除一條緩衝 * @param string $key KEY值 */ public function clear($key) { return $this->memcache->delete($key); } /** * Memcache緩衝-清空所有緩衝 * 不建議使用該功能 * @return */ public function clear_all() { return $this->memcache->flush(); } /** * 欄位自增-用於記數 * @param string $key KEY值 * @param int $step 新增的step值 */ public function increment($key, $step = 1) { return $this->memcache->increment($key, (int) $step); } /** * 欄位自減-用於記數 * @param string $key KEY值 * @param int $step 新增的step值 */ public function decrement($key, $step = 1) { return $this->memcache->decrement($key, (int) $step); } /** * 關閉Memcache連結 */ public function close() { return $this->memcache->close(); } /** * 替換資料 * @param string $key 期望被替換的資料 * @param string $value 替換後的值 * @param int $time 時間值 * @param bool $flag 是否進行壓縮 */ public function replace($key, $value, $time = 0, $flag = false) { return $this->memcache->replace($key, $value, false, $time); } /** * 擷取Memcache的版本號碼 */ public function getVersion() { return $this->memcache->getVersion(); } /** * 擷取Memcache的狀態資料 */ public function getStats() { return $this->memcache->getStats(); } /** * Memcache緩衝-設定連結的伺服器 * 支援多MEMCACHE伺服器 * 設定檔中配置Memcache快取服務器: * $InitPHP_conf['memcache'][0] = array('127.0.0.1', '11211'); * @param array $servers 伺服器數組-array(array('127.0.0.1', '11211')) */ public function add_server($servers) { $this->memcache = new Memcache; if (!is_array($servers) || empty($servers)) exit('memcache server is null!'); foreach ($servers as $val) { $this->memcache->addServer($val[0], $val[1]); } } } 使用方法 $newclass = new memcachedInit(); $newclass->getVersion() //擷取版本號碼 $newclass->close() //關閉Memcache連結 $newclass->clear($key) //從memcache中刪除一條緩衝 $newclass->get_cache($key) //通過KEY擷取快取資料 |