Mysql-php database cache design

Source: Internet
Author: User
Tags php database
For example, to display the total number of views of all subitems under a category, but it is impossible for a user to access the page once, the statistics are too resource-consuming. please give us some advice on how to implement the statistics for the next time. for example, to display the total number of views of all sub-items under the category, but it is not possible for users to access the category once. the statistics are too resource-consuming. please give me some advice on how to implement the statistics again after a period of time.

Reply content:

For example, to display the total number of views of all subitems under a category, but it is not possible for users to access the page once, so the statistics are too resource-consuming. please give us some advice on how to implement the statistics by yourself.

$ Redis = new redis (); $ pv = "select pv from views where id = {$ id}"; // pv value for the first time $ num = 100; // update database $ incrPv = $ redis-> incr ('PV :'. $ id); if ($ incrPv % $ num = 0) {$ SQL = "update views set pv = pv + {$ num} where id = {$ id}";} else if ($ incrPv = 1) {$ incrPv = $ pv + 1; $ redis-> set ('PV :'. $ id, $ incrPv );}

Write a scheduled script to count the old data
Redis can be used for caching statistics on browsing volumes

Real-time statistics are written to redis or other nosql databases. crontab regularly calculates and clears nosql
Online PHP code execution

It's easy to set cache when you set to display the page views of subitems under the corresponding category.

For example:

// This method is used to measure the synthesis of the page views of all child classes under a parent class. '$ time' is the set cache time function getCategoryNum ($ categoryId, $ time = 3600) {// calculate the page views of all child classes based on the parent class id} // This is the method used to collect statistics on your database or cache during each access. You do not need to set cache for this method, because the function getViewsNum () {// user access Volume + 1 operation} is required each time}

Through the abovegetCategoryNum()The method sets the cache Time. during the cache time, the database is not requested. only when the time is reached will the database be requested to obtain the latest statistics, this is the time you have set.

How to Set cache for your project! I think it is a waste of time to do scheduled tasks. Of course, I am not sure about your specific business needs. everything is subject to your business needs. Hope to help you

If you do not want to use other caching technologies, you can use mysql for caching. the cache structure is roughly Key, Value, and the last update time. if this time is exceeded, the cache will be updated, note that when writing data, you must check whether the consistency of the written version is verified.

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.