How to cache files in php correctly

I have a page with a post and a few comments using PHP ob_start() . I can successfully cache it.

Next to each comment, I have a username and its number of current posts and reputations. Now I save the cache of the message page until someone adds a new comment, only then I update the cache file.

Now the problem is that the number of posts and the user's reputation will increase as he publishes / comments on other topics, and his post number and reputation will not change in senior posts.

What would be the best practice to solve this problem.

+4
source share
3 answers

If you are in any way connected with the performance of your site, you should switch to APC , because it provides both caching of the operation code and caching facilities as a store of keys / values.

You can store entire blocks of content, arrays, objects that you call:

 // you must supply: // 1. a key you will later use to retrieve your content // 2. the data you wish to cache // 3. how long the cache should remain valid apc_store($key, $data, $ttl); 

As for the search, you just call this call:

 $data = apc_fetch($key); 
+3
source

I hope it turns out to be erroneous, but I don’t think that there is currently an easy way around this, other than limiting the cache duration.

Of course, you could update your respective reputation, etc. through AJAX, but it’s entirely possible that the connections and bandwidth that it consumes will ultimately outweigh the advantage of page caching in the first place.

If one of the main goals of caching is to reduce processing overhead (as opposed to bandwidth consumption), you can, of course, simply smooth out the non-dynamic parts of the page (each post as a static text file or similar - hence reducing the need to re-generate HTML if you use Markdown or BBCode, etc.) and enable them as needed / update them if they are edited.

+3
source

Some of my thoughts:

You can save page caching for a certain period of time, for example, one hour or 15 minutes. This time depends on the number of visitors that you receive on the page, the frequency of changes in details and your personal preferences. Because it really doesn’t matter if the number of user posts is out of date. After this period, delete the cached version (also save resources), and if the page is visited again, it will be overwritten with updated data.

With the clever (re) use of ob_start() you can buffer several parts of the page, for example, part of the mail and part of the comments. Keep these parts separate, and you only need to restore one part instead of a full page. In most cases, the postal part does not change very often.

Keep track of the pages on which the user posted comments (or the page itself, if he created it). After making changes to user data (adding new posts / comments), make these pages obsolete (i.e. delete the cached version). If you have a lot of changes over a short period of time, you can use some kind of background process to re-cache the pages and maintain your web server.

Inserting tokens (unique pieces of text, for example% user: 123, postcount%) of frequently changing details is another possibility. Then save this version in your cache and at the request of the page you can replace the tokens with your data. It can also be combined with other caching methods if the number of page views over a period of time is very large (or at least much higher than the frequency of changes in the part).

+1
source

All Articles