How to use memcached from codeigniter
ที่นี้มาถึงวิธีแก้ปัญหาในการโค้ด codeigniter ใช้ memcache library หลังจากลอง หาในกูเกิลก็มาเจอ เวบนี้ครับ สามารถไปโหลดมาได้เลยครับที่ http://github.com/egnity/codeigniter-memcache/ ก็เอาไฟล์ MemcacheLibrary.php ไปใส่ไว้ในโฟลเดอร์ library แล้วก็ไฟล์ memcache.php ใส่ไว้ในโฟลเดอร์ config แต่ทีนี้ โค้ดที่ได้มาจะมาปัญหานิดหน่อยครับ (คาดว่าน่าจะเป็นที่เวอร์ชั่นของ memcache library ของ php) แก้โค้ดตามนี้นะครับ
ที่ฟังก์ชั่น __construct
public function __construct()
{
/* initialize memcached instance */
if (class_exists(“Memcache”)) {
$this->memcachedInstance = new Memcache();
} else {
throw new Exception(“Memcached client doesn’t exists in your PHP configuration”);
}
/* load super CI instance */
$this->CI =& get_instance();
/* load default server info */
$this->CI->config->load(“memcache”);
/* connect to default server */
if ($this->CI->config->item(“MEMCACHE_HOST”) && $this->CI->config->item(“MEMCACHE_PORT”)) {
$this->addServer($this->CI->config->item(“MEMCACHE_HOST”), $this->CI->config->item(“MEMCACHE_PORT”));
}
}
แก้จาก class_exists(“Memcached”) เป็น class_exists(“Memcache”)
และที่ฟังก์ชั่น set()
public function set($key, $value, $expire = null)
{
$this->logDebugMessage(sprintf("%s key set to memcache. (expire: %s)", $key, $expire));
return $this->memcachedInstance->set($key, $value, 0, $expire);
}
เพิ่ม พารามิเตอร์ไปตัวหนึ่งครับ จาก set($key, $value, $expire); เป็น set($key, $value,0, $expire);
ส่วนพารามิเตอร์ตัวไหนใช้ทำอะไรไปดูเอาเองที่นี่นะครับ http://php.net/manual/en/book.memcache.php

