cnnrrss / go-cache

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Go Report Card Scc Count Badge

go-cache

Package go-cache is an in-memory key-value store where keys expire after a period of time.

The Cache type provides Get and Set methods. The cache is goroutine safe.

Values are stored as type interface{}. You will need to use a type assertion after retrieval to use the typed value.

Example

package main

var (
	appCache *cache.Cache
	once         sync.Once
)

func setup() {
	once.Do(func() {
		appCache = cache.New(time.Hour * 1)
	})
}

func main() {
    setup()
    
    appCache.Set("key", 1)
    v, found := appCache.Get("key")
    if found && v.(int) == 1 {
        fmt.Println("success")
    }
}

About

License:MIT License


Languages

Language:Go 100.0%