Documentation ¶
Index ¶
- Variables
- func FileGetContents(filename string) (data []byte, e error)
- func FilePutContents(filename string, content []byte) error
- func GetBool(v interface{}) bool
- func GetFloat64(v interface{}) float64
- func GetInt(v interface{}) int
- func GetInt64(v interface{}) int64
- func GetString(v interface{}) string
- func GobDecode(data []byte, to *FileCacheItem) error
- func GobEncode(data interface{}) ([]byte, error)
- func Register(name string, adapter Instance)
- type Cache
- type FileCache
- func (fc *FileCache) ClearAll(context.Context) error
- func (fc *FileCache) Decr(ctx context.Context, key string) error
- func (fc *FileCache) Delete(ctx context.Context, key string) error
- func (fc *FileCache) Get(ctx context.Context, key string) (interface{}, error)
- func (fc *FileCache) GetMulti(ctx context.Context, keys []string) ([]interface{}, error)
- func (fc *FileCache) Incr(ctx context.Context, key string) error
- func (fc *FileCache) Init()
- func (fc *FileCache) IsExist(ctx context.Context, key string) (bool, error)
- func (fc *FileCache) Put(ctx context.Context, key string, val interface{}, timeout time.Duration) error
- func (fc *FileCache) StartAndGC(config string) error
- type FileCacheItem
- type Instance
- type MemoryCache
- func (bc *MemoryCache) ClearAll(context.Context) error
- func (bc *MemoryCache) Decr(ctx context.Context, key string) error
- func (bc *MemoryCache) Delete(ctx context.Context, key string) error
- func (bc *MemoryCache) Get(ctx context.Context, key string) (interface{}, error)
- func (bc *MemoryCache) GetMulti(ctx context.Context, keys []string) ([]interface{}, error)
- func (bc *MemoryCache) Incr(ctx context.Context, key string) error
- func (bc *MemoryCache) IsExist(ctx context.Context, key string) (bool, error)
- func (bc *MemoryCache) Put(ctx context.Context, key string, val interface{}, timeout time.Duration) error
- func (bc *MemoryCache) StartAndGC(config string) error
- type MemoryItem
Constants ¶
This section is empty.
Variables ¶
var ( FileCachePath = "cache" // cache directory FileCacheFileSuffix = ".bin" // cache file suffix FileCacheDirectoryLevel = 2 // cache file deep level if auto generated cache files. FileCacheEmbedExpiry time.Duration // cache expire time, default is no expire forever. )
FileCache Config
var ( // Timer for how often to recycle the expired cache items in memory (in seconds) DefaultEvery = 60 // 1 minute )
Functions ¶
func FileGetContents ¶
FileGetContents Reads bytes from a file. if non-existent, create this file.
func FilePutContents ¶
FilePutContents puts bytes into a file. if non-existent, create this file.
func GobDecode ¶
func GobDecode(data []byte, to *FileCacheItem) error
GobDecode Gob decodes a file cache item.
Types ¶
type Cache ¶
type Cache interface { // Get a cached value by key. Get(ctx context.Context, key string) (interface{}, error) // GetMulti is a batch version of Get. GetMulti(ctx context.Context, keys []string) ([]interface{}, error) // Set a cached value with key and expire time. Put(ctx context.Context, key string, val interface{}, timeout time.Duration) error // Delete cached value by key. Delete(ctx context.Context, key string) error // Increment a cached int value by key, as a counter. Incr(ctx context.Context, key string) error // Decrement a cached int value by key, as a counter. Decr(ctx context.Context, key string) error // Check if a cached value exists or not. IsExist(ctx context.Context, key string) (bool, error) // Clear all cache. ClearAll(ctx context.Context) error // Start gc routine based on config string settings. StartAndGC(config string) error }
Cache interface contains all behaviors for cache adapter. usage:
cache.Register("file",cache.NewFileCache) // this operation is run in init method of file.go. c,err := cache.NewCache("file","{....}") c.Put("key",value, 3600 * time.Second) v := c.Get("key") c.Incr("counter") // now is 1 c.Incr("counter") // now is 2 count := c.Get("counter").(int)
func NewCache ¶
NewCache creates a new cache driver by adapter name and config string. config: must be in JSON format such as {"interval":360}. Starts gc automatically.
func NewFileCache ¶
func NewFileCache() Cache
NewFileCache creates a new file cache with no config. The level and expiry need to be set in the method StartAndGC as config string.
type FileCache ¶
FileCache is cache adapter for file storage.
func (*FileCache) Get ¶
Get value from file cache. if nonexistent or expired return an empty string.
func (*FileCache) GetMulti ¶
GetMulti gets values from file cache. if nonexistent or expired return an empty string.
func (*FileCache) Init ¶
func (fc *FileCache) Init()
Init makes new a dir for file cache if it does not already exist
func (*FileCache) Put ¶
func (fc *FileCache) Put(ctx context.Context, key string, val interface{}, timeout time.Duration) error
Put value into file cache. timeout: how long this file should be kept in ms if timeout equals fc.EmbedExpiry(default is 0), cache this item forever.
func (*FileCache) StartAndGC ¶
StartAndGC starts gc for file cache. config must be in the format {CachePath:"/cache","FileSuffix":".bin","DirectoryLevel":"2","EmbedExpiry":"0"}
type FileCacheItem ¶
FileCacheItem is basic unit of file cache adapter which contains data and expire time.
type MemoryCache ¶
type MemoryCache struct { sync.RWMutex Every int // run an expiration check Every clock time // contains filtered or unexported fields }
MemoryCache is a memory cache adapter. Contains a RW locker for safe map storage.
func (*MemoryCache) ClearAll ¶
func (bc *MemoryCache) ClearAll(context.Context) error
ClearAll deletes all cache in memory.
func (*MemoryCache) Decr ¶
func (bc *MemoryCache) Decr(ctx context.Context, key string) error
Decr decreases counter in memory.
func (*MemoryCache) Delete ¶
func (bc *MemoryCache) Delete(ctx context.Context, key string) error
Delete cache in memory.
func (*MemoryCache) Get ¶
func (bc *MemoryCache) Get(ctx context.Context, key string) (interface{}, error)
Get returns cache from memory. If non-existent or expired, return nil.
func (*MemoryCache) GetMulti ¶
func (bc *MemoryCache) GetMulti(ctx context.Context, keys []string) ([]interface{}, error)
GetMulti gets caches from memory. If non-existent or expired, return nil.
func (*MemoryCache) Incr ¶
func (bc *MemoryCache) Incr(ctx context.Context, key string) error
Incr increases cache counter in memory. Supports int,int32,int64,uint,uint32,uint64.
func (*MemoryCache) Put ¶
func (bc *MemoryCache) Put(ctx context.Context, key string, val interface{}, timeout time.Duration) error
Put puts cache into memory. If lifespan is 0, it will never overwrite this value unless restarted
func (*MemoryCache) StartAndGC ¶
func (bc *MemoryCache) StartAndGC(config string) error
StartAndGC starts memory cache. Checks expiration in every clock time.
type MemoryItem ¶
type MemoryItem struct {
// contains filtered or unexported fields
}
MemoryItem stores memory cache item.