Skip to content

malt03/go-cache

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

go-cache

go-cache is a thread-safe in-memory cache library that caches a single value in a single instance.
Unlike key-value stores, it can be implemented without worrying about duplicate keys.

Installation

go get github.com/malt03/go-cache

Usage

// Create a cache of 1 hour for ttl and 5 minutes for jitter.
var entitiesCache = cache.New(cache.NewConfig(time.Hour, time.Minute*5))

func GetValues() ([]*Entity, error) {
	entities, err := entitiesCache.Get(func() (interface{}, error) {
		var entities []*Entity
		
		// fetch entities
		
		return entities, nil
	})
	if err != nil {
		return nil, err
	}
	return entities.([]*Entity), nil
}

Other functions

NoExpiration

var entitiesCache = cache.New(cache.NewConfig(cache.NoExpiration, 0))

Invalidate

entitiesCache.Invalidate()

About

No description or website provided.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages