cache

package
v0.0.23 Latest Latest
Warning

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

Go to latest
Published: Jul 31, 2020 License: Apache-2.0 Imports: 2 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// TypeGoCache - type go cache
	TypeGoCache = "goCache"
	// TypeMemoryCache - type use memcached
	TypeMemoryCache = "memCache"
	// TypeRedisCache - type use redis
	TypeRedisCache = "redisCache"
)

Variables

This section is empty.

Functions

func Register

func Register(name string, adapter Instance)

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

Types

type Cache

type Cache interface {
	// get cached value by key.
	Get(key string) interface{}
	// GetMulti is a batch version of Get.
	GetMulti(keys []string) []interface{}
	// set cached value with key and expire time.
	Put(key string, val interface{}, timeout time.Duration) error
	// delete cached value by key.
	Delete(key string) error
	// increase cached int value by key, as a counter.
	Incr(key string) error
	// decrease cached int value by key, as a counter.
	Decr(key string) error
	// increase cached int value by key and return value.
	IncrBy(key string) (interface{}, error)
	// decrease cached int value by key and return value.
	DecrBy(key string) (interface{}, error)

	// check if cached value exists or not.
	IsExist(key string) bool
	// clear all cache.
	ClearAll() error
	// start gc routine based on config settings.
	StartAndGC(config Config) error
}

Cache interface contains all behaviors for cache adapter.

func NewCache

func NewCache(adapterName string, config Config) (adapter Cache, err error)

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

type Config

type Config struct {
	Expiration time.Duration `json:"expiration"`

	// public common
	Server   string `json:"server"`
	User     string `json:"user"`
	Password string `json:"password"`
	Port     int64  `json:"port"`

	// redis
	DBNum     int64  `json:"db_num"`
	MaxIdle   int64  `json:"max_idle"`
	MaxActive int64  `json:"max_active"`
	Key       string `json:"key"`

	// Extend fields
	// Extended fields can be used if there is a special implementation
	Extend1 string `json:"extend_1"`
	Extend2 string `json:"extend_2"`
}

Config - define cache config

type Instance

type Instance func() Cache

Instance 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