E
E
Eugene2014-02-12 20:16:28
PHP
Eugene, 2014-02-12 20:16:28

What are the caching options in PHP?

Good evening.
I ask you to suggest the correct solution in caching.
I have a page that does a lot of database queries before getting to the user. For example, comments are loaded in it with a ladder, information about pictures, news, events, similar posts, etc.
Accordingly, there is an idea to cache all this at once into the generated html, and then just give it to all users.
But here immediately there is a snag. Suppose, next to the rating, you need to display the rating given by a specific user. How can I do it? I have two ideas.
The first is to add some flag in the right place in the generated html (for example, %%USER_RATING%%), and then insert specific information into this place.
The second is a few pieces of the cache, which we will then collect. For example, your own cache for comments, your own for news, etc.
But I like the second option much less, since at first I will have to use the file cache.
Perhaps there are other options?
PS What is the most efficient way to replace %%USER_RATING%% in a large piece of text with something of your own?

Answer the question

In order to leave comments, you need to log in

4 answer(s)
S
Sergey Sokolov, 2014-02-12
@Qwofer

@pygame wrote everything correctly in the comments. I will summarize:
1. remove apache. nginx can do everything that is required on fewer resources. You will need its SSI module, which is already enabled by default;
2. Insertion using SSI;
3. cache these very inserts as best you can in files, incl. you can also use the nginx cache when accessing virtuals <!--# include virtual="/ssi/news.php" -->;- designate location /ssi/ { ... } as upstream with a certain caching time.
4. Finally, cache entire pages so that several reloads within 1 minute will definitely not execute a single request to the database.
PS question about PHP, I'll add about APC - if installed and enough memory, this is the same key-value storage: see apc_store() , apc_fetch() .

F
FanatPHP, 2014-02-12
@FanatPHP

I'll reveal a secret.
EVERY site on the internet displays "ladder comments, information about pictures, news, events, related posts, etc."
With a properly organized database, it will not be difficult to query and show all this.
As far as I can see from the wording of the question, there are no performance problems right now, but there are vague abstract fears. And any technology should be mastered only on the basis of real need, and not fantasies.

R
Rowdy Ro, 2014-02-12
@rowdyro

In fact, it’s more correct to make several caches, give them in the front to be collected by template engines.
Because the nature of the entities that you cache is different and the time and relevance are also different.
If you don't feel like rewriting the front and don't want to use key=>value stores, keep cache files in tmpfs, do str_replace when requested.
But this is actually a bicycle from which everyone has gone to key=>value storages, to which you, in principle, do not want to use.

G
GHua, 2014-02-12
@GHua

And why under this business not to use ESI? Varnish is acceptable. You can cache the general comment feed already with a rating (public), and remember who rated where for each user separately (private). When you update the feed, the ratings update the corresponding caches.
It seems to be pretty much what you're talking about.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question