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.

Where should a plugin cache its re-usable results?

I have read the code of several well-used Wordpress plugins, and it seems that the answer is not consistent. I have written several plugins myself, and I store things in a cache directory. But I just picked the location of the directory out of a hat, and my code creates the cache directory if it doesn't exist. This seems arbitrary.

I know there are plugins that do caching. Suppose I don't want to use a plugin. I find that relying on the filesystem to cache results is easy for maintenance, diagnostics, and performance.

Is there a good, more-or-less standard approach, or a blessed approach, to choosing a filesystem location for a cache?

share|improve this question
    
It depends on the type/nature of the data being cached. Can you elaborate on the specifics of the data? –  Chip Bennett Jun 15 '12 at 23:52
    
I'm interested in general answers. In the plugins I'm writing, the cached items are generally results of parameterized REST queries on external services. They are XML, or JSON, or something else. –  Cheeso Jun 15 '12 at 23:59
    
But different types of data can/should be stored differently: wp_options, transients, wp_content, the object cache, etc. –  Chip Bennett Jun 16 '12 at 0:02
3  
I think a problem is that general type questions don't get much traction compared to specific ones. My advice would be to make the questions very specific to one particular case. For example: "best practices for caching/storing an external http request from an XML file." –  Wyck Jun 16 '12 at 1:36
1  
Since I'm not sure whether your question is a general "where do I cache?" question vs. your final sentence that is a question about best practices for using the filesystem to cache instead, I'll just add this slide deck link as a comment: tollmanz.github.com/caching-for-coders –  helenhousandi Jun 16 '12 at 4:52

1 Answer 1

up vote 2 down vote accepted

I find that relying on the filesystem to cache results is easy for maintenance, diagnostics, and performance.

Please note that this might be true in some (maybe even most) circumstances, but not all of them. If your code is meant for anything beyond personal usage you don't know with which file system and hardware will it be used and how will it perform there.

WP is engineered to use database for storage of textual data, that is more typical and logical first choice than file system.

Is there a good, more-or-less standard approach, or a blessed approach, to choosing a filesystem location for a cache?

wp-content folder is meant as a place for files uploaded or generated during operation. Plugins that use file cache should create folder for it in there. Note that WP is flexible about most of folder structure and it can be easily renamed/relocated from defaults - use appropriate functions to determine that rather than hardcode default path.

share|improve this answer
    
Great, thank you. –  Cheeso Jun 16 '12 at 16:27

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.