Php+redis instance "Two" page cache new gameplay

Source: Internet
Author: User

Today is to recognize the blog park in the review team more insider, Haha, put a picture to play under.

It's killing the baby.

Go to the theme!

Today does not write what functional, change the taste of the page-level cache, what should be done.

I believe there are many small partners to check Baidu, and even Google, read a lot of articles, I did so before, but found that the above written really unintelligible.

There are two points to see where they are not:

    • First, there are many kinds of online writing, as long as you a little bit of Redis Foundation, is to understand the use of Redis five data structures, you will find very obvious error, the connection is wrong.
    • Second, the cache time, you will find a lot of them write the cache, but there is no cache expiration time, this is fatal, no cache is not expired, just the length of time to cache. You can give 10 seconds, or you can give 10 months. Depends on your specific business. It will teach you how to judge the length of time.

Above is to judge the online some write page cache article good basis, everyone's usage always a bit different, younger brother also dare not jump to the conclusion, for reference only.

Prerequisite Analysis!

or directly:

The above figure, I divided into three levels to do page caching, in fact, not necessarily three levels are implemented, if you do a page-level, the initial project is enough, as the interface level, the basic can solve a lot of throughput.

For the above three levels, I used the same method to do it.

Knowledge Preparation:

    • string REDIS data structure (I've never been able to find a better alternative here, string is mostly handy for me to manipulate its expiration time )
    • The expire () function sets the life time of the key.
    • Exists () function to determine whether a key exists. (There are many ways to determine the method, TTL () gets its time to live, get () function, to determine whether there is data (but the Get judgment is best not)).

All right, the crap is over, just on the code!

1<?PHP2 classTest3 {4      Public $redis=NULL;5 6      Public function__construct ()7     {8         $this->redis =NewRedis ();9         $this->redis->connect (' 127.0.0.1 ', 6379);Ten         $this->redis->auth (' '); One     } A     //Here's an example model, just put it in your own way . - //Take the article as an example. - //page level, interface level can be used the      Public functionGetData () -     { -         //determine if the cached key is still present -         if(!$this->redis->exists ("cache:".)$commentid)) +         { -             //cache does not exist + //The Get_mysql_data () function below is just an example of how to get data from MySQL as per your own situation A             $data=$this-Get_mysql_data (); at             $json= Json_encode ($data,json_unescaped_unicode); -             //Deposit to Redis -             $this->redis->set ("cache:".)$commentid,$json); -             //set expiration time 5 minutes -             $this->redis->expire ("cache:".)$commentid, 60*60*5); -         } in  -         $json=$this->redis->get ("cache:".)$commentid); to         $data= Json_decode ($json,true); +         return $data; -     } the}

There are a few tips for the above code:

    • Above is just an example, see your own specific use to decide
    • Where MySQL data gets to see your own MySQL using mysqli or PDO and so on method obtained, where you replace.
    • Expiry time where are you going to say
    • See 30.31 rows, where do you get one more data from Redis? Why not directly return to MySQL $data not on the line, in fact, it is possible, just to prevent redis not to cache the data in, and do a step, there is a step error, I did not write, I add it.

Here's how to set the Expiration time:

This is not a unified statement, the first according to your project stage, the second is the amount of user traffic, and the third is the degree of hot and cold data to judge.

Project stage: In fact, and user traffic has a lot of association, for example, assuming that your project in the early stage, but the user has exploded, at this time, set the cache can be a thief short, even 2 seconds, can help you solve a lot of throughput.

Hot and Cold degree: Cold refers to the user visits less, hot is very obvious, hot there is a Special Representative, the update frequency of the data, the update frequency of high should be about a short time.

Special operation: To update some data, you can consider the corresponding cache key time to expire, so that the user sees the data is up-to-date, and the cache function is still there, set the lifetime expires, very simple, expire set to 0 can be

Another page cache:

A different data cache is described below:

Suddenly do not want to write, because I feel that the above practice is covered, is considered the above interface-level approach. I'll simply say it.

Here is still the article as an example, our forum is certainly the most of the article, the heat of the article must be sorted. The amount of access to the hot article is relatively large, then we will cache the assumption that the number of posts within the 10,000, for the article beyond this can not be cached.

Here by the way, the next article is to write about the leaderboard, the daily list, the weekly list, the annual list of how to do.

Welcome to the Exchange!

Php+redis instance "Two" page cache new gameplay

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.