PHP 資料庫緩衝Memcache操作類

來源:互聯網
上載者:User

例子,代碼如下

 代碼如下 複製代碼

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擷取快取資料

上面就簡單介紹了它的使用方法了,其實還有很多在這裡我就不介紹了呀。

總結,這個只是一個最基於的Memcache快取作業類了,比起像資料庫緩衝類操作會更好更複雜了哦,希望例子能協助到各位朋友哦。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.