首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何在APC Cache中存储PHP会话?

如何在APC Cache中存储PHP会话?
EN

Stack Overflow用户
提问于 2009-11-13 03:05:34
回答 9查看 11.4K关注 0票数 20

在磁盘中存储会话对我来说非常慢和痛苦。我的车流量很大。我想在高级PHP缓存中存储会话,我该怎么做?

EN

回答 9

Stack Overflow用户

回答已采纳

发布于 2012-12-09 06:01:56

我试图通过提供100分作为赏金来吸引更好的答案,但没有一个答案真正令人满意。

我会像这样汇总推荐的解决方案:

使用APC作为会话存储

APC不能真正用作会话存储,因为APC没有允许正确锁定的机制,但这种锁定对于确保没有人在将最初读取的会话数据写回之前更改它是必不可少的。

底线:避免它,它不会起作用的。

替代方案

可能有许多会话处理程序可用。在Session部分检查phpinfo()的输出中是否有“注册的存储处理程序”。

RAM磁盘上的文件存储

开箱即用,但出于显而易见的原因,需要一个挂载为RAM磁盘的文件系统。

共享内存(mm)

在启用mm的情况下编译PHP时可用。这是内置在windows上的。

Memcache(%d)

PHP为此提供了一个专用的会话保存处理程序。需要安装memcache服务器和PHP客户端。根据安装了两个memcache扩展中的哪一个,保存处理程序称为memcachememcached

票数 9
EN

Stack Overflow用户

发布于 2012-08-05 01:26:22

<?php

// to enable paste this line right before session_start():
//   new Session_APC;
class Session_APC
{
    protected $_prefix;
    protected $_ttl;
    protected $_lockTimeout = 10; // if empty, no session locking, otherwise seconds to lock timeout

    public function __construct($params=array())
    {
        $def = session_get_cookie_params();
        $this->_ttl = $def['lifetime'];
        if (isset($params['ttl'])) {
            $this->_ttl = $params['ttl'];
        }
        if (isset($params['lock_timeout'])) {
            $this->_lockTimeout = $params['lock_timeout'];
        }

        session_set_save_handler(
            array($this, 'open'), array($this, 'close'),
            array($this, 'read'), array($this, 'write'),
            array($this, 'destroy'), array($this, 'gc')
        );
    }

    public function open($savePath, $sessionName)
    {
        $this->_prefix = 'BSession/'.$sessionName;
        if (!apc_exists($this->_prefix.'/TS')) {
            // creating non-empty array @see http://us.php.net/manual/en/function.apc-store.php#107359
            apc_store($this->_prefix.'/TS', array(''));
            apc_store($this->_prefix.'/LOCK', array(''));
        }
        return true;
    }

    public function close()
    {
        return true;
    }

    public function read($id)
    {
        $key = $this->_prefix.'/'.$id;
        if (!apc_exists($key)) {
            return ''; // no session
        }

        // redundant check for ttl before read
        if ($this->_ttl) {
            $ts = apc_fetch($this->_prefix.'/TS');
            if (empty($ts[$id])) {
                return ''; // no session
            } elseif (!empty($ts[$id]) && $ts[$id] + $this->_ttl < time()) {
                unset($ts[$id]);
                apc_delete($key);
                apc_store($this->_prefix.'/TS', $ts);
                return ''; // session expired
            }
        }

        if (!$this->_lockTimeout) {
            $locks = apc_fetch($this->_prefix.'/LOCK');
            if (!empty($locks[$id])) {
                while (!empty($locks[$id]) && $locks[$id] + $this->_lockTimeout >= time()) {
                    usleep(10000); // sleep 10ms
                    $locks = apc_fetch($this->_prefix.'/LOCK');
                }
            }
            /*
            // by default will overwrite session after lock expired to allow smooth site function
            // alternative handling is to abort current process
            if (!empty($locks[$id])) {
                return false; // abort read of waiting for lock timed out
            }
            */
            $locks[$id] = time(); // set session lock
            apc_store($this->_prefix.'/LOCK', $locks);
        }

        return apc_fetch($key); // if no data returns empty string per doc
    }

    public function write($id, $data)
    {
        $ts = apc_fetch($this->_prefix.'/TS');
        $ts[$id] = time();
        apc_store($this->_prefix.'/TS', $ts);

        $locks = apc_fetch($this->_prefix.'/LOCK');
        unset($locks[$id]);
        apc_store($this->_prefix.'/LOCK', $locks);

        return apc_store($this->_prefix.'/'.$id, $data, $this->_ttl);
    }

    public function destroy($id)
    {
        $ts = apc_fetch($this->_prefix.'/TS');
        unset($ts[$id]);
        apc_store($this->_prefix.'/TS', $ts);

        $locks = apc_fetch($this->_prefix.'/LOCK');
        unset($locks[$id]);
        apc_store($this->_prefix.'/LOCK', $locks);

        return apc_delete($this->_prefix.'/'.$id);
    }

    public function gc($lifetime)
    {
        if ($this->_ttl) {
            $lifetime = min($lifetime, $this->_ttl);
        }
        $ts = apc_fetch($this->_prefix.'/TS');
        foreach ($ts as $id=>$time) {
            if ($time + $lifetime < time()) {
                apc_delete($this->_prefix.'/'.$id);
                unset($ts[$id]);
            }
        }
        return apc_store($this->_prefix.'/TS', $ts);
    }
}
票数 17
EN

Stack Overflow用户

发布于 2009-11-13 03:18:40

从理论上讲,您应该能够编写一个custom session handler,它使用APC为您透明地完成这项工作。然而,在5分钟的快速搜索中,我实际上没有找到任何真正有希望的东西;大多数人似乎使用APC作为字节码缓存,并将他们的会话放在memcached中。

票数 10
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/1724587

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档