cache

package module
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Feb 8, 2020 License: MIT Imports: 4 Imported by: 0

README

cache

go cache with fetch hook

usage

use string key

build your own Key

# 1. impl the keyer
func NewExampleKey(field1 string, field2 int64) ExampleKey {
	return ExampleKey{
		Field1: field1,
		Field2: field2,
	}
}

func (k ExampleKey) Key() string {
	return fmt.Sprintf("%s:%d", k.Field1, k.Field2)
}



# 2. impl the reterive func
func RetrieveExample(key ExampleKey) (interface{}, error) {
    f1 := key.Field1
    f2 := key.Field2

	username, err := GetFromDatabase(f1, f2)
	if err != nil {
		return nil, err
	}
	return username, nil
}



# 3. new a cache
var exampleCache cache.CacheFactory
exampleCache = cache.NewCache(
	"example",
	false,
    RetrieveExample,
	5*time.Minute,
	6*time.Minute)

# 4. use it
k := ExampleKey{
    f1: "aaaa",
    f2: 1,
}
exmaplCache.Get(k)
exmaplCache.GetString(k)
exmapleCache.Set(k, xxx)

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewCache

func NewCache(name string, disabled bool, retrieveFunc types.RetrieveFunc,
	expiration time.Duration, cleanupInterval time.Duration) types.Cache

func NewRedisCache

func NewRedisCache(name string, disabled bool, retrieveFunc types.RetrieveFunc,
	cli *redis.Client, expiration time.Duration) types.Cache

Types

This section is empty.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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