TokyoTyrant 搭建 session 服务器

纯记录 

 

#安装 Tokyo Cabinet

wget http://1978th.net/tokyocabinet/tokyocabinet-1.4.47.tar.gz
tar zxvf tokyocabinet-1.4.47.tar.gz
cd tokyocabinet-1.4.47
./configure --enable-off64
#32位系统需要 --enable-off64,以让它支持大于2GB的文件
make
make install
cd ..
#安装 lua
wget http://www.lua.org/ftp/lua-5.1.4.tar.gz
tar zxvf lua-5.1.4.tar.gz
cd lua-5.1.4
#要根据系统而定,直接make它会有提示
make linux
make install
ln -s /usr/local/bin/lua /usr/bin/lua
cd ..
#安装Tokyo Cabinet 的 lua 扩展
wget http://1978th.net/tokyocabinet/luapkg/tokyocabinet-lua-1.10.tar.gz
tar zxvf tokyocabinet-lua-1.10.tar.gz
cd tkoyocabinet-lua-1.10
./configure
make
make install
cd ..
#安装 Tokyo Tyrant
wget http://1978th.net/tokyotyrant/tokyotyrant-1.1.41.tar.gz
tar zxvf tokyotyrant-1.1.41.tar.gz
cd tokyotyrant-1.1.41
./configure --enable-lua --with-lua
make
make install
cd ../
mkdir -p /session/
#还需要一个 expire.lua 文件,这个文件用于定时将过期的 session 清理。代码如下:
cat > /session/expire.lua << "EOF" 
function expire()
local args = {}
local cdate = string.format("%d", _time())
table.insert(args, "addcond\0ts\0NUMLE\0" .. cdate)
table.insert(args, "out")
local res = _misc("search", args)
if not res then
_log("expiration was failed", 2)
end
end
EOF
ulimit -SHn 51200
ttserver -host 127.0.0.1 -port 11222 -ext /session/expire.lua -extpc expire 5.0 -thnum 8 -dmn -pid /session/session.pid -log /session/session.log -le -ulog /session/ -ulim 128m -sid 1 -rts /session/session.rts '/session/session.tct#idx=key:lex#idx=ts:dec#dfunit=8'
#安装pecl tokyo_tyrant
wget http://pecl.php.net/get/tokyo_tyrant
tar zxvf tokyo_tyrant
cd tokyo_tyrant-*
/usr/local/php/bin/phpize
./configure --with-php-config=/usr/local/php/bin/php-config 
make && make install
echo '[tokyo_tyrant]
extension="/usr/local/php/lib/php/extensions/no-debug-non-zts-20060613//tokyo_tyrant.so"' >> /usr/local/php/lib/php.ini
cd ../

<?php

/**
 * Encoding     :   UTF-8
 * Created on   :   2011-05-15 05:18:09
 */
class Session {

    private $sessionCN = 'sessionid';
    private $sessionid;
    private $tt;

    public function __construct() {
        define('SESS_EXPIRE', 3600);
        $this->sessionCN;
        $this->issessionid();
        $this->tt =  new TokyoTyrantTable('127.0.0.1', 11222);
    }

    public function get($name) {
        $sessvl = $this->tt->get($this->sessionid);
        $arr = unserialize($sessvl['vaule']);
        return $arr[$name];
    }

    public function set($name, $vaule=NULL, $sessiontimeleft=SESS_EXPIRE) {
        $sessvl = $this->tt->get($this->sessionid);
        $arr = unserialize($sessvl['vaule']);
        if ($vaule == NULL)
            unset($arr[$name]);
        else
            $arr[$name] = $vaule;
        $this->tt->put($this->sessionid, array('vaule' => serialize($arr), 'ts' => time() + $sessiontimeleft));
    }

    private function creatsessionid() {

        $this->sessionid = md5(uniqid());
        setcookie($this->sessionCN, $this->sessionid, 9999999999, NULL, NULL, NULL, TRUE);
    }

    private function issessionid() {
        if (isset($_COOKIE[$this->sessionCN]))
            $this->sessionid = $_COOKIE[$this->sessionCN];
        else
            $this->creatsessionid();
    }

}
 使用方法
<?php header("Content-type: text/html; charset=utf-8"); ?>
<?php

include 'class/Session.php';
$ss = new Session();
$ss->set('a', 's2a');
$ss->set('b', 's2b');
$ss->set('bc', 's2b');

$ss->set('bc', '');
var_dump($ss->get('bc'));
 


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值