Expiration policy¶
Otter supports two types of expiration policies
- Expiration policy where all cache entries have the same ttl.
- Expiration policy, when ttl is not known in advance and is specified for each entry separately.
Tip
Try to use an expiration policy with constant ttl whenever possible, as this allows otter to use more efficient algorithms and helps you write cleaner code.
WithTTL¶
The WithTTL
function in the builder specifies the ttl to be used for all entries in the cache. In fact, calling Set(key, value)
is equivalent to calling SetWithTTL(key, value, ttl)
in other libraries.
Example
This code creates a cache with constant ttl = time.Hour
WithVariableTTL¶
The WithVariableTTL
function in the builder tells otter that you want to specify ttl for each entry separately.
Example
This code creates a cache with a variable ttl and several entries with unique ttl.