cachemap
support caching for map written in GO
1. How to use
1.1. import library
import cachemap "github.com/KIWOKIM/cachemap"
1.2. define expiration duration of element in cachemap
Duration allows the type of time.Duration and duration should greater than 60 seconds.
If it is less than 60 seconds, the duration will be set as 60 seconds
exmaple :
const DURATION time.Duration = time.Hour * 96 // 4 days
1.3. define schedule time to remove expired element in cachemap
The expression follows cron expression. Based on the schedule expression, cachemap will remove all expired elements throughout the cache.
example :
const SCHEDULE string = "0 0 18 * * *" // run at everyday 18:00 in UTC
1.4. create cachemap
example :
cacheMap := cachemap.CreateCacheMap(DURATION, true, SCHEDULE)
Regarding the variable "randomizedDuration bool" in the middle of parameter, you can choose the option to randomize expiration time if the value is true.
Randomized duration will distribute cache update more evenly.
1.5. store key/value
Expeiration time will be decided when the element is stored in cache. If the "randomizedDuration" is true, duration will be randomized up to 25% of "DURATION".
example :
cacheMap.Store(a_key, a_value)
1.6. load key/value
example :
element, status := cacheMap.Load(somekey)
You can test whether the value is valid or not. "status" will be one of the values below
VALID : found value by the key and it is not expired yet.
EXPIRED : found value by the key but, it was already expired.
NOT_FOUND : can not get the value by the key
const (
VALID RESULT = iota
EXPIRED
NOT_FOUND
)
2. Effect when "randomizedDuration" is true.
2.1. Example of 'false'
As you can see image below, most of cache update is concentrated near expiration time.
It may cause surge or peak resource consuming to handle all updates.
- expiration duration : 96 hours (4days) + randomizedDuration is 'false'

2.2. Example of 'true'
As compared with 'false', cache update is more evenly distributed through time span.
- expiration duration : 96 hours (4days) + randomizedDuration is 'true'
