Caching debate / forum posts in PHP

Just looking for advice. On one of our web pages we have a forum / forum. Each time a user requests a discussion page, he / she receives a list of all topics (and their number of answers, etc.).

Too, when the user requests a specific topic / thread, all responses to the stream will be shown to the user with the user name, user image, age, number of public forum posts from the response poster.

All content is currently retrieved using a MySQL query every time a page is accessed. But this, however, begins to suffer slowly (especially with large flows, +3000 responses).

I would like to cache discussion notes somehow in order to speed up this process. However, the problem is that if I cache the entries myself, the number of messages, etc. (Which is dynamic, of course) will not always be relevant.

Is there any smart way to cache pages / delete them when such things get updated? :)

Thanks in advance, Fisher

+4
source share
2 answers

You must create a tag or name for the cache based on the data.

For example, for a Jake Post message, you can create an md5 name, this will give you the tag 49fec15add24931728652baacc08b8ee.

Now cache the contents and everything related to this post against the 49fec15add24931728652baacc08b8ee tag. When a message is updated or a comment is added, go to the cache and delete everything associated with the 49fec15add24931728652baacc08b8ee file.

Now there is no cache, and it will be rebuilt when the next visitors start a new message.

You can break this down by listing multiple tags per post. For example, you can have a tag for comments and answers, when a comment is added, remove the comment tag, but not the response tag. This reduces the work that the server has to do when restoring the cache, because now there are no comments.

There are many libraries and frameworks that can help you with this.

Jake

EDIT

I would use files to store data, or rather HTML output on the page. Then you can do something like:

if(file_exists($tag)) { // Load the contents of the cache file here and output it } else { // Do complex database look up and cache the file for later } 

Remember that frameworks like Zend have such embedded materials. I would seriously think about using a framework.

+2
source

Interesting topic!

The first thing I look at is optimizing your database - even if you have to spend money on upgrading equipment, it will be much easier and cheaper than introducing into the cache - fewer moving parts, fewer things that can go wrong ...

If you cannot get more performance out of your database, the next thing I would consider is de-normalizing the data. For example, save the "reply_count" column rather than counting responses for each topic. This is ugly, but introduces fewer possibilities for things to go wrong - with little luck, you can localize all the logic at your data access level.

The next option I would consider is page caching. For example, just caching the “talk page” for 30 seconds should significantly reduce the load on your database, if you have reasonable levels of traffic, and even if everything goes wrong, because you are caching the whole page, it will sort itself into the next the time the page becomes obsolete. In most cases, caching the entire page in order is not the end of the world if a new record appeared in the last 30 seconds and you do not see it on your page.

If you really need to provide more “relevant” content on the page, you can enter caching at the database access level. In the past, I created a database access level that cached the results of SQL queries based on hard logic about how long to cache the results. In our case, we created a function to call the database, which allowed you to specify the request (for example, to receive messages for the user), an array of parameters (for example, username, date-time) and cache duration. The database access function will cache the results for the duration of the cache based on the query and parameters; if the cache expired, it will update the cache. This scheme was fairly error-resistant - as an end user, you rarely notice weirdness due to caching, and because we kept the cache period pretty short, it all sorted out very quickly.

Creating your page by caching pieces of content is possible, but very quickly becomes terribly difficult. It is very easy to create a page that makes no sense to the end user due to different caching policies - “unread messages” do not summarize the number of messages broken down due to different caching policies between “summary” and “summary” messages, detail. ”

0
source

All Articles