簡體   English   中英

使用memcached作為CodeIgniter的會話存儲

[英]Using memcached as a session storage with CodeIgniter

我正在研究將memcached用作構建在CodeIgniter上的系統的會話存儲的可能性。 有沒有人這樣做過(這可能是一個愚蠢的問題:)如果是這樣,你的經驗是什么? 您是否使用過任何現有的庫/擴展? 對於性能提升,您看到了什么? 有什么警告嗎?

讓PHP直接將會話放入Memcache,而不是通過框架代碼很容易 - 它只是改變了PHP.ini中的兩行:

# see http://php.net/manual/en/memcache.ini.php
session.save_handler = memcache
session.save_path="tcp://127.0.0.1:11211?persistent=1&weight=1&timeout=1&retry_interval=15"

這使用了PECL稍微較舊(但仍然完全支持)的“memcache”擴展。

您可以選擇CodeIgniter Multicache Library ,可在此處找到: http//www.haughin.com/code/multicache/

在代碼中,您可以像這樣簡單地使用:

$this->load->library('cache');
//To use memcache
$this->cache->useMemcache($iptomemcache, $port); /*if you want, you can check to see if the connection even worked, as this will return false if the connection failed.*/
$this->cache->save('testkey', 'testdata', NULL, 3600); /*caches the testdata string for 1 hour. */
echo $this->cache->get('testkey');
//To switch back to file based caching
$this->cache->useFile();
//etc.

使用Memcached存儲關系數據(如MySQL)是不切實際的; 從Memcached請求每個項目然后測試它是否與查詢匹配將是低效的。 對於像這樣的問題有更好的解決方案(例如,考慮MySQL中的內存表)。

另一方面,如果您正在尋找簡單的鍵/值存儲,那肯定是Memcached的實際應用。 不過,我有點擔心的是為它編寫CodeIgniter驅動程序。 PHP中的Memcached接口已經很簡單了:

$memcached->get('my key');
$memcached->set('my key', 'my value');

我建議直接使用Memcached類。 將所有額外開銷添加到CI中對我來說似乎很臟並且沒有必要。

另一方面,我已經看到用於CodeIgniter會話引擎的Memcached實現。 這當然是寫一個驅動程序的一個非常正當的理由,我會高度鼓勵它(會話是一個痛苦的規模)。

祝好運

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM