cachemap

package module
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 31, 2021 License: MIT Imports: 4 Imported by: 0

README

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' cache_random_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' cache_random_true

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type CacheJob

type CacheJob struct {
	// contains filtered or unexported fields
}

func (CacheJob) Run

func (j CacheJob) Run()

type CacheMap

type CacheMap struct {
	// contains filtered or unexported fields
}

func CreateCacheMap

func CreateCacheMap(duration time.Duration, randomizedDuration bool, cronExprForScheduler string) *CacheMap

func (*CacheMap) Load

func (c *CacheMap) Load(key interface{}) (element interface{}, result RESULT)

func (*CacheMap) Store

func (c *CacheMap) Store(key interface{}, value interface{})

type CacheScheduler

type CacheScheduler struct {
	// contains filtered or unexported fields
}

type ElementForCacheMap

type ElementForCacheMap struct {
	// contains filtered or unexported fields
}

struct to store diningcode result in map

type RESULT

type RESULT int
const (
	VALID RESULT = iota
	EXPIRED
	NOT_FOUND
)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL
⌂ 换网址