鑒於上次的デュエル学園因為讀取速度太慢造成很多時間差上面的BUG
這次的モバゲーアプリ決定使用Memcached和SESSION來解決
由於視同時使用多台前端SERVER然後又特別準備了一臺Memcache SERVER
所以就必須把SESSION存到Memcache SERVER中囉!
yum -y install libevent-devel gcc-c++ php php-pecl-memcache wget http://memcached.googlecode.com/files/memcached-1.4.5.tar.gz tar zxvf memcached-1.4.5.tar.gz cd memcached-1.4.5 ./configure make make install memcached -d -m 1024 -p 11211 -u root |
啟動參數說明
-d 指定用 daemon 方式
-m 指定使用記憶體容量(單位MB)
-p 指定要監聽的 port (不指定則預設 11211)
-u 指定用哪個帳號來執行,例如 nobody
接著編輯/etc/php5/apache2/php.ini 加上
extension=memcache.so |
重新啓動
/etc/init.d/apache2 restart |
至於如何使用Memcached來存取SESSION
可以參考一下這個CLASS:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 | /* * Sample code to use SessionManagerMemCache class * //////////////////////////////////////////////////////////////////////////////////////// // Add Session Class require_once("my_class_file_path/class.Session.php"); // Initialize session info here $server['info'] = "127.0.0.1"; $server['port'] = "11211"; $sess = new SessionManagerMemCache($server); if(isset($_REQUEST['sessionId']) && $_REQUEST['sessionId']!="") { session_id($_REQUEST['sessionId']); // To have the param [sessionId] with all the URLS and form tags output_add_rewrite_var('sessionId', $_REQUEST['sessionId']); } session_start(); //////////////////////////////////////////////////////////////////////////////////////// * * * */ class SessionManagerMemCache { var $lifetime = 0; var $memcache = null; function __construct($server = array('info'=>'xxx.xxx.xxx.xxx', 'port'=>'11211')) { register_shutdown_function("session_write_close"); $this->lifetime = intval(ini_get("session.gc_maxlifetime")); $this->memcache = new Memcache; if(!$this->memcache->connect($server['info'], $server['port'])) { die("Memcache server was not connected successfully."); } // Register this object as the session handler session_set_save_handler( array( &$this, "open" ), array( &$this, "close" ), array( &$this, "read" ), array( &$this, "write"), array( &$this, "destroy"), array( &$this, "gc" ) ); } function open() { return true; } function read($id) { return $this->memcache->get("sessions/{$id}"); } function write($id, $data) { return $this->memcache->set("sessions/{$id}", $data, MEMCACHE_COMPRESSED, $this->lifetime); } function destroy($id) { return $this->memcache->delete("sessions/{$id}"); } function gc(){ return true; } function close(){ $this->lifetime = null; $this->memcache = null; return true; } function __destruct() { session_write_close(); } } |