zoukankan      html  css  js  c++  java
  • PHP框架Yii系列教程(四):使用Memcache保存会话

    1环境准备

    安装Memcached服务端:

    yum -y installmemcached.x86_64

    安装PHP-Memcache扩展:

    yum -y installphp-pecl-memcache.x86_64

    查看PHP-Memcache扩展是否安装成功:

    php -m | grepmemcache


    2启动Memcached服务

    分别在11211和11212端口启动主从两个Memcached服务:

    memcached -d -p 11211 -u memcached -m 64 -c1024 -P /var/run/memcached/memcached.pid

    memcached -d -p 11212 -u memcached -m 64 -c1024 -P /var/run/memcached/memcached2.pid

    具体参数含义如下:

    -d选项是启动一个守护进程;

    -m是分配给Memcache使用的内存数量;单位是MB;

    -u是运行Memcache的用户,或者是root,

    -l是监听的服务器IP地址;

    -p是设置Memcache监听的端口;

    -c选项是最大运行的并发连接数,默认是1024;

    -P是设置保存Memcache的pid文件;保存在 /tmp/memcached.pid

    通过Telnet客户端访问:

    telnet 127.0.0.111211

    telnet 127.0.0.111212

    注:如果没有安装Telnet则先执行

    yum -y installtelnet.x86_64

    也可以通过一个PHP文件测试PHP-Memcache扩展和Memcached服务是否连通:

    memcache.php

    ===============================================================================

    <?php

    $memcache = new Memcache();

    $memcache->connect('127.0.0.1', 11211);

    $memcache->set('key', 'Memcache test successful!', 0, 60);

    $result = $memcache->get('key');

    unset($memcache);

    echo $result;

    ?>

    3配置Yii应用组件

    Yii对Memcached提供直接支持,只需简单配置,就可以启用Memcached服务端作为Session服务器。

    1.helloyii/protected/config/main.php

    ===============================================================================

    array(

       'components'=>array(

           'cache'=>array(

               'class'=>'CMemCache',

               'servers'=>array(

                    array(

                        'host'=>'127.0.0.1',

                        'port'=>11211,

                    ),

                    array(

                        'host'=>'127.0.0.1',

                        'port'=>11212,

                    ),

               ),

           ),

             'session' => array (

                 'class'=> 'CCacheHttpSession',

            'cookieMode' => 'only',

            'timeout' => 1200

           ),

    ),

    )

    2.helloyii/protected/controllers/CacheController.php

    ===============================================================================

           class CacheController extends CController

           {

                    public functionactionFetch($key, $value)

                    {

                            Yii::app()->session[$key]= $value;

                            echo'session:['.Yii::app()->session[$key].']';

                            Yii::app()->cache->set($key,$value);

                            $data =Yii::app()->cache->get($key);

                           Yii::app()->getController()->render('result',array('data'=>$data));

                    }

           }

    这个例子分别将键值对保存到session和缓存中。


    4 Memcached客户端操作

    (TODO)

    如何对memcache的数据(key-value)进行遍历操作

    http://www.cnblogs.com/sunli/archive/2008/11/01/1324153.html

    利用shell命令操作Memcached[原创]

    http://www.s135.com/post/384/

    memcached命令行参数说明

    http://blog.csdn.net/zzulp/article/details/7823511

    参考资料

    1 CentOS Memcache安装配置教程

    http://www.bootf.com/442.html

    2 Yii memcache 保存 session

    http://blog.163.com/darwin_zhang/blog/static/12848873620127725122706/

  • 相关阅读:
    基于对象颜色的对象检测(翻译)
    根据颜色和大小来检测和跟踪实时网络摄像机中的对象(翻译)
    C# 笔记 基础(2)
    C#学习笔记 基础 (1)
    深入学习RBAC系列模型——RBAC0模型的开发与学习心得
    RBAC权限管理
    SSL协议的工作流程
    页面的加载
    java实例化对象的方式
    cron表达式详解 原创
  • 原文地址:https://www.cnblogs.com/xiaomaohai/p/6157743.html
Copyright © 2011-2022 走看看