5

That's the question!

There's not much explanation on wp_cache_set() an even less for wp_cache_add().

What should I use in which circumstances?

1 Answer 1

7

According with WP Object API documentation both functions accepts these arguments:

  • $key: the key to indicate the value.
  • $data: the value you want to store.
  • $group: (optional) this is a way of grouping data within the cache. Allows you to use the same key across different groups.
  • $expire: (optional) this defines how many seconds to keep the cache for. Only applicable to some functions. Defaults to 0 (as long as possible).

The difference is:

  • wp_cache_add(): if the cache key doesn't exist, it add the cache data. If the cache key already exists, the function returns false and does nothing with the cached data.
  • wp_cache_set(): same as above but if the cache key already exists, the cache data is overwritten.
1
  • 2
    and why didn't I RTFM? Thank you!
    – Xaver
    Oct 21, 2015 at 9:23

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.