首页 > mysql > memcached +mysql+php 测试例子

memcached +mysql+php 测试例子

2010年5月18日 eric 发表评论 阅读评论

最近看了看memcached 缓存 mysql,经过一段时间的研究·把自己测试方法写出来,给像我一样的初学者用~
我的环境是 nginx + php-fpm + apc + mysql

编译安装memcached

tar zxvf memcached-1.4.0.tar.gz
cd memcached-1.4.0
./configure --prefix=/usr
make
make install

启动memcached

memcached -d -m 64 -u root -l 192.168.6.191 -p 11211 -c 64 -P /tmp/memcached.pid
/usr/local/mysql/bin/mysql -uroot -p

创建一个测试数据库 mydb

create database mydb;

创建一个空的数据表 personal_info

CREATE TABLE `personal_info` (
`pi_id` bigint(20) NOT NULL auto_increment,
`pi_name` varchar(50) NOT NULL,
`pi_tel` varchar(15) default NULL,
`pi_qq` varchar(15) default NULL,
`pi_email` varchar(50) default NULL,
PRIMARY KEY (`pi_id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=4 ;

给表插入数据

INSERT INTO `mydb`.`personal_info` (
`pi_id` ,
`pi_name` ,
`pi_tel` ,
`pi_qq` ,
`pi_email`
)
VALUES (
'1', 'eric', '13611031222', '55555555', 'eric@nginxs.com'
);

创建一个 php 文件 mysql-memcache.php

<?php
$memcachehost = '192.168.6.191';
$memcacheport = 11211;
$memcachelife = 60;
$memcache = new Memcache;
$memcache->connect($memcachehost,$memcacheport) or die ("Could not connect");
$query="select * from personal_info limit 10";
$key=md5($query);
if(!$memcache->get($key))
{
        $conn=mysql_connect("localhost","root","passwd");
        mysql_select_db(mydb);
        $result=mysql_query($query);
        while ($row=mysql_fetch_assoc($result))
        {
            $arr[]=$row;
        }
        $f = 'mysql';
        $memcache->add($key,serialize($arr),0,30);    //mysql 查询后,插入 memcached
        $data = $arr ;
}
else{
    $f = 'memcache';
    $data_mem=$memcache->get($key);
    $data = unserialize($data_mem);
}
echo $f;
echo "<br>";
//print_r($data);
foreach($data as $a)
{
        echo "number is <b><font color=#FF0000>$a[pi_id]</font></b>";
        echo "<br>";
        echo "name is <b><font color=#FF0000>$a[pi_name]</font></b>";
        echo "<br>";
        echo "tel is <b><font color=#FF0000>$a[pi_tel]</font></b>";
        echo "<br>";
        echo "qq is <b><font color=#FF0000>$a[pi_qq]</font></b>";
        echo "<br>";
        echo "email is <b><font color=#FF0000>$a[pi_email]</font></b>";
        echo "<br>";

}
?>

测试 第一次访问 提示 mysql

刷新,第二次访问,提示memcache

转载请注明:文章转载自:Eric's linux and nginx! (http://www.nginxs.com)
本文地址:http://www.nginxs.com/linux/152.html

分类: mysql 标签: , ,
  1. 2018年1月8日05:10 | #1

    If some one desires expert view on the topic of blogging and site-building then i recommend him/her to pay a visit this weblog, Keep
    up the pleasant work.

  2. 2018年1月8日07:04 | #2

    Hi there! I could have sworn I’ve been to this website before but after browsing through some of the post I realized
    it’s new to me. Anyhow, I’m definitely happy I found it
    and I’ll be bookmarking and checking back frequently!

  3. 2018年1月8日08:33 | #3

    Great delivery. Great arguments. Keep up the great work.

  4. 2018年1月9日17:45 | #4

    Nice post. I was checking constantly this blog and I’m impressed!
    Extremely useful info particularly the closing section :) I maintain such info much.
    I was seeking this particular info for a long time.
    Thank you and best of luck.

  5. 2018年1月10日11:21 | #5

    Awesome! Its really amazing post, I have got much clear idea
    about from this paragraph.

  6. 2018年1月11日01:52 | #6

    loans no credit
    payday loans no credit
    payday loans no credit check
    payday loans no credit check
    payday loans online no credit check

  7. 2018年1月13日03:26 | #7

    I was wondering if you ever considered changing the page layout of your site?
    Its very well written; I love what youve got to say. But maybe you could a little more in the way of content
    so people could connect with it better. Youve got an awful lot of text
    for only having 1 or 2 pictures. Maybe you could space it
    out better?

  8. 2018年1月13日04:29 | #8

    My partner and I stumbled over here from a different page and thought I should
    check things out. I like what I see so now i am following you.
    Look forward to exploring your web page again.

  9. 2018年1月13日17:27 | #9

    casino online
    casino games
    casino games
    online slots
    online casino

  10. 2018年1月13日23:17 | #10

    Definitely imagine that which you stated. Your favorite reason appeared to be at
    the internet the easiest thing to have in mind of. I say to you, I certainly get irked
    while other folks consider issues that they plainly do not recognise about.
    You managed to hit the nail upon the top as well as defined out the entire thing without having
    side-effects , folks could take a signal. Will probably be again to get more.
    Thanks

  11. 2018年1月14日07:45 | #11

    There are, after aⅼl, sօme ⅾeѕtructiνe factors to freelancing.
    One necessarу point is that in the event you work as a contract paraⅼegal youll nnot
    be eligibⅼe for the sοrtѕ of benefits that yyou would havee in workiing for a
    regulationn fifm or a persѕonal attorney. If you happen to гeally feewl that such “perks” as basic medical
    healtһ іnsurance and other such benefits are essential, freelancing will not
    give you theese benefits.

  12. 2018年1月19日00:22 | #12

    Many thanks for this well-arranged write-up about %BT%!
    This is exactly I was trying to find. Having
    great composing skillsets is great but not everyone is able to be a good author.
    So what to do in the case if you need to write a lot but you are bad
    at it? On the other hand, I’ve identified a solution to this
    trouble. On this website essay writing service reviews uk you
    will find a lot of evaluations of authoring companies
    and opt for the most reputable one.

  13. 2018年1月19日04:45 | #13

    Many thanks pertaining to talking about Eric’s linux and
    nginx! > memcached +mysql+php 测试例子! That has become all sorts of things I have been previously searching for online
    for ages! Might you distribute some more and also?

评论分页
1 ... 3 4 5 152
订阅评论
0+4= (必填)