Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I have an object that saves its data in postmeta. When there is a post_id, the object class will query data by using get_post_cutom(); After get data, it starts to do its jobs, the end result is a graphic presentation based on the data.

I understand that wordpress handles postmeta caching, but I would like to know:

Should I cache the end result of the graphic presentation?

share|improve this question
    
Please add more details about that "graphic presentation". Currently it's too vague to answer. –  kaiser Sep 29 '12 at 22:58

2 Answers 2

up vote 2 down vote accepted
+50

If the generation of the graphic presentation requires a lot of CPU time or memory, then yes, you could cache your graphic presentation (HTML I guess, right?). Perhaps in the database/memory, as a transient.

Caching post meta data (that you use to generate the graphic) is not necessary, because WP already handles this. If you do it too, then the same data will get stored twice in memory. But keep in mind that by default WP's cache is not persistent, so the cache gets flushed after script finishes execution (on each page load). There are plugins out there that implement persistent object caching, if you need it.


Summary:

  • you don't cache results from post queries made with wrapper functions like get_posts(), get_post_meta() etc ; WP does already that internally.
  • you cache your own data, like your graphic, when the operation necessary to build it is more expensive than the cache query that would run to get that graphic; but:

    • if the operation is less expensive than a typical database select query from WP's options table, then you use WP's object cache API

    • if it's more expensive than such queries, you use the Transient API, which will store your data in the database, but only if a memory caching interface is not available. Otherwise it will make use of the object cache.

Regardless of all the above, if you can use a persistent object caching plugin (example), do it! Because accessing the memory will always be faster than querying a database, which is typically a file on a disk, and disks are slow.

share|improve this answer
    
Thanks for clearing my doubts! The class is for loading/creating, there are several other php classes providing service to this class. yes, the end result is HTML. To use object caching plugins, do I need to use a template class to load stuffs instead of add an action of $presentation=new Presentation_Class($post_id) in other post's template? –  Jenny Sep 30 '12 at 8:59
    
These plugins extend WP's object cache, so no special treatment is needed for them. I clarified my answer a little, but as Kaiser said you need to provide more info in order to get better answers, without so many ifs :) –  onetrickpony Sep 30 '12 at 17:12

The simple answer is:

If it costs considerably more to grab the cached data from the DB than it does to generate it from scratch, cache it.

Is the creation of your 'graphics presentation' significantly slowing down the page? If so cache it. If not don't.

Is it duplication? No What you started with from post meta and what you get in the end aren't the same.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.