cache

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Mar 25, 2019 License: MIT Imports: 12 Imported by: 0

README

cache

GoDoc Go Report Card

中文说明

Generic cache use and cache manager for golang.

Supported Drivers:

  • file internal driver
  • memory internal driver
  • redis powered by github.com/gomodule/redigo
  • memCached powered by github.com/bradfitz/gomemcache
  • buntdb powered by github.com/tidwall/buntdb
  • boltdb powered by github.com/etcd-io/bbolt

GoDoc

Interface

// Cache interface definition
type Cache interface {
	// basic op
	Has(key string) bool
	Get(key string) interface{}
	Set(key string, val interface{}, ttl time.Duration) (err error)
	Del(key string) error
	// multi op
	GetMulti(keys []string) map[string]interface{}
	SetMulti(values map[string]interface{}, ttl time.Duration) (err error)
	DelMulti(keys []string) error
	// clear
	Clear() error
}

Usage

package main

import (
	"fmt"
	"github.com/gookit/cache"
	"github.com/gookit/cache/redis"
)

func main() {
	// register one(or some) cache driver
	cache.Register(cache.DvrFile, cache.NewFileCache(""))
	cache.Register(cache.DvrMemory, cache.NewMemoryCache())
	cache.Register(cache.DvrRedis, redis.Connect("127.0.0.1:6379", "", 0))
	
	// setting default driver name
	cache.SetDefName(cache.DvrRedis)

	// quick use.(it is default driver)
	//
	// set
	cache.Set("name", "cache value", cache.TwoMinutes)
	// get
	val := cache.Get("name")
	// del
	cache.Del("name")

	// get: "cache value"
	fmt.Print(val)
}

License

MIT

Documentation

Overview

Package cache is a generic cache use and cache manager for golang. FileCache is a simple local file system cache implement. MemoryCache is a simple memory cache implement.

Example
// register some cache driver
Register(DvrFile, NewFileCache(""))
Register(DvrMemory, NewMemoryCache())
Register(DvrRedis, redis.Connect("127.0.0.1:6379", "", 0))

// setting default driver name
SetDefName(DvrRedis)

// quick use.(it is default driver)
//
// set
Set("name", "cache value", TwoMinutes)
// get
val := Get("name")
// del
Del("name")

// get: "cache value"
fmt.Print(val)
Output:

Index

Examples

Constants

View Source
const (
	// 永远存在
	FOREVER = 0
	// 1 分钟
	OneMinutes = 60 * time.Second
	// 2 分钟
	TwoMinutes = 120 * time.Second
	// 3 分钟
	ThreeMinutes = 180 * time.Second
	// 5 分钟
	FiveMinutes = 300 * time.Second
	// 10 分钟
	TenMinutes = 600 * time.Second
	// 半小时
	HalfHour = 1800 * time.Second
	// 1 小时
	OneHour = 3600 * time.Second
	// 2 小时
	TwoHour = 7200 * time.Second
	// 3 小时
	ThreeHour = 10800 * time.Second
	// 12 小时(半天)
	HalfDay = 43200 * time.Second
	// 24 小时(1 天)
	OneDay = 86400 * time.Second
	// 2 天
	TwoDay = 172800 * time.Second
	// 3 天
	ThreeDay = 259200 * time.Second
	// 7 天(一周)
	OneWeek = 604800 * time.Second
)

some generic expire time define.

View Source
const (
	DvrFile      = "file"
	DvrRedis     = "redis"
	DvrMemory    = "memory"
	DvrMemCached = "memCached"
	DvrBoltDB    = "boltDB"
	DvrBuntDB    = "buntDB"
)

default supported cache driver name

Variables

data (Un)marshal func

Functions

func BindStruct

func BindStruct(val interface{}, ptr interface{}) error

BindStruct get cache value and map to a struct

func Clear

func Clear() error

Clear all caches

func Del

func Del(key string) error

Del value by key

func DelMulti

func DelMulti(keys []string) error

DelMulti values by keys

func Get

func Get(key string) interface{}

Get value by key

func GetMulti

func GetMulti(keys []string) map[string]interface{}

GetMulti values by keys

func GobDecode

func GobDecode(bts []byte, ptr interface{}) error

GobDecode decode data by gob.Decode

func GobEncode

func GobEncode(val interface{}) (bs []byte, err error)

GobEncode encode data by gob.Encode

func Has

func Has(key string) bool

Has cache key

func Set

func Set(key string, val interface{}, ttl time.Duration) error

Set value by key

func SetDefName

func SetDefName(driverName string)

SetDefName set default driver name

func SetMulti

func SetMulti(mv map[string]interface{}, ttl time.Duration) error

SetMulti values

Types

type Cache

type Cache interface {
	io.Closer

	// basic op
	// Has cache key
	Has(key string) bool
	// Get value by key
	Get(key string) interface{}
	Set(key string, val interface{}, ttl time.Duration) (err error)
	Del(key string) error
	// multi op
	GetMulti(keys []string) map[string]interface{}
	SetMulti(values map[string]interface{}, ttl time.Duration) (err error)
	DelMulti(keys []string) error
	// clear
	Clear() error
}

Cache interface definition

func Default

func Default() Cache

Default get default cache driver instance

func Use

func Use(driverName string) Cache

Use returns a driver instance

type FileCache

type FileCache struct {
	// caches in memory
	MemoryCache
	// contains filtered or unexported fields
}

FileCache definition.

Example
c := NewFileCache("./testdata")
key := "name"

// set
c.Set(key, "cache value", TwoMinutes)
fmt.Println(c.Has(key))

// get
val := c.Get(key)
fmt.Println(val)

// del
c.Del(key)
fmt.Println(c.Has(key))
Output:

true
cache value
false

func NewFileCache

func NewFileCache(dir string, pfxAndKey ...string) *FileCache

NewFileCache create a FileCache instance

func (*FileCache) Clear

func (c *FileCache) Clear() error

Clear caches and files

func (*FileCache) Del

func (c *FileCache) Del(key string) error

Del value by key

func (*FileCache) DelMulti

func (c *FileCache) DelMulti(keys []string) error

DelMulti values by multi key

func (*FileCache) Get

func (c *FileCache) Get(key string) interface{}

Get value by key

func (*FileCache) GetFilename

func (c *FileCache) GetFilename(key string) string

GetFilename cache file name build

func (*FileCache) GetMulti

func (c *FileCache) GetMulti(keys []string) map[string]interface{}

GetMulti values by multi key

func (*FileCache) Has

func (c *FileCache) Has(key string) bool

Has cache key. TODO decode value, and check expire time

func (*FileCache) Set

func (c *FileCache) Set(key string, val interface{}, ttl time.Duration) (err error)

Set value by key

func (*FileCache) SetMulti

func (c *FileCache) SetMulti(values map[string]interface{}, ttl time.Duration) (err error)

SetMulti values by multi key

type Item

type Item struct {
	// Exp expire time
	Exp int64
	// Val cache value storage
	Val interface{}
}

Item for memory cache

type Manager

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

Manager definition

func DefMgr

func DefMgr() *Manager

DefMgr get default cache manager instance

func NewManager

func NewManager() *Manager

NewManager create a cache manager instance

func Register

func Register(name string, driver Cache) *Manager

Register driver to manager instance

func (*Manager) DefName

func (m *Manager) DefName() string

DefName get default driver name

func (*Manager) Default

func (m *Manager) Default() Cache

Default returns the default driver instance

func (*Manager) Driver

func (m *Manager) Driver(name string) Cache

Driver object get

func (*Manager) Get

func (m *Manager) Get(name string) Cache

Get driver object by name

func (*Manager) Register

func (m *Manager) Register(name string, driver Cache) *Manager

Register new driver object

func (*Manager) SetDefName

func (m *Manager) SetDefName(driverName string)

SetDefName set default driver name

func (*Manager) Use

func (m *Manager) Use(driverName string) Cache

Use returns a driver instance

type MarshalFunc

type MarshalFunc func(v interface{}) ([]byte, error)

MarshalFunc define

type MemoryCache

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

MemoryCache definition.

Example
c := NewMemoryCache()
key := "name"

// set
c.Set(key, "cache value", TwoMinutes)
fmt.Println(c.Has(key), c.Count())

// get
val := c.Get(key)
fmt.Println(val)

// del
c.Del(key)
fmt.Println(c.Has(key), c.Count())
Output:

true 1
cache value
false 0

func NewMemoryCache

func NewMemoryCache() *MemoryCache

NewMemoryCache create a memory cache instance

func (*MemoryCache) Clear

func (c *MemoryCache) Clear() error

Clear all caches

func (*MemoryCache) Count

func (c *MemoryCache) Count() int

Count cache item number

func (*MemoryCache) Del

func (c *MemoryCache) Del(key string) error

Del cache by key

func (*MemoryCache) DelMulti

func (c *MemoryCache) DelMulti(keys []string) error

DelMulti values by multi key

func (*MemoryCache) DumpDB

func (c *MemoryCache) DumpDB(file string) error

DumpDB to a file

func (*MemoryCache) Get

func (c *MemoryCache) Get(key string) interface{}

Get cache value by key

func (*MemoryCache) GetMulti

func (c *MemoryCache) GetMulti(keys []string) map[string]interface{}

GetMulti values by multi key

func (*MemoryCache) Has

func (c *MemoryCache) Has(key string) bool

Has cache key

func (*MemoryCache) Iter

func (c *MemoryCache) Iter(file string) error

Iter iteration all caches

func (*MemoryCache) LastErr

func (c *MemoryCache) LastErr() error

LastErr get

func (*MemoryCache) Restore

func (c *MemoryCache) Restore(file string) error

Restore DB from a file

func (*MemoryCache) Set

func (c *MemoryCache) Set(key string, val interface{}, ttl time.Duration) (err error)

Set cache value by key

func (*MemoryCache) SetMulti

func (c *MemoryCache) SetMulti(values map[string]interface{}, ttl time.Duration) (err error)

SetMulti values by multi key

type UnmarshalFunc

type UnmarshalFunc func(data []byte, v interface{}) error

UnmarshalFunc define

Directories

Path Synopsis
Package leveldb use the https://github.com/dgraph-io/badger as cache driver
Package leveldb use the https://github.com/dgraph-io/badger as cache driver
Package bolt use the github.com/etcd-io/bbolt as cache driver
Package bolt use the github.com/etcd-io/bbolt as cache driver
Package buntdb use the github.com/tidwall/buntdb as cache driver
Package buntdb use the github.com/tidwall/buntdb as cache driver
Package gcache use the github.com/bluele/gcache as cache driver
Package gcache use the github.com/bluele/gcache as cache driver
Package leveldb use the https://github.com/syndtr/goleveldb as cache driver
Package leveldb use the https://github.com/syndtr/goleveldb as cache driver
Package memcached use the "github.com/bradfitz/gomemcache/memcache" as cache driver
Package memcached use the "github.com/bradfitz/gomemcache/memcache" as cache driver
Package nutsdb use the https://github.com/xujiajun/nutsdb as cache driver
Package nutsdb use the https://github.com/xujiajun/nutsdb as cache driver
Package redis is a simple redis cache implement.
Package redis is a simple redis cache implement.

Jump to

Keyboard shortcuts

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