【问题标题】:Using memcached as a session storage with CodeIgniter使用 memcached 作为 CodeIgniter 的会话存储
【发布时间】:2011-02-06 18:02:46
【问题描述】:

我正在研究将 memcached 用作构建在 CodeIgniter 上的系统的 会话存储 的可能性。以前有没有人这样做过(这可能是一个愚蠢的问题:)如果是这样,你的经验是什么?您是否使用过任何现有的库/扩展? 至于性能改进,您看到了什么?有什么注意事项吗?

【问题讨论】:

    标签: php codeigniter session memcached


    【解决方案1】:

    让 PHP 直接将会话放入 Memcache,而不是通过框架代码很容易 - 只需更改 PHP.ini 中的两行:

    # see http://php.net/manual/en/memcache.ini.php
    session.save_handler = memcache
    session.save_path="tcp://127.0.0.1:11211?persistent=1&weight=1&timeout=1&retry_interval=15"
    

    这使用了来自 PECL 的稍旧(但仍完全受支持)的“memcache”扩展。

    【讨论】:

    • 出于显而易见的原因,您可能不想将所有 PHP 会话保存到 memcached。在这种情况下,您应该阅读 3x 系列的 codeigniter 文档,而不是采取解决方法并保存所有项目的所有会话数据:codeigniter.com/userguide3/libraries/…
    【解决方案2】:

    您可以选择CodeIgniter Multicache Library,可以在这里找到:http://www.haughin.com/code/multicache/

    在代码中你可以像这样简单地使用:

    $this->load->library('cache');
    //To use memcache
    $this->cache->useMemcache($iptomemcache, $port); /*if you want, you can check to see if the connection even worked, as this will return false if the connection failed.*/
    $this->cache->save('testkey', 'testdata', NULL, 3600); /*caches the testdata string for 1 hour. */
    echo $this->cache->get('testkey');
    //To switch back to file based caching
    $this->cache->useFile();
    //etc.
    

    【讨论】:

      【解决方案3】:

      使用 Memcached 存储关系数据(如 MySQL)是不切实际的;从 Memcached 请求每个项目然后测试它是否与查询匹配是低效的。对于这样的问题有更好的解决方案(例如,考虑 MySQL 中的内存表)。

      另一方面,如果您正在寻找简单的键/值存储,那肯定是 Memcached 的实际应用。不过,我有点担心的是为它编写一个 CodeIgniter 驱动程序。 PHP 中 Memcached 的接口已经非常简单了:

      $memcached->get('my key');
      $memcached->set('my key', 'my value');
      

      我建议直接使用 Memcached 类。将所有额外开销添加到 CI 对我来说似乎很脏而且没有必要。

      另一方面,我看到了用于 CodeIgniter 会话引擎的 Memcached 实现。这当然是编写驱动程序的一个非常正当的理由,我会极力鼓励它(会话对规模来说是一件令人头疼的事情)。

      祝你好运

      【讨论】:

        猜你喜欢
        • 2017-02-17
        • 1970-01-01
        • 2013-12-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-10-18
        • 2015-10-01
        • 1970-01-01
        相关资源
        最近更新 更多