ThinkPHP使用Memcached快取資料

2022-07-22 04:18:09 字數 1150 閱讀 8453

thinkphp預設使用檔案快取資料,支援memcache等其他快取方式,有兩個php擴充套件:memcache和memcached,memcahe官方有說明,主要說一下memcached。

相對於php memcache,php memcached是基於原生的c的libmemcached的擴充套件,更加完善,建議替換為php memcached。

版本3.2.2開始內建了memcached驅動(thinkphp/library/think/cache/driver/memcached.class.php),但是文件中並沒有說明用法,通過檢視原始碼配置並測試成功。

有個bug至今未修復,就是過期時間為0的問題,理論上應該是永久快取,但是驅動中未做處理,會馬上過期,set方法修改如下

public

function set($name, $value, $expire = null

)

$name = $this->options['prefix'].$name

;

if (empty($expire

))

$time = 0;

else

$time = time() + $expire

;

if($this->handler->set($name, $value, $time

))

return

true

; }

return

false

; }

在配置檔案config.php中新增

//

快取配置

'data_cache_type' => 'memcached',

'memcached_server' => array

(

array('127.0.0.1', 11211, 0)

),

驅動中是呼叫memcached::addservers(array)可以新增多個快取伺服器

還有乙個配置項是 memcached_lib,呼叫的是 memcached::setoptions(array)

具體選項可參考php中文手冊

memcache 使用php操作memcache

1 概念 memcache 是乙個高效的分布式的記憶體物件快取系統,他可以支援把php的各種資料 陣列,物件,基本資料型別 放在它管理的記憶體中 2.安裝步驟 2 開啟php.ini檔案 這裡啟用memcache.dll擴充套件 extension php memcache.dll 3 使用 1.例...

windows php環境下安裝memcache

準備 首先檢視php的版本,根據php的版本 步驟1 安裝memcached 一.解壓檔案 二.開始 執行 輸入cmd開啟dos視窗 跳轉到memcached的解壓目錄 如 c program files memcache 三.安裝memcached 命令 memcached.exe d insta...

nginx 下在擴充套件Redis和memcache

1.檢視php有沒有相關擴充套件 php m grep redis 2.如果沒有 安裝php redis 的擴充套件 pecl install redis pecl是乙個php擴充套件庫 3.驗證是否成功 檢視php有沒有相關擴充套件 php m grep redis 4.重啟php service...