zoukankan      html  css  js  c++  java
  • memcached操作

    add

    往内存增加一行新记录
    语法: add key flag expire length 回车

    1:编译 memcached 时,指定一个最长常量,默认是 30天.
    所以,即使设为 0,30天后也会失效.
    2:可能等不到 30天,就会被新数据挤出去.

    add color 0 10 3

    red

    delete
    delete key [time seconds]
    删除指定的 key. 如加可选参数 time,则指删除 key,并在删除 key 后的 time 秒内,不允许
    get,add,replace操作此 key.

    delete color

    replace

    替换
    replace key flag expire length
    参数和 add完全一样,不单独写

    get

    查询
    get key
    返回 key 的值

    set

    设置和修改值
    参数和 add ,replace 一样,但功能不一样.

     而 set 想当于有 add replace 两者的功能.
    set key flag expire leng 时
    如果服务器无此键 ----> 增加的效果
    如果服务器有此键 ----> 修改的效果.

    如下图的演示,该图中,name 是已经存在,而 date 原本不存在. set 都可以成功设置他们.

    incr ,decr

    命令:增加/减少值的大小
    incr/decr key num

    incr age 1

    注意:incr,decr 操作是把值理解为 32 位无符号来+-操作的. 值在[0-2^32-1]范围内

    查看memcached运行状态

    stat pid 2296 进程号
    stat uptime 4237 持续运行时间
    stat time 1370054990
    stat version 1.2.6
    stat pointer_size 32
    stat curr_items 4 当前存储的键个数
    stat total_items 13
    stat bytes 236
    stat curr_connections 3
    stat total_connections 4
    stat connection_structures 4
    stat cmd_get 20
    stat cmd_set 16
    stat get_hits 13
    stat get_misses 7 // 这 2个参数 可以算出命中率
    stat evictions 0
    stat bytes_read 764
    stat bytes_written 618
    stat limit_maxbytes 67108864
    stat threads 1
    end
    缓存有一个重要的概念: 命中率.
    命中率是指: (查询到数据的次数/查询总数)*100%
    如上, 13/(13+7) = 60+% , 的命中

     flush_all 清空所有的存储对象,慎用

    编译memcached的php扩展

    [root@localhost src]# wget http://pecl.php.net/get/memcache-2.2.7.tgz

    [root@localhost src]# cd memcache-2.2.7

    运行下面这个命令可以生成可执行脚本:

    [root@localhost memcache-2.2.7]# /usr/local/php/bin/phpize

    [root@localhost memcache-2.2.7]# ./configure --with-php-config=/usr/local/php/bin/php-config

    [root@localhost memcache-2.2.7]# make && make install

     

    安装memcache扩展成功!

    配置memcache:

    找到php.ini的存放目录

    [root@localhost memcache-2.2.7]# vim /etc/php/php.ini

    安装memecache扩展成功

    安装php扩展,总结一下,就是:

    1:  到pecl.php.net    去寻找扩展源码并下载解压
    2:切换到解压⽬录
    3:运⾏path/php/bin/phpize命令,动态创建编译选项脚本⽂件;
    4:make    &&    make    install    编译安装
    5:编译成功后,会⽣成.so⽂件,在php.ini中引⼊
    6:重启PHP

    小试一下:

    $mem = new Memcache;
    $mem->connect('localhost', 11211);
    $new = $mem->get('new');
    if(!$var){
        $new = 'ceshishuju';
        $mem->add('new',$new,false,10);
        echo 'mysql';
    }else{
        echo 'cache';
    }

    <?php

    $memcache= new Memcache;
    $sql = 'select goods_id,goods_name from ecs_goods where is_hot=1 limit
    5';
    // 判断 memcached 中是否缓存热门商品,如果没有,则查询数据库
    $hot = array();
    if( !($hot=$memcache->get($sql)) ) {
    $hot = $mysql->getAll($sql);
    echo '<font color="red">查询自数据库</font>';
    //从数据库取得数据后,把数据写入 memcached
    $memcache->add($sql,$hot,0,300); // 并设置有效期 300 秒
    } else {
    echo '<font color="red">查询自 memcached</font>';
    }
    ?>

  • 相关阅读:
    .NET I/O 学习笔记:目录和文件
    WCF学习笔记(1)——Hello WCF
    《Effective C#》读书笔记——条目10:使用可选参数减少方法重载的数量<C#语言习惯>
    .NET I/O 学习笔记:文件的读和写
    开场白:我关注反洗钱、大家都来关注反洗钱
    关于EOM(Enterprise Operating Model)企业经营模型(12)
    关于EOM(Enterprise Operating Model)企业经营模型(7)
    关于EOM(Enterprise Operating Model)企业经营模型(9)
    关于EOM(Enterprise Operating Model)企业经营模型(10)
    关于EOM(Enterprise Operating Model)企业经营模型(11)
  • 原文地址:https://www.cnblogs.com/xiong63/p/6284266.html
Copyright © 2011-2022 走看看