cache

package module
v0.0.0-...-9463477 Latest Latest
Warning

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

Go to latest
Published: Jun 23, 2017 License: MIT Imports: 6 Imported by: 0

README

A Go caching Library

cache is a Go cache manager. It can use many cache. Default use gob encode/decode Serialize data. Supported cache stores

  • Memory
  • Filesystem
  • Memcache
  • Redis

How to install?

go get -u -v  github.com/ghongli/cache

How to use it?

  • First must import it
import (
	"time"
	"fmt"
	
	"github.com/ghongli/cache"
	"github.com/ghongli/cache/filesys"
	"github.com/ghongli/cache/memory"
	"github.com/ghongli/cache/redis"
)
  • Memory
// Setting a larger interval for gc would be better. like time.Minute * 10
// prevent doing the same thing over and over again if nothing really changed
memoryStore := memory.NewMemoryStore(time.Minute*1, nil)
fmt.Println(memoryStore.Put("example", []byte("example"), time.Second*10))
fmt.Println(memoryStore.Get("example"))
fmt.Println(memoryStore.ClearAll())
fmt.Println(memoryStore.Put("n", []byte("n"), time.Second*5))
fmt.Println(memoryStore.Get("n"))
time.Sleep(time.Second * 5)
fmt.Println(memoryStore.Get("n"))
  • Filesystem
fileStore := filesys.MustNewFileSysStore("./../tmp/cache", time.Minute*5, nil)
fmt.Println(fileStore.Put("example", []byte("example"), time.Second*20))
fmt.Println(fileStore.Get("example"))
  • Redis
opt := &r.Options{
	Addr:     "localhost:6379",
	Password: "",
	DB:       0,
}

redisStore := redis.NewRedisCache(opt, redis.PREFIX)
fmt.Println(redisStore.Put("example", []byte("example"), time.Second*10))
val, _ := redisStore.Get("example")
fmt.Println(s.Serialized(val))
fmt.Println(redisStore.ClearAll())
fmt.Println(redisStore.Get("example"))

LICENSE

MIT

Documentation

Index

Constants

View Source
const (
	EXPIRES_DEFAULT = time.Duration(0)
	EXPIRES_FOREVER = time.Duration(-1)
)

Variables

View Source
var (
	ErrCacheMiss                             = errors.New("Key not found")
	ErrCacheNotStored                        = errors.New("Data not stored")
	ErrCacheNotSupported                     = errors.New("Operation not supported")
	ErrCacheDataCannotBeIncreasedOrDecreased = errors.New(`
		Data isn't an integer/string type, it cannot be increased or decreased`)
)

Functions

func GetBool

func GetBool(v interface{}) bool

GetBool convert interface to bool.

func GetFloat64

func GetFloat64(v interface{}) float64

GetFloat64 convert interface to float64.

func GetInt

func GetInt(v interface{}) int

GetInt convert interface to int.

func GetInt64

func GetInt64(v interface{}) int64

GetInt64 convert interface to int64.

func GetString

func GetString(v interface{}) string

GetString convert interface to string.

func Register

func Register(name string, adapter Store)

Register makes a cache adapter available by the adapter name. If Register is called twice with the same name or if adapter is nil, it panics.

Types

type Cache

type Cache interface {
	// get cached value by key.
	Get(key string) ([]byte, error)
	// set cached value with key and expire time.
	Put(key string, data []byte, expire time.Duration) error
	// delete cached value by key.
	Delete(key string) error
	// check if cached value exists or not.
	IsExist(key string) bool
	// clear all cache.
	ClearAll() error
}

Cache interface contains all behaviors for cache adapter.

func NewCache

func NewCache(adapterName string) (cache Cache, err error)

NewCache Create a new cache driver by adapter name and config string. it will start gc automatically.

type CacheItem

type CacheItem struct {
	CreatedTime time.Time
	Data        []byte
	Expired     time.Duration
}

a cached piece of data

func (*CacheItem) IsExpired

func (ci *CacheItem) IsExpired() bool

type GarbageCollector

type GarbageCollector interface {
	//StartAndTrashGc(config string) error
	TrashGc(interval time.Duration)
}

Some caches like redis automatically clear out the cache But for the filesystem and in memory, this cannot. Caches that have to manually clear out the cached data should implement this method. start trash gc routine based on config string settings.

type SerializeHelper

type SerializeHelper struct {
}

a helper types to serialize and deserialize

func (*SerializeHelper) DeSerialized

func (sh *SerializeHelper) DeSerialized(data []byte, i interface{}) error

Writes a byte array into a type. use the encoding/gob package

func (*SerializeHelper) Serialized

func (sh *SerializeHelper) Serialized(i interface{}) ([]byte, error)

Convert a given type into a byte array use the encoding/gob package

type Serializer

type Serializer interface {
	Serialized(i interface{}) ([]byte, error)
	DeSerialized(data []byte, i interface{}) error
}

func NewCacheSerializer

func NewCacheSerializer() Serializer

type Store

type Store func() Cache

Store is a function create a new Cache Instance

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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