1

I want to cache on demand some expensive calculations and store them in a plain file that I can fetch later on.

Is there any built-in function in WordPress Core Utilities that may handle adding, fetching, deleting cache?

Something that is similar to the following snippet:

if ($foo_string = $storage->fetch('foo')) {
    $foo = unserialize($fooString);
} else {
    // Do the work to calculate $foo.
    $storage->save('foo', serialize($foo));
}
4
  • 1
    Third-party lugins are off topic, so I removed that part from your question. My suggestion would be, to keep it short, make use of the Transients API. Oct 28, 2017 at 11:06
  • Is Transients API suited for caching many big entries like text files?
    – AFA Med
    Oct 28, 2017 at 11:13
  • Well it's DB based. So it's not text file based. But it's definitely possible to store big entries. Personally I just wouldn't - or at least never had a use case where it made sense - use text files. Apart from that, as I said, it's a suggestion. Good luck! Oct 28, 2017 at 11:44
  • If you use transients and no cache driver is enabled (e.g Redis), you'd end up overloading the options table, or overloading your cache server if any. FileSystem is faster than the DB, but if you're expecting huge files then try to store in grouped directories instead of just 1, like wp media does with wp-content/uploads files.
    – Ismail
    Oct 28, 2017 at 11:55

1 Answer 1

1

There is no explicit mechanism for disk based caching. If you are writing a plugin or theme you intend to distribute you should be careful with allocating a folder for it, probably better to have some kind of an option for a user to configure that location. If this is something for a specific site, at least this should not be a problem, but you should probably use a directory which is not web public.

Of course, you should consider do you actually need to create such a thing. Unless you need to store a huge amount of data per "item" in the cache you are more likely to get a good enough results using an object cache plugin. You can store data directly with the wordpress caching api (wp_cache_get and family functions), or indirectly with transients, with the big advantage of not having to design something new yourself ;)

2
  • The problem is that wp_cache_get is not a Persistent Caching system. The Transients API is not very suitable for my case, however I will give it a try and see how does it scale.
    – AFA Med
    Oct 28, 2017 at 18:41
  • wp_cache_get is persitant if you have object caching Oct 29, 2017 at 2:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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