MemCache + PHP

memcache就是把数据放到内从中,从而减少数据库访问次数,提高响应速度的作用


ubuntu安装    #sudo apt-get install memcached


启动参数:

#memcache -d -u linux用户名

-d选项是启动一个守护进程,
-m是分配给Memcache使用的内存数量,单位是MB,
-u是运行Memcache的用户
-l是监听的服务器IP地址
-p是设置Memcache监听的端口,默认11211
-c选项是最大运行的并发连接数,默认是1024
-P是设置保存Memcache的pid文件

启动 后的memcache 可以通过telnet登录(登录后没有提示用户名和密码,直接就输入memcache命令,觉得很不安全.有相关资料的麻烦在评论里告诉一下)


测试是否开启
#telnet 127.0.0.1 11211

命令:
stats:状态
add:添加
set:添加/设置
    存储命令的格式:
        <command name> <key> <flags> <exptime> <bytes>
        <data block>
    格式说明:
        <command name>     set/add/replace
            <key>     查找关键字
            <flags>     客户机使用它存储关于键值对的额外信息
            <exptime>     该数据的存活时间,0表示永远
            <bytes>     存储字节数
            <data block>     存储的数据块(可直接理解为key-value结构中的value)
    提示:STORED存储/
        
get <key> :取值
delete <key>    :删除
flush_all:清空全部

quit:退出


php中的memcache测试

<?php
    //这个函数模拟数据库查询
    function finddb(){
        return array('a','b','c','d');
    }

    $memcache= new Memcache;
    $con=$memcache->connect('127.0.0.1');

    if(!$con){
        $data=finddb();
        $zt='没有链接,直接查询数据库';

    }else{
        if($memcache->get('sta')){
            $data=$memcache->get('data');
            $zt='直接从memcache获取值';
        }else{
            $db=finddb();
            $status=$memcache->set('data',$db,false,20);
            if($status){
                $memcache->set('sta','1',false,18);
                $data=$memcache->get('data');
                $zt='memacache查询数据库,并保存到memcache';
            }else{
                $data=finddb();
                $zt='m没有从数据库中获取得到值,再直接查询数据库';
            }
        }
    }

    var_dump($data);
    echo '<hr />';
    var_dump($zt);

    //$sta=$me->getStats();
    //var_dump($sta);
    //$me->close();

    
?>


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Memcache是一个高性能的分布式内存对象缓存系统,常用于网站的性能优化。在PHP中使用Memcache可以提高网站的访问速度和响应时间。为了方便使用Memcache,很多人会对其做封装,使其更易于使用。以下是一个MemcachePHP封装示例: ```php class MemcacheWrapper { private $memcache; public function __construct() { $this->memcache = new Memcache(); $this->memcache->connect('localhost', 11211); } public function set($key, $value, $expire = 0) { return $this->memcache->set($key, $value, 0, $expire); } public function get($key) { return $this->memcache->get($key); } public function delete($key) { return $this->memcache->delete($key); } public function flush() { return $this->memcache->flush(); } } ``` 在这个封装中,我们使用了一个MemcacheWrapper类来包装Memcache,使其更易于使用。该类提供了四个方法: - set($key, $value, $expire = 0):将$key和$value存储在缓存中,$expire是过期时间,默认为0,表示永不过期。 - get($key):从缓存中获取$key对应的值。 - delete($key):从缓存中删除$key对应的值。 - flush():清空缓存。 使用该封装的示例代码如下: ```php $memcache = new MemcacheWrapper(); $memcache->set('key', 'value', 60); $value = $memcache->get('key'); $memcache->delete('key'); $memcache->flush(); ``` 在这个示例中,我们使用了MemcacheWrapper类的set()方法将键值对存储在缓存中,并指定了过期时间为60秒。然后使用get()方法获取键对应的值,并使用delete()方法删除键值对,最后使用flush()方法清空缓存。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值