9 thoughts on “WordPress caching, Part 2 Leave a comment

  1. Pingback: Best On WordPress From The Past Week N.8 ยป wpCanyon
  2. That’s pretty awesome! Apparently I’ve been living underground to miss this!

    If a caching plugin that caches the DB queries is running, that’d effectively have the same result though, wouldn’t it? Seems a bit of a cop out, but seems to be the easier way(!).

    1. Hey Alex, good question! ๐Ÿ™‚

      The two things are actually quite different. DB caching plugins store the results of DB queries, so if you execute the same query twice, it will be loaded from the cache rather than executed again. What this persistant cache does is store data about the results of the query. For example if you do a query to get a posts contents, and then do a query to get recent posts (which includes this post) and then do a query to get popular posts (which includes this post) then the data you need to display about that single post will already be stored in memory. Obviously it’s not quite that straight forward but that’s the easiest way I can think of to explain it ๐Ÿ™‚

  3. Pingback: Speeding Up Your WordPress Website: 11 Ways to Improve Your Load Time | WordPress News at WPMU.org
    1. Thanks for the comment. Could you explain a bit further how this is wrong?

      The wp_cache_add function adds data to the cache (which is what I am doing). Then I use get_permalink – and this did an additional query if the data wasn’t cached previously. Making use of the wp_cache_add function solved this issue. Perhaps I explained it poorly, but the functionality worked great for me, and I reduced my queries a lot.

      Of course this article is 2 years old, and I do everything differently now anyway. I now use WP_Query for just about everything, and that takes care of caching for me.

      1. First of all you need to cache whole object, not part of it. You adding to cache entries in foreach loop, which is stupid. Let’s say object is of 200 entries, so you will add 200 entries to “cache”.
        I would do it this way: before loop: wp_cache_add(‘some_uniq_id’, $posts, ‘posts’);
        Ofcourse at first we HAVE TO CHECK is there anything in cache, what you did not do. So what’s the purpose of cache if you don’t use it?

        so before executing query to get posts we have to check if we have cached copy, so: $posts = wp_cache_get( ”some_uniq_id’, ‘posts’ ). And then we check if(false === $posts) { ONLY then we make query and add to cache, after we checked that there is no cached version }

        Anyway this stuff doesn’t work anymore, and you have to use Transient.

      2. I think you’re misunderstanding what it’s doing. This is caching it in memory for this request only. If I were to use a unique and not the standard WordPress key then get_permalink won’t make use of the data stored and so will continue making lots of unnecessary database queries.

        It’s not suitable for a transient at all since it’s just caching the post information for that one request and isn’t required for page views between different users.

Leave a Reply

Your email address will not be published.