cache

package
v1.0.3 Latest Latest
Warning

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

Go to latest
Published: May 2, 2022 License: GPL-3.0 Imports: 1 Imported by: 0

README

cache

Cache library with cache manager.

All cache driver implemented the cache.Cache interface. So, You can add any custom driver.

Supported Drivers:

  • redis by github.com/go-redis/redis
  • memCached by github.com/bradfitz/gomemcache/memcache
  • freeCache by github.com/coocood/freecache
  • bigCache by github.com/allegro/bigcache

Install

go get github.com/najibulloShapoatov/server-core/cache

You can also install individual drivers

go get github.com/najibulloShapoatov/server-core/cache/bigcache

Cache Interface

All cache driver implemented the cache.Cache interface. So, You can add any custom driver.

// Cache interface definition
type Cache interface {
	Type() string
	// Retrieve value at key from cache
	Get(key string, value interface{}) (err error)
	// Checks if key is available in cache
	Has(key string) (ok bool)
	// Stores a key with a given life time. 0 for permanent
	Set(key string, value interface{}, ttl time.Duration) (err error)
	// Remove a key by name
	Del(key string) (err error)
	// List all available cache keys
	Keys(pattern string) (available []string)
	// Removes all keys
	Clear()
}

Usage example

package main

import (
	"fmt"
	"github.com/allegro/bigcache"
	bigcachecore "github.com/najibulloShapoatov/server-core/cache/bigcache"
	"github.com/najibulloShapoatov/server-core/cache/freecache"
	"github.com/najibulloShapoatov/server-core/cache/memcache"
	"github.com/najibulloShapoatov/server-core/core/cache"
	"github.com/najibulloShapoatov/server-core/core/cache/redis"
	"time"
)

// change package main
func main() {
	redisCfg := redis.Config{Addr: "0.0.0.0:6379", Password: ""}
	redisClient := redis.New(&redisCfg)
	memCacheCfg := memcache.Config{Addr: "127.0.0.1:11211"}
	memCacheClient := memcache.New(&memCacheCfg)
	freeCacheCfg := freecache.Config{}
	freeCacheClient := freecache.New(&freeCacheCfg)
	bigCacheCfg := bigcache.DefaultConfig(5 * time.Second)
	bigCacheClient := bigcachecore.New(&bigCacheCfg)

	// register one(or some) cache driver

	cache.Register(cache.Redis, redisClient)
	cache.Register(cache.MemCache, memCacheClient)
	cache.Register(cache.FreeCache, freeCacheClient)
	cache.Register(cache.BigCache, bigCacheClient)
	cache.DefaultUse(cache.Redis)
	for i := 0; i < 10; i++ {
		key := fmt.Sprintf("testm-%d", i)
		value := []byte(fmt.Sprintf("value-%d", i))

		err := cache.Set(key, value, 5)
		fmt.Println(err)
	}
	var rez []byte
	_ = cache.Get("test-77", &rez)
	fmt.Println(rez)
	cache.DefaultUse(cache.BigCache)
	key := fmt.Sprintf("testm-%d", 99)
	value := []byte(fmt.Sprintf("value-%d", 48777772788))
	_ = cache.Set(key, value, 60)
	_ = cache.Get("testm-99", &rez)
	fmt.Println("BC:", rez)
	fc := cache.Driver(cache.FreeCache)
	keyb := fmt.Sprintf("testdriver-%d", 611011010)
	valueb := []byte(fmt.Sprintf("valuedriver-%d", 611011010))
	err := fc.Set(keyb, valueb, 0)
	fmt.Println("ERR:", err)
	var fcr []byte
	_ = fc.Get("test-611011010", &fcr)
	fmt.Println("FC:", &fcr)

}

Documentation

Index

Constants

View Source
const (
	FreeCache = "freeCache"
	BigCache  = "bigCache"
	Redis     = "redis"
	MemCache  = "memCache"
)

Built in driver name

Variables

This section is empty.

Functions

func DefaultUse

func DefaultUse(driverName string)

DefaultUse set default driver name

func Del

func Del(key string) (err error)

Del remove a key by name

func Get

func Get(key string, value interface{}) (err error)

Get retrieve value at key from cache

func Has

func Has(key string) (ok bool)

Has checks if key is available in cache

func Keys

func Keys(pattern string) (available []string)

Keys list all available cache keys

func Set

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

Set stores a key with a given life time. 0 for permanent

Types

type Cache

type Cache interface {
	// Type returns the type of the cache
	Type() string
	// Get retrieve value at key from cache
	Get(key string, value interface{}) (err error)
	// Has checks if key is available in cache
	Has(key string) (ok bool)
	// Set stores a key with a given life time. 0 for permanent
	Set(key string, value interface{}, ttl time.Duration) (err error)
	// Del removes a key by name
	Del(key string) (err error)
	// Keys list all available cache keys
	Keys(pattern string) (available []string)
	// Clear removes all keys
	Clear()
}

func Default

func Default() Cache

Default get default cache driver instance

func Driver

func Driver(driverName string) Cache

Driver get a driver instance by name

func GetCache

func GetCache(driverName string) Cache

GetCache returns a cache instance by it's type

func Use

func Use(driverName string) Cache

Use driver object by name and set it as default driver.

type Config

type Config struct {
	Engine string `config:"platform.cache.engine" default:"bigCache"`
}

type Manager

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

Manager definition with default driver name and drivers map

func DefManager

func DefManager() *Manager

DefManager get default cache manager instance

func New

func New() *Manager

New creates 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) DefaultUse

func (m *Manager) DefaultUse(driverName string)

DefaultUse sets default driver name

func (*Manager) Del

func (m *Manager) Del(key string) (err error)

Del remove a key by name

func (*Manager) Driver

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

Driver get a driver instance by name

func (*Manager) Get

func (m *Manager) Get(key string, value interface{}) (err error)

Get retrieves value at key from cache

func (*Manager) Has

func (m *Manager) Has(key string) (ok bool)

Has checks if key is available in cache

func (*Manager) Keys

func (m *Manager) Keys(pattern string) (available []string)

Keys lists all available cache keys

func (*Manager) Register

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

Register new driver object

func (*Manager) Set

func (m *Manager) Set(key string, value interface{}, ttl time.Duration) (err error)

Set stores a key with a given life time. 0 for permanent

func (*Manager) Use

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

Use driver object by name and set it as default driver.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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