[精讚] [會員登入]
116

[PHP+ci4] codeignitor4 Cache 及 Session 使用 memcached

php 的framework codeignitor4 中的cache和session使用memcached

分享此文連結 //n.sfs.tw/16398

分享連結 [PHP+ci4] codeignitor4 Cache 及 Session 使用 memcached@新精讚
(文章歡迎轉載,務必尊重版權註明連結來源)
2024-04-13 01:40:12 最後編修
2024-04-08 13:04:32 By 張○○
 

自動目錄

Codeignitor4(以下全稱為ci4) 自帶提供memcache,設置上非常方便,其實ci3上就有memcache,但是因為 selinux的關係,我一直試不成功,所以放棄。

memcache是使用記憶體當空間給session或cache使用,7版以前的php使用的是Memcache類別,8版使用 Memcached類別,除了名稱多了一個 d以外,裡面的方法都不一樣。

ci4上設置有兩個部分,一是修改cache另一個是修改session的設置,兩者可擇一設置,session的部分如果要使用memcache的話,需安裝 php-memcached 這個模組並開啟。

以下文章中 memcache 指的是這個服務,memcached 是這個服務的程式,名稱上常常會混用,要注意看,雖然我也很困擾。

程式碼來源於[1],有小部分修改。

此次異動的檔案有二個,在app/目錄下

├── Config
│   ├── Cache.php  <== Cacha的設定
│   └── Session.php <== Session的設定
├── Controller
│   └── TestSession.php <== 測試用,非必要
└── .env <==設定顯示錯誤

Config/Cache.php

修改兩個地方即可[1] :

...
//    public string $handler = 'file';  <== 註解原本的,加上下面這行
    public string $handler ='memcached';
...

    public array $memcached = [
        'host'   => 'memcache', //'127.0.0.1',  <== 改成ip或是container的service name
        'port'   => 11211,
        'weight' => 1,
        'raw'    => false,
    ];

其餘不必動
 

Config/Session.php

修改或新增以下兩行

//    public string $driver = FileHandler::class;  <== 註解這行
    public string $driver = 'CodeIgniter\Session\Handlers\MemcachedHandler';
...
//    public string $savePath = WRITEPATH . 'session';  <== 註解這行
    public string $savePath = 'memcache:11211';

其中 $savepath 的 memcahce 改成ip即可。

 

讓你的php支援 php-memcached 

在session中使用memcached得安裝 php-memcached 並啟用。

安裝 php memcached 模組

# yum install php-pecl-memcache

重啟後請檢查

# php -m

...
mbstring
memcached   <== 需要有這個模組
mysqli
...

Docker中安裝 php-memcached

預設docker php:8.2-fpm 中並無安裝此模組,要自行建立,請在 Dockerfile 中加入[4]

RUN
  ...
  apt-get install -y libmemcached-dev zlib1g-dev libssl-dev && \
  yes '' | pecl install -f memcached-3.2.0 && docker-php-ext-enable memcached  && \
  ...

 

測試及除錯

使用的memcache如果無法連線,大概率會得到這樣的錯誤:

codeignitor4 Fatal error: Uncaught ErrorException: session_write_close(): Failed to write session data using user defined save handler. memcached

這樣的錯誤無法清楚的知道是怎樣的問題,所以可以使用下面兩種方式先確定你的memcache伺服器是正常的。

 

使用php原生的方式測試 [5]

$m= new MemCached();

$m->addServer('memcached', 11211);

$m->set('int', 99);
$m->set('string', 'a simple string');
$m->set('array', array(11, 12));
/* expire 'object' key in 5 minutes */
$m->set('object', new stdClass, time() + 300);
print $m->get('int');

var_dump($m->get('int'));
var_dump($m->get('string'));
var_dump($m->get('array'));
var_dump($m->get('object'));

結果:

99int(99)
string(15) "a simple string"
array(2) {
  [0]=>
  int(11)
  [1]=>
  int(12)
}
object(stdClass)#2 (0) {
}

CI4中叫用測試

session原則上和原本使用的方式一樣,不必特別改變習慣

Controller 中session叫用測試:

$session = session();

$session->set('cachename', 'My Memcache');
print $session->get('cachename');
//or
print $session->cachename;

結果:

My MemcacheMy Memcache

備註,CI4出現 whoops! 怎麼辦

如果出現 whoops!,到 ci4的根目錄下[3]

$ cp env .env

修改.env 把

CI_ENVIRONMENT = production

換成這個

CI_ENVIRONMENT = development

 

預設使用session library

如果要預設載入 session的library,修改 BaseController.php [2]

    public $session;
    public function initController(RequestInterface $request, ResponseInterface $response, LoggerInterface $logger)
    {
        // Do Not Edit This Line
        parent::initController($request, $response, $logger);

        // Preload any models, libraries, etc, here.
        $this->session = \Config\Services::session();
    }

其它controller 直接使用 $this->session 即可

 

參考資料

[1] https://codeigniter.com/user_guide/libraries/caching.html#memcached-caching

[2] https://forum.codeigniter.com/showthread.php?tid=72170

[3] https://stackoverflow.com/questions/60558157/why-whoops-error-showing-in-codeigniter-4

[4] https://stackoverflow.com/questions/76490445/problem-adding-memcached-support-in-docker-for-php8-1-using-bookworm

[5] https://www.php.net/manual/en/memcached.set.php

 

END

你可能感興趣的文章

使用Yahoo OAuth2 2/2 使用Yahoo OAuth2認證我的網頁

[PHP] 將UTF8中文字轉成10進位或16進位數值 原本為了處理 preg_match 中文字的問題[2],用php把中文字轉換成10進位和6進位的數值編碼

設定Google analytics API #2 -- PHP的程式安裝和撰寫 讓你的網站能夠存取你的Google analytics上面的資料

[PHP] 移除檔案的UTF8 BOM 移除檔案的UTF8 BOM

[PHP] preg_match 的貪婪和不貪婪比對 在php preg_match中預設是採用貪婪比對,太貪婪反而不符合需要,因此得採用「非貪婪比對」...

利用 php 木馬作為駭客攻擊的手法 利用 php 木馬作為駭客攻擊的手法

我有話要說

>>

限制:留言最高字數1000字。 限制:未登入訪客,每則留言間隔需超過10分鐘,每日最多5則留言。

訪客留言

[無留言]

隨機好文

世紀帝國征服者新版本--被遺忘的帝國 世紀帝國征服者新版本--被遺忘的帝國 世紀二代的征服者是精典遊戲中的精典,aofe更好玩...

問問題 問問題其實內涵很深,我悟了很久才懂。 有人問題的目的並不一定是想要得到答案,有時只是純粹想問問題..

超扯童話血多-賣火柴小女孩 這個真的是太扯了,扯到一直笑,尤其是作者出來那段 因為他第一到第九會連播,笑滿累的。如果你看不懂的話表示你的大腦還滿正經

維修洗衣機 2005年初買的西屋15KG洗衣機,丟在陽台風吹雨打日曬雨淋,終於在2009.08的時候罷工了...

[HTLM5] 表單color、email、url、search、tel輸入類型 介紹HTML5好用的輸入類型:color、email、url,以及兩個形同雞肋的類型search、tel..