cache

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Nov 3, 2020 License: MIT Imports: 2 Imported by: 0

README

Cache - A multi level cache

Cache implements multi cache levels:

  • Local cache: By using freecache.
  • Redis cache: By using Redis.
  • Multi level cache: combine of multi caches.
  • id cache: Convention cache by caching id (string) by name (string).

Installation

Init go module first before installation:

go get github.com/hoaitan/cache

Usage

// Test data
type Data struct {
    Number int
}

// Local cache
localCache := local.New(local.Config{
    Enable:     true,
    Size:       10 * 1024 * 1024, // 10 MB
    DefaultTTL: 10,               // TTL in seconds
})

// Redis cache
redisCache := redis.New(redis.Config{
    Enable:     true,
    Endpoint:   "localhost:6379", // Redis endpoint
    Timeout:    60,               // Redis connection timeout
    DefaultTTL: 60,               // TTL in seconds
}, "my-service")

// Multi cache
multiCache := multi.New(
    localCache, // 1st cache layer
    redisCache, // 2nd cache layer
    // ... more cache layers but not recommend because of performance.
)

// Close these caches when don't use any more
defer multiCache.Close()

// Test data
sampleData := &Data{1}
cachedData := new(Data)

// localCache, redisCache or multiCache implement same Cache interface
// so they have same way to use

// Load missing cache
multiCache.Get("key1", cachedData, func() error {
    fmt.Println("missing cache")

    // Set cache when missing with:
    // ttl = -1: using cache's default TTL
    // ttl =  0: never expired
    // ttl >  0: expired in seconds
    return multiCache.Set("key1", sampleData, -1)
})
fmt.Printf("missing cache: cachedData=%+v\n", cachedData)

// Load cached data
multiCache.Get("key1", cachedData, func() error {
    fmt.Println("missing cache")
    return fmt.Errorf("should never happen")
})
fmt.Printf("with cache: cachedData=%+v\n", cachedData)

// Invalid cache in all levels
multiCache.Delete("key1")

Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

Please make sure to update tests as appropriate.

License

MIT

Documentation

Index

Constants

This section is empty.

Variables

View Source
var NotSupportedErr = fmt.Errorf("not suppported")

Functions

func MakeKey

func MakeKey(parts ...string) string

Types

type Cache

type Cache interface {
	// ttl=-1: will use default TTL
	// ttl=0 : no expire
	Set(key string, data interface{}, ttl int) (err error)
	Get(key string, ptr interface{}, fn MissCacheFn) (err error)
	Delete(key string) (ok bool, err error)
	IsExist(key string) (ok bool, err error)
	Flush() (count int, err error)
	IsReady() (ok bool)
	IsEnable() (ok bool)
	Close() (err error)
}

type MissCacheFn

type MissCacheFn func() error

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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