cache

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

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

Go to latest
Published: Nov 3, 2018 License: MIT Imports: 21 Imported by: 0

README

Go cache

GoDoc Build Status Go Report Card

Installation

To install the Go Cache, please execute the following go get command.

go get github.com/apzuk3/go-cache

Usage

package main

import (
    "fmt"
    "time"

    "github.com/apzuk3/go-cache"
)

func main() {
    c := cache.New(
        cache.WithStorage(cache.InMemory())
        cache.WithStorage(cache.Filesystem("./cache"))
    )

    c.Set("key1", 123, 0, "tag1", "tag2")
    c.Set("key2", "abc", 0, "tag2")

    var i int
    c.Get("key1", &i)
    fmt.Println(i) // prints 123

    var v interface{}
    c.ByTag("tag2", &v)
    fmt.Printf("%v\n", v) // prints []interface{}{123, "abs"}
}

Contributing

If you found bugs please file an issue or pull-request with the fix

License

The library is available as open source under the terms of the MIT License.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrKeyNotExist indicates that the key does not exist in the storage
	ErrKeyNotExist = errors.New("key does not exist")

	// ErrNotJSONMarshalable indicates that the content is not json marshalable
	ErrNotJSONMarshalable = errors.New("value is not json marshalable")
)

Functions

This section is empty.

Types

type Cache

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

Cache manages to Set, Get, Delet and Tag keys

func New

func New(options ...Option) *Cache

New constructs a new Cache instance which can store, read and remove items with tags

func (*Cache) ByTag

func (c *Cache) ByTag(tag string, out interface{}) error

ByTag reads tagged values into `out` `out` is always a slice of values

func (*Cache) Close

func (c *Cache) Close() (err error)

Close closes the storage resource if it implements io.Closer

func (*Cache) Del

func (c *Cache) Del(keys ...string) error

Del deletes the given key from all registered storage

func (*Cache) DelByTag

func (c *Cache) DelByTag(tags ...string) error

DelByTag deletes tagged values

func (*Cache) Extend

func (c *Cache) Extend(key string, expiration time.Duration) error

Extend sets the new expiration time for the given key If the expiration has not initially been set this method will add one

func (*Cache) Flush

func (c *Cache) Flush() (err error)

Flush flushes all the data in all registered storage

func (*Cache) Get

func (c *Cache) Get(key string, out interface{}) error

Get reads for the given key from the registered storage unless a valid content is received. It will ignore any error occurred for medium level storage

func (*Cache) Loop

func (c *Cache) Loop(high func(s Storage) (bool, error), medium func(s Storage) (bool, error)) (e error)

Loop iterates through registered high and medium storage and pass them to the coressponding function to use

func (*Cache) NsKey

func (c *Cache) NsKey(key string) string

NsKey wraps the given key with the namespace prefix

func (*Cache) Set

func (c *Cache) Set(key string, v interface{}, expiration time.Duration, tags ...string) error

Set stores a value into configured stores expiration and tags list Zero expiration means the key has no expiration time. Additionally, all string argument passed after expiration will be used to tag the value It ignores any error occurred for medium level storage

type Fs

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

Fs is a struct implementing Storage interface using File System as data storage

func Filesystem

func Filesystem(dir string) Fs

Filesystem creates a new File System storage which can be used when creating a new cache instance cache.New(WithStorage(...))

func (Fs) Delete

func (f Fs) Delete(key string) error

Delete deletes file with cached content

func (Fs) Flush

func (f Fs) Flush() error

Flush flushes File System storage

func (Fs) Read

func (f Fs) Read(key string) (interface{}, error)

Read reads the cached content from the corresponding file

func (Fs) Write

func (f Fs) Write(key string, v interface{}, ttl time.Duration) error

Write writes the given content for the given key in File System storage

type InMem

type InMem struct {
	sync.RWMutex
	// contains filtered or unexported fields
}

InMem is a struct which implements Storage interface using memory as storage

func InMemory

func InMemory() *InMem

InMemory creates a new in memory storage which can be passed to cache.New(WithStorage(...))

func (*InMem) Delete

func (i *InMem) Delete(key string) error

Delete deletes content of the given key from in memory storage

func (*InMem) Flush

func (i *InMem) Flush() error

Flush flushes in momory storage

func (*InMem) Read

func (i *InMem) Read(key string) (interface{}, error)

Read reads coontent for the given key from in memory storage

func (*InMem) Write

func (i *InMem) Write(key string, v interface{}, d time.Duration) error

Write writes the given content for the given key in memory storage

type Memcache

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

Memcache is a structure implemening Storage interface using memcached as a storage provider

func Memcached

func Memcached(servers ...string) Memcache

Memcached creates a new memcached storage which can be passed to cache.New(WithStorage(...))

func (Memcache) Delete

func (m Memcache) Delete(key string) error

Delete deletes content of the given key from in memcached storage

func (Memcache) Flush

func (m Memcache) Flush() error

Flush flushes memcached storage

func (Memcache) Read

func (m Memcache) Read(key string) (interface{}, error)

Read reads coontent for the given key from in memcached storage

func (Memcache) Write

func (m Memcache) Write(key string, v interface{}, ttl time.Duration) error

Write writes the given content for the given key in memcached storage

type Option

type Option func(c *Cache)

Option is the type of constructor options for New(...).

func WithDebug

func WithDebug() Option

WithDebug configures a cache instance with debug flag on

func WithHighPriorityStorage

func WithHighPriorityStorage(storage ...Storage) Option

WithHighPriorityStorage configures a cache instance with a priority cache.Storage to store data

func WithMediumPriorityStorage

func WithMediumPriorityStorage(storage ...Storage) Option

WithMediumPriorityStorage configures a cache instance with a medium cache.Storage to store data

func WithNamespace

func WithNamespace(ns string) Option

WithNamespace configures a cache instance with namespace.

func WithStorage

func WithStorage(storage ...Storage) Option

WithStorage configures a cache client with a cache.Storage to store data

func WithTagger

func WithTagger(tagger Tagger) Option

WithTagger configures a cache instance with custom cache.Tagger

type Priority

type Priority int

Priority refers to the storage priority There are `high` and `medium` priorities for storage Any error occurred for high level storage will be reported Any error occurred for medium level storage will be ignored

const (
	// PriorityMedium is a medium level storage priority
	PriorityMedium Priority = iota

	// PriorityHigh is a high level storage priority
	PriorityHigh
)

type Storage

type Storage interface {
	// Write writes to the storage
	Write(key string, v interface{}, ttl time.Duration) error

	// Read reads from the storage for the key
	Read(key string) (interface{}, error)

	// Delete deletes from the storage
	Delete(key string) error

	// Flush flushes cache storage
	Flush() error
}

Storage is an interface to write, read, delete and empty a data storage. Any struct implementing Storage interface can be passed to cache.New(WithStorage(...)) to use as taggable cache data adapter

func NewS3

func NewS3(sess *session.Session, bucket string) Storage

func Redis

func Redis(options *redisClient.Options) Storage

type Tagger

type Tagger interface {
	// attach tags to the given key
	Tag(s Storage, key string, tags ...string) error

	// unattach tags from the given key
	UnTag(s Storage, key string, tags ...string) error

	// receive all key's tags
	Tags(s Storage, key string) ([]string, error)

	// receive all tag's keys
	Keys(s Storage, tag string) ([]string, error)
}

Tagger ins an interface to tag and untag data with the given tags Any struct implemening tagger interface can be passed to cache.New(WithTagger(...)) to use a data tagger

Jump to

Keyboard shortcuts

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