The new cache API introduces a new way to “remove” a cache item. In addition to the existing delete methods where cache entries are permanently deleted, cache entries can now also be invalidated.
- Delete methods
- delete(), deleteMultiple(), deleteTags(), deleteAll(), deleteExpired()
- Invalidate methods
- invalidate(), invalidateMultiple(), invalidateTags(), invalidateAll()
Invalidation is like a “soft delete” where items are not deleted but only marked as invalid, meaning “not fresh” or “not fresh enough”. An item automatically becomes invalid when the expire time specified in the set() call is reached, or it may explicitly be marked as invalid using the functions listed above. The invalid items are not returned by a normal get() call, so in most ways they act as they have been deleted. However, by passing TRUE as the second argument for get(), it is possible to fetch the invalid value.
// Explicit invalidation
cache()->get('foo'); // Returns a cache item
cache()->get('foo'); // Returns FALSE
cache()->get('foo', TRUE); // Returns a cache item
cache()->get('bar', TRUE); // Returns FALSE
// Expired items
cache()->set('bar', 123, REQUEST_TIME + 60);
cache()->get('bar'); // Returns a cache item