onecache

package module
v0.0.0-...-4af24e4 Latest Latest
Warning

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

Go to latest
Published: May 25, 2020 License: MIT Imports: 5 Imported by: 3

README

OneCache - A Go caching Library

Coverage Status Build Status

Installation


$ go get -u github.com/adelowo/onecache

Supported cache stores

  • InMemory
  • Filesystem
  • Memcached
  • Redis

OneCache also comes with garbage collection. This is used by the filesystem and memory adapter to purge out expired items automatically. Please refer to the examples

Examples containing all adapters can be found here

var store onecache.Store

store = filesystem.MustNewFSStore("/home/adez/onecache_tmp")

err := store.Set("profile", []byte("Lanre"), time.Second*60)

if err != nil {
	fmt.Println(err)
	return
}

value,err := store.Get("profile")
if err != nil {
	fmt.Println(err)
	return
}

fmt.Println(string(value))

Some adapters like the filesystem and memory have a Garbage collection implementation. All that is needed to call is store.GC(). Ideally, this should be called in a ticker.C.

LICENSE

MIT

Documentation

Index

Constants

View Source
const (
	EXPIRES_DEFAULT = time.Duration(0)
	EXPIRES_FOREVER = time.Duration(-1)
)

Variables

View Source
var (
	ErrCacheMiss                             = errors.New("Key not found")
	ErrCacheNotStored                        = errors.New("Data not stored")
	ErrCacheNotSupported                     = errors.New("Operation not supported")
	ErrCacheDataCannotBeIncreasedOrDecreased = errors.New(`
		Data isn't an integer/string type. Hence, it cannot be increased or decreased`)
)

Functions

func Decrement

func Decrement(val interface{}, steps int) (interface{}, error)

Decrement decreases the value of an item by the specified number of steps

func DefaultKeyFunc

func DefaultKeyFunc(s string) string

DefaultKeyFunc is the default implementation of cache keys All it does is to preprend "onecache:" to the key sent in by client code

func Increment

func Increment(val interface{}, steps int) (interface{}, error)

Increment increases the value of an item by the specified number of steps

Types

type CacheSerializer

type CacheSerializer struct {
}

Helper to serialize and deserialize types

func NewCacheSerializer

func NewCacheSerializer() *CacheSerializer

func (*CacheSerializer) DeSerialize

func (b *CacheSerializer) DeSerialize(data []byte, i interface{}) error

Writes a byte array into a type.

func (*CacheSerializer) Serialize

func (b *CacheSerializer) Serialize(i interface{}) ([]byte, error)

Convert a given type into a byte array Caveat -> Types you create might have to be registered with the encoding/gob package

type GarbageCollector

type GarbageCollector interface {
	GC()
}

Some stores like redis and memcache automatically clear out the cache But for the filesystem and in memory, this cannot be said. Stores that have to manually clear out the cached data should implement this method. It's implementation should re run this function everytime the interval is reached Say every 5 minutes.

type Item

type Item struct {
	ExpiresAt time.Time
	Data      []byte
}

Item identifes a cached piece of data

func (*Item) IsExpired

func (i *Item) IsExpired() bool

Helper method to check if an item is expired. Current usecase for this is for garbage collection

type KeyFunc

type KeyFunc func(s string) string

KeyFunc defines a transformer for cache keys

type Serializer

type Serializer interface {
	Serialize(i interface{}) ([]byte, error)
	DeSerialize(data []byte, i interface{}) error
}

type Store

type Store interface {
	Set(key string, data []byte, expires time.Duration) error
	Get(key string) ([]byte, error)
	Delete(key string) error
	Flush() error
	Has(key string) bool
}

Interface for all onecache store implementations

Directories

Path Synopsis
Package filesystem provides a filesystem cache implementation for onecache
Package filesystem provides a filesystem cache implementation for onecache
Package memcached is a cache implementation for onecache which uses memcached
Package memcached is a cache implementation for onecache which uses memcached
Package memory provides a lightweight in memory store for onecache Do take a look at other stores
Package memory provides a lightweight in memory store for onecache Do take a look at other stores
Package redis provides a cache implementation of onecache using redis as the backend
Package redis provides a cache implementation of onecache using redis as the backend

Jump to

Keyboard shortcuts

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