php memcache on freebsd

先裝/usr/posrts/databases/memcache

再裝/usr/ports/databases/pecl-memcache

重啟 apache

寫一個測試範本 

<?php
$memcache = new Memcache; //Star memcache
$memcache->connect('localhost', 11211) or die ("Could not connect"); //Connect Memcached
$memcache->set('ezmobo', 'johnpupu'); // Set a variable to the mem key=ezmobo val=test
$get_value = $memcache->get('ezmobo'); //
echo $get_value;
print "<br/>";
$b = $memcache->getStats();
print_r($b);exit;
?>

成功!! 

UPDATE(20060830): 後續還有session 的用法,這幾天再補充。

UPDATE(20060906): Session 用法如下。

加一個session_handler 的php 

<?php
$mem_cache_obj = new Memcache;
$mem_cache_obj->connect('localhost', 11211) or die ("Could not connect");
class Shared_Session
{
  function init()
  {
    ini_set("session.gc_maxlifetime", 3600);
    ini_set("session.use_cookies", 1);
    ini_set("session.cookie_path", "/");
    ini_set("session.cookie_domain", ".yourdomain.com.tw");

    session_module_name("User");
    session_set_save_handler(
        array("Shared_Session", "open"),
        array("Shared_Session", "close"),
        array("Shared_Session", "read"),
        array("Shared_Session", "write"),
        array("Shared_Session", "destroy"),
        array("Shared_Session", "gc")
        );
  }

  function open($save_path, $session_name) {
    return true;
  }

  function close() {
    return true;
  }

  function read($sesskey) {
    global $mem_cache_obj;
    return $mem_cache_obj->get($sesskey);
  }

  function write($sesskey, $data) {
    global $mem_cache_obj;
    $mem_cache_obj->set($sesskey, $data, 3600);
    return true;
  }

  function destroy($sesskey) {
    global $mem_cache_obj;
    $mem_cache_obj->delete($sesskey);
    $mem_cache_obj->flush_all();

    return true;
  }

  function gc($maxlifetime = null) {
    return true;
  }
}

Shared_Session::init();

?> 

 PS: 在測試時發現,因為session會lock 住你目前在使用的session,所以之前在測式時,一直抓不到剛寫入的session,就是這原因。

另外最近學了新的vim skill 心情滿爽的,這幾天再記錄下來。

About johnpupu

..........
This entry was posted in FreeBSD. Bookmark the permalink.

One Response to php memcache on freebsd

  1. Pingback: [FreeBSD] 安裝 memcache for php 多重伺服器共享 Session | 小惡魔 - 電腦技術 - 生活日記 - 美食介紹 - AppleBOY

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

*
To prove you're a person (not a spam script), type the security word shown in the picture. Click on the picture to hear an audio file of the word.
Click to hear an audio file of the anti-spam word