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
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.