cache

package module
v0.0.3 Latest Latest
Warning

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

Go to latest
Published: Mar 15, 2020 License: MIT Imports: 5 Imported by: 0

README

cache

Build Status GoDoc Go Report Card

go cache with multiple backends support, auto to fetch the data if missing.

  • retrieveFunc will be called if the key not in cache
  • cache the missing key for 5s, avoid cache breakdown
  • TTL required
  • support go version 1.11+
  • support backend: memory (via go-redis/cache)
  • support backend: redis(via patrickmn/go-cache)

Installation

$ go get github.com/wklken/cache

Documentation

usage

use string key
package main

import (
	"fmt"
	"time"

	"github.com/wklken/cache"
)

// 1. impl the reterive func
func RetrieveOK(k cache.Key) (interface{}, error) {
	arg := k.(cache.StringKey)
	fmt.Println("arg: ", arg)
	// you can use the arg to fetch data from database or http request
	// username, err := GetFromDatabase(arg)
	// if err != nil {
	//     return nil, err
	// }
	return "ok", nil
}

func main() {
	// 2. new a cache
	c := cache.NewCache(
		"example",
		false,
		RetrieveOK,
		5*time.Minute)

	// 4. use it
	k := cache.NewStringKey("hello")

	data, err := c.Get(k)
	fmt.Println("err == nil: ", err == nil)
	fmt.Println("data from cache: ", data)
}
use your own key
package main

import (
	"fmt"
	"time"

	"github.com/wklken/cache"
)

// 1. impl the key interface, Key() string
type ExampleKey struct {
	Field1 string
	Field2 int64
}

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

// 2. impl the reterive func
func RetrieveExample(inKey cache.Key) (interface{}, error) {
	k := inKey.(ExampleKey)
	fmt.Println("ExampleKey Field1 and Field2 value:", k.Field1, k.Field2)
	// data, err := GetFromDatabase(k.Field1, k.Field2)
	// if err != nil {
	//     return nil, err
	// }
	return "world", nil
}

func main() {
	// 3. new a cache
	c := cache.NewCache(
		"example",
		false,
		RetrieveExample,
		5*time.Minute)

	// 4. use it
	k := ExampleKey{
		Field1: "hello",
		Field2: 42,
	}

	data, err := c.Get(k)
	fmt.Println("err == nil: ", err == nil)
	fmt.Println("data from cache: ", data)

	dataStr, err := c.GetString(k)
	fmt.Println("err == nil: ", err == nil)
	fmt.Printf("data type is %T, value is %s\n", dataStr, dataStr)
}
use redis backend
func retrieveOK(k Key) (interface{}, error) {
	return "ok", nil
}

func main() {
    // 1. mock redis cli via miniredis
	mr, err := miniredis.Run()
	if err != nil {
		panic(err)
	}

	cli := redis.NewClient(&redis.Options{
		Addr: mr.Addr(),
	})

    // 2. create redis backend
	be := NewRedisBackend("test", cli, 5*time.Second)

    // 3. new the cache
	c := NewRedisCache("test", false, retrieveOK, cli, 5 * time.Minute)
}

License

MIT

Documentation

Index

Constants

View Source
const EmptyCacheExpiration = 5 * time.Second

Variables

This section is empty.

Functions

This section is empty.

Types

type BaseCache added in v0.0.2

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

func (*BaseCache) Delete added in v0.0.3

func (c *BaseCache) Delete(key Key) error

func (*BaseCache) DirectGet added in v0.0.3

func (c *BaseCache) DirectGet(key Key) (interface{}, bool)

DirectGet will get key from cache, without calling the retrieveFunc

func (*BaseCache) Disabled added in v0.0.2

func (c *BaseCache) Disabled() bool

func (*BaseCache) Exists added in v0.0.3

func (c *BaseCache) Exists(key Key) bool

func (*BaseCache) Get added in v0.0.2

func (c *BaseCache) Get(key Key) (interface{}, error)

Get will get the key from cache, if missing, will call the retrieveFunc to get the data, add to cache, then return

func (*BaseCache) GetBool added in v0.0.2

func (c *BaseCache) GetBool(k Key) (bool, error)

func (*BaseCache) GetString added in v0.0.2

func (c *BaseCache) GetString(k Key) (string, error)

! if retrieve fail, will return ("", err) for expire time

func (*BaseCache) GetTime added in v0.0.2

func (c *BaseCache) GetTime(k Key) (time.Time, error)

type Cache added in v0.0.2

type Cache interface {
	Get(key Key) (interface{}, error)

	GetString(key Key) (string, error)
	GetBool(key Key) (bool, error)
	GetTime(key Key) (time.Time, error)

	Delete(key Key) error
	Exists(key Key) bool

	DirectGet(key Key) (interface{}, bool)

	Disabled() bool
}

func NewBaseCache added in v0.0.2

func NewBaseCache(disabled bool, retrieveFunc RetrieveFunc, backend backend.Backend) Cache

func NewCache

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

func NewRedisCache

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

type EmptyCache added in v0.0.2

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

TODO: 内存上可以优化, error相同的话使用同一个对象

type Int64Key added in v0.0.2

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

func NewInt64Key added in v0.0.2

func NewInt64Key(key int64) Int64Key

func (Int64Key) Key added in v0.0.2

func (k Int64Key) Key() string

type Key added in v0.0.2

type Key interface {
	Key() string
}

type RetrieveFunc added in v0.0.2

type RetrieveFunc func(key Key) (interface{}, error)

type StringKey added in v0.0.2

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

StringKey

func NewStringKey added in v0.0.2

func NewStringKey(key string) StringKey

func (StringKey) Key added in v0.0.2

func (s StringKey) Key() string

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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