memcached How to use

Source: Internet
Author: User
How is memcached used?
I am doing an online answer system, in the same interface to refresh the topic, each click on the next topic refresh a question,
Procedure: 1 Take the question from the database (after taking the $db->close ()), 2 points next question. Repeat 1, 22 steps until the question is answered.
In this case, the equivalent of every question to be connected to a database, after the classmate suggested that the data can be stored in the memcached
This allows you to reduce the number of connections to the database.
What I want to ask is where to put the memcached class in order to achieve the effect?
Like taking the first question.
First I want to connect to the database
And take out the problem.
while ($row =mysql_fetch_array ($result) {
$question []= $row [question];
All the questions have been taken out of the}//so far.
$mem = new Memcache; I put the Memcache class behind the database.
$mem->connect ("127.0.0.1", 11211);
$mem->set ("key", $question);
$question = $mem->get ("key");
$db->close ();
And then the question came.
1. How to ensure that only the first time the problem is connected to the database and all the data are assigned to the Memcache class $question like I wrote the value to the memcache, but when you click on the next question, still the equivalent of connecting to the database.
2. After reading the first question, I put the database connection class commented out, I think is because the first question because the problem has been assigned to the cache, so there is no need to connect the database. But when the next question is not read, then I read the first question when the value of the cache assigned to where? Does page refresh cause the values in the cache to be lost?
I think it's definitely me. Using the Memcached method is not correct, the class should be placed in order to reduce the number of connections to the database, I hope the use of people give me some ideas

------Solution--------------------
Your code above is equal to read the database every time, and then write the value to the cache, should be read before the database if (false = = $mem->get ("key"), so that when the key exists, it will not be connected to the database reset set
------Solution--------------------
This order
PHP code
 $mem = new Memcache; $mem->connect ("127.0.0.1",  11211), if (! $mem->get ("key")) {//Database connection query code while ($row =mysql_fetch_array ($result) {$question []= $row [question]; All the questions were taken out of the}//so far $mem->set ("key", $question);} $question = $mem->get ("key"); 
  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.