cache

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

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

Go to latest
Published: Feb 13, 2025 License: MIT Imports: 10 Imported by: 0

README

cacher

Cacher is an interface on top of different databases

How to use

go get github.com/csturiale/cacher

REDIS
opts := cache.Options{
    Server:   localhost,
    Port:     6379,
    Password: "",
    Prefix:   "myprefix",
    DB:       0
}
	
db, err = cache.New(&cache.RedisCache{}, &opts)
if err != nil {
	log.Fatalf("Cannot connect to redis")
}
BADGER
opts := cache.Options{
    Path:   "badger"
}
db, err = cache.New(&cache.BadgerCache{}, &opts)
if err != nil {
	log.Fatalf("Cannot connect to Badger")
}
BUNTDB
opts := cache.Options{
    Path:   ":memory:" // For in memory DB, otherwise you can set a path
}
db, err = cache.New(&cache.BuntDBCache{}, &opts)
if err != nil {
	log.Fatalf("Cannot connect to BuntDB")
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type BadgerCache

type BadgerCache struct {
	Conn   *badger.DB
	Prefix string
}

func (*BadgerCache) Close

func (b *BadgerCache) Close() error

Close closes the badger database.

func (*BadgerCache) Empty

func (b *BadgerCache) Empty() error

Empty removes all entries in Badger.

func (*BadgerCache) EmptyByMatch

func (b *BadgerCache) EmptyByMatch(str string) error

EmptyByMatch removes all entries in Redis which have the prefix match.

func (*BadgerCache) Forget

func (b *BadgerCache) Forget(str string) error

Forget removes an item from the cache, by key.

func (*BadgerCache) Get

func (b *BadgerCache) Get(str string, dest any) (any, error)

Get attempts to retrieve a value from the cache.

func (*BadgerCache) GetAll

func (c *BadgerCache) GetAll() (map[string]any, error)

func (*BadgerCache) GetDefaultOpts

func (b *BadgerCache) GetDefaultOpts() *Options

func (*BadgerCache) GetInt

func (b *BadgerCache) GetInt(key string) (int, error)

GetInt is a convenience method which retrieves a value from the cache, converts it to an int, and returns it.

func (*BadgerCache) GetString

func (b *BadgerCache) GetString(key string) (string, error)

GetString is a convenience method which retrieves a value from the cache and returns it as a string.

func (*BadgerCache) GetTime

func (b *BadgerCache) GetTime(key string) (time.Time, error)

GetTime retrieves a value from the cache by the specified key and returns it as time.Time.

func (*BadgerCache) Has

func (b *BadgerCache) Has(str string) bool

func (*BadgerCache) JSONDel

func (c *BadgerCache) JSONDel(key string) error

func (*BadgerCache) JSONGet

func (c *BadgerCache) JSONGet(key string) (any, error)

func (*BadgerCache) JSONSearch

func (c *BadgerCache) JSONSearch(index string, query string) ([]any, error)

func (*BadgerCache) JSONSet

func (c *BadgerCache) JSONSet(key string, data any) error

func (*BadgerCache) Set

func (b *BadgerCache) Set(str string, value any, expires ...time.Duration) error

Set puts a value into Badger. The final parameter, expires, is optional.

type BuntDBCache

type BuntDBCache struct {
	Conn   *buntdb.DB
	Prefix string
}

func (*BuntDBCache) Close

func (b *BuntDBCache) Close() error

Close closes the badger database.

func (*BuntDBCache) Empty

func (b *BuntDBCache) Empty() error

Empty removes all entries in Badger.

func (*BuntDBCache) EmptyByMatch

func (b *BuntDBCache) EmptyByMatch(str string) error

EmptyByMatch removes all entries in Redis which have the prefix match.

func (*BuntDBCache) Forget

func (b *BuntDBCache) Forget(str string) error

Forget removes an item from the cache, by key.

func (*BuntDBCache) Get

func (b *BuntDBCache) Get(str string, dest any) (any, error)

Get attempts to retrieve a value from the cache.

func (*BuntDBCache) GetAll

func (c *BuntDBCache) GetAll() (map[string]any, error)

func (*BuntDBCache) GetDefaultOpts

func (c *BuntDBCache) GetDefaultOpts() *Options

func (*BuntDBCache) GetInt

func (b *BuntDBCache) GetInt(key string) (int, error)

GetInt is a convenience method which retrieves a value from the cache, converts it to an int, and returns it.

func (*BuntDBCache) GetString

func (b *BuntDBCache) GetString(key string) (string, error)

GetString is a convenience method which retrieves a value from the cache and returns it as a string.

func (*BuntDBCache) GetTime

func (b *BuntDBCache) GetTime(key string) (time.Time, error)

GetTime retrieves a value from the cache by the specified key and returns it as time.Time.

func (*BuntDBCache) Has

func (b *BuntDBCache) Has(str string) bool

func (*BuntDBCache) JSONDel

func (c *BuntDBCache) JSONDel(key string) error

func (*BuntDBCache) JSONGet

func (c *BuntDBCache) JSONGet(key string) (any, error)

func (*BuntDBCache) JSONSearch

func (c *BuntDBCache) JSONSearch(index string, query string) ([]any, error)

func (*BuntDBCache) JSONSet

func (c *BuntDBCache) JSONSet(key string, data any) error

func (*BuntDBCache) Set

func (b *BuntDBCache) Set(str string, value any, expires ...time.Duration) error

Set puts a value into BuntDB. The final parameter, expires, is optional.

type Cacher

type Cacher interface {
	GetDefaultOpts() *Options
	Empty() error
	EmptyByMatch(match string) error
	Forget(key string) error
	Get(key string, dest any) (any, error)
	GetInt(key string) (int, error)
	GetString(key string) (string, error)
	GetTime(key string) (time.Time, error)
	GetAll() (map[string]any, error)
	Has(key string) bool
	Set(key string, data any, expires ...time.Duration) error
	JSONSet(key string, data any) error
	JSONGet(key string) (any, error)
	JSONDel(key string) error
	JSONSearch(index string, query string) ([]any, error)
	Close() error
	// contains filtered or unexported methods
}

Cacher is the interface which anything providing cache functionality must satisfy.

func New

func New(cache Cacher, o ...*Options) (Cacher, error)

New is a factory method which returns an instance of a Cacher.

type Options

type Options struct {
	Server   string `yaml:"server"`   // The server where Redis exists.
	Port     string `yaml:"port"`     // The port Redis is listening on.
	Password string `yaml:"password"` // The password for Redis.
	Prefix   string `yaml:"prefix"`   // A prefix to use for all keys for this client.
	DB       int    `yaml:"db"`       // Database. Specifying 0 (the default) means use the default database.
	Path     string `yaml:"path"`     // The location for the database on disk.
}

Options is the type used to configure a Cacher object.

type RedisCache

type RedisCache struct {
	Conn   *redis.Client
	Prefix string
}

func (*RedisCache) Close

func (c *RedisCache) Close() error

Close closes the pool of redis connections

func (*RedisCache) Empty

func (c *RedisCache) Empty() error

Empty removes all entries in Redis for a given client.

func (*RedisCache) EmptyByMatch

func (c *RedisCache) EmptyByMatch(match string) error

EmptyByMatch removes all entries in Redis which have the prefix match.

func (*RedisCache) Forget

func (c *RedisCache) Forget(key string) error

Forget removes an item from the cache, by key.

func (*RedisCache) Get

func (c *RedisCache) Get(key string, dest any) (any, error)

Get attempts to retrieve a value from the cache.

func (*RedisCache) GetAll

func (c *RedisCache) GetAll() (map[string]any, error)

func (*RedisCache) GetDefaultOpts

func (c *RedisCache) GetDefaultOpts() *Options

func (*RedisCache) GetInt

func (c *RedisCache) GetInt(key string) (int, error)

GetInt is a convenience method which retrieves a value from the cache, converts it to an int, and returns it.

func (*RedisCache) GetString

func (c *RedisCache) GetString(key string) (string, error)

GetString is a convenience method which retrieves a value from the cache and returns it as a string.

func (*RedisCache) GetTime

func (c *RedisCache) GetTime(key string) (time.Time, error)

GetTime retrieves a value from the cache by the specified key and returns it as time.Time.

func (*RedisCache) Has

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

Has checks to see if the supplied key is in the cache and returns true if found, otherwise false.

func (*RedisCache) JSONDel

func (c *RedisCache) JSONDel(key string) error

func (*RedisCache) JSONGet

func (c *RedisCache) JSONGet(key string) (any, error)

func (*RedisCache) JSONSearch

func (c *RedisCache) JSONSearch(index string, query string) ([]any, error)

func (*RedisCache) JSONSet

func (c *RedisCache) JSONSet(key string, data any) error

func (*RedisCache) Set

func (c *RedisCache) Set(key string, data any, expires ...time.Duration) error

Set puts a value into Redis. The final parameter, expires, is optional.

Jump to

Keyboard shortcuts

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