wp_cache_set() or wp_cache_add()

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.

Leave a Comment