首页 > 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年10月21日03:02 | #1

    Aticle – website here

    Norm attendance as well as video gaming quantity
    within Southern area Korean casino houses

    - 예스카지노

    Why users still make use of to read news papers when in this technological globe everything is accessible on web?
    https://issuu.com/mia2mynotes/docs/gaming-online_-on-being-legal-or-un

  2. 2018年10月21日07:05 | #2

    Source – 더킹카지노 [슈퍼카지노]

    Southern region Korea arranged to spread out luxurious gambling house

    - see this page

    Wonderful, what a blog it is! This web site presents useful data
    to us, keep it up. http://www.menlosoftware.net/UserProfile/tabid/43/UserID/1510552/Default.aspx

  3. 2018年11月5日17:09 | #3

    https://is.gd/7pfTJj- – Keep looking for more such
    useful information; stay informed, stay healthy. You’re
    guaranteed to find something perfect for you knowning that you’ll
    enjoy. When we travel as a family we always have
    a kitchenette in the accommodation to ensure that we can easily make most our meals with food we realize
    and after that have one meal out a day.

  4. 2018年11月6日21:37 | #4

    Hey I know this is off topic but I was wondering if you knew of any
    widgets I could add to my blog that automatically
    tweet my newest twitter updates. I’ve been looking for a plug-in like this for quite some time and was hoping maybe you would
    have some experience with something like this.
    Please let me know if you run into anything. I truly enjoy reading
    your blog and I look forward to your new updates.

  5. 2018年11月7日08:21 | #5

    Great post. I was checking constantly this blog
    and I’m impressed! Extremely helpful information particularly the last
    part :) I care for such info much. I was seeking this
    particular information for a long time. Thank you and
    good luck.

  6. 2018年11月9日11:27 | #6

    At this time it looks like Drupal is the best blogging
    platform available right now. (from what I’ve read) Is that what you’re using on your
    blog?

  7. 2018年11月9日19:44 | #7

    You ought to take part in a contest for one of the finest blogs on the net.
    I most certainly will highly recommend this blog!

  8. 2018年11月10日13:29 | #8

    Way cool! Some very valid points! I appreciate you writing this post
    and the rest of the site is also really good.

  9. 2018年11月12日00:51 | #9

    Good post! We will be linking to this particularly great article on our website.
    Keep up the great writing.

  10. 2018年11月13日00:01 | #10

    Hello i am kavin, its my first time to commenting anywhere,
    when i read this piece of writing i thought i could also make comment due to
    this brilliant paragraph.

  11. 2018年11月15日04:22 | #11

    Undeniably imagine that that you said. Your favourite reason appeared to be
    at the web the easiest factor to have in mind of. I say to you, I definitely get annoyed while folks think about issues that they just don’t know
    about. You managed to hit the nail upon the top and also outlined out the whole thing without having side effect , other people could take a signal.
    Will likely be again to get more. Thanks

  12. 2018年11月15日21:59 | #12
  13. 2018年11月16日03:23 | #13

    [url=https://cialisq.com/]cialis[/url]

  14. 2018年11月17日09:16 | #14

    Sling tv coupons and promo codes for november 2018
    Very descriptive post, I loved that a lot. Will there be a part
    2? Sling tv coupons and promo codes for november 2018

  15. 2018年11月18日01:32 | #15

    I think everything said made a ton of sense. But, what about this?
    what if you wrote a catchier title? I am not suggesting your information is not good, however what
    if you added something that makes people desire more? I mean Eric's linux and nginx!
    » memcached +mysql+php 测试例子 is a little boring.
    You might look at Yahoo’s front page and note how they write news titles to get viewers
    interested. You might try adding a video or a pic or two to grab people excited about everything’ve written. In my opinion, it would
    bring your blog a little bit more interesting.

  16. 2018年11月19日02:35 | #16

    Yes! Finally something about What Equipment is Needed for Sling TV.

  17. 2018年11月20日01:34 | #17

    Do you have a spam problem on this blog; I also am a
    blogger, and I was wondering your situation; many of us have developed some nice practices and we are looking to swap methods with others, why not shoot me an email if
    interested.

  18. 2018年11月20日04:01 | #18

    What i do not understood is if truth be told how you’re now not
    actually much more well-favored than you might be right now.
    You are very intelligent. You know thus significantly in the case of this topic, produced
    me in my view believe it from numerous various angles.
    Its like women and men don’t seem to be involved until it is something to accomplish with Lady gaga!

    Your own stuffs nice. All the time care for it up!

  19. 2018年11月20日21:15 | #19

    Great post however , I was wondering if you could write a litte more on this
    topic? I’d be very grateful if you could elaborate a little
    bit further. Kudos!

  20. 2018年11月21日22:43 | #20

    Excellent blog! Do you have any suggestions for aspiring writers?
    I’m planning to start my own website soon but I’m a little lost on everything.
    Would you advise starting with a free platform like WordPress or
    go for a paid option? There are so many options out there that I’m
    completely overwhelmed .. Any suggestions? Many thanks!

  21. 2018年11月22日18:51 | #21

    Greetings! Very helpful advice in this particular article!
    It’s the little changes that will make the greatest changes.
    Many thanks for sharing!

  22. 2018年12月1日13:49 | #22
  23. 2018年12月3日16:15 | #23

    https://carloan2018.com/ – capital one car loan

  24. 2018年12月6日21:39 | #24

    Greate pieces. Keep writing such kind of info on your page.
    Im really impressed by your blog.
    Hi there, You’ve performed a fantastic job. I’ll definitely digg it and for my part recommend
    to my friends. I am sure they’ll be benefited from this web site.

  25. 2018年12月7日01:42 | #25

    An impressive share! I’ve just forwarded this onto a colleague who has
    been conducting a little research on this. And he
    in fact bought me breakfast because I found it for
    him… lol. So let me reword this…. Thank YOU for the meal!!

    But yeah, thanks for spending the time to talk about this
    issue here on your site.

  26. 2018年12月7日04:42 | #26

    View – 더킹카지노 [슈퍼카지노]

    To the south Korea: A different international gambling establishment
    hotspot

    - article

    Very great post. I simply stumbled upon your blog and wished
    to say that I’ve really loved surfing around your weblog posts.
    After all I will be subscribing for your rss feed
    and I am hoping you write once more soon! http://www.rfw1.com/home.php?mod=space&uid=483775&do=profile&from=space

  27. 2018年12月7日14:13 | #27
  28. 2018年12月7日16:25 | #28
评论分页
1 ... 5 6 7 152
订阅评论
2+7= (必填)