eurekache

package module
v0.3.3 Latest Latest
Warning

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

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

README

eurekache

GoDoc Release Build Status Co decov Coverage Go Report Card Code Climate BCH compliance

eurekache is cache library, implementing multiple cache source and fallback system.

Supported Cache Type

  • on memory
    • max size
    • expire ttl
  • Redis

Installation

Install eurekache and required packages using go get command:

$ go get github.com/evalphobia/eurekache

Usage

caches

Memory cache

// create on-memory cache
maxCacheItemSize := 100 // max allocated caches
expiredTTL := 5 * 60 * 1000 // 5 minutes (millisecond)

mc := memorycache.NewCacheTTL(maxCacheItemSize)
mc.SetTTL(expiredTTL)

cache := eurekache.New()
cache.SetCacheSources([]cache{mc})

Redis cache

import redigo "github.com/garyburd/redigo/redis"

// create redis cache
redisHost := "127.0.0.1:6379"
expiredTTL := 5 * 60 * 1000 // 5 minutes (millisecond)
keyPrefix := "myapp:" // added key prefix before set on redis
dbNumber := 1 // redis db number

pool := &redigo.Pool{
    Dial: func() (redigo.Conn, error) {
        return redigo.Dial("tcp", redisHost)
    },
}

rc := rediscache.NewRedisCache(pool)
rc.SetTTL(expiredTTL)
rc.SetPrefix(keyPrefix)
rc.Select(dbNumber)

cache := eurekache.New()
cache.SetCacheSources([]cache{rc})

Multiple cache

// search cache using from 1st cache to last cache by index order
cacheSources := []cache{mc, rc}

cache := eurekache.New()
cache.SetCacheSources(cacheSources)

Set data

cache := eurekache.New()
cache.SetCacheSources([]cache{mc, rc})


// save data to all of caches with default TTL
// when TTL=0, cache is not expired
cache.Set("key", "value")

// save data and cache lives on 24 hours
cache.SetExpire("key", "value", 24 * 60 * 60 * 1000)

Eurekache uses encoding/gob internally, you register your own types before use it.

type MyType struct {
    Data interface{}
}

func init() {
    gob.Register(&MyType{})
}

Get data

cache := eurekache.New()
cache.SetCacheSources([]cache{mc, rc})

var ok bool // is cache existed or not

// pass pointer value; type must be equal
var stringValue string
ok = cache.Get("key", &stringValue)

// return interface value
var result interface{}
result, ok = cache.GetInterface("key")
stringValue, ok = result.(string)

// return []byte encoded by gob
var b []byte
b, ok := cache.GetGobBytes("key")
dec := gob.NewDecoder(bytes.NewBuffer(b))
err = dec.Decode(&stringValue)

Contribution

Thanks!

Before create pull request, check the codes using below commands:

$ go vet
$ gofmt -s -l .
$ golint

And test on your local machine:

# install assertion library
$ go get github.com/stretchr/testify/assert

# you need to install and run redis-server before running test
$ go test ./...

Documentation

Overview

Package eurekache provides fallback cache system with multiple cache source

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CopyValue added in v0.2.0

func CopyValue(dst, src interface{}) bool

CopyValue copies src value into dst.

Types

type Cache

type Cache interface {
	Get(string, interface{}) bool
	GetInterface(string) (interface{}, bool)
	GetGobBytes(string) ([]byte, bool)
	Set(string, interface{}) error
	SetExpire(string, interface{}, int64) error
	Clear() error
}

Cache is interface for storing data

type Eurekache

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

Eurekache will contains multiple cache source

func New

func New() *Eurekache

New returns empty new Eurekache

func (*Eurekache) AddCacheSource added in v0.3.1

func (e *Eurekache) AddCacheSource(cache Cache)

AddCacheSource adds cache source

func (*Eurekache) ClearAll added in v0.3.3

func (e *Eurekache) ClearAll() error

ClearAll deletes all of cached data from cache sorces.

func (*Eurekache) Get

func (e *Eurekache) Get(key string, data interface{}) (ok bool)

Get searches cache by given key and returns flag of cache is existed or not. when cache hit, data is assigned.

func (*Eurekache) GetGobBytes

func (e *Eurekache) GetGobBytes(key string) (b []byte, ok bool)

GetGobBytes searches cache by given key and returns gob-encoded value.

func (*Eurekache) GetInterface

func (e *Eurekache) GetInterface(key string) (v interface{}, ok bool)

GetInterface searches cache by given key and returns interface value.

func (*Eurekache) Set

func (e *Eurekache) Set(key string, data interface{})

Set sets data into all of cache sources.

func (*Eurekache) SetCacheSources

func (e *Eurekache) SetCacheSources(caches []Cache)

SetCacheSources sets cache sources

func (*Eurekache) SetExpire

func (e *Eurekache) SetExpire(key string, data interface{}, ttl int64)

SetExpire sets data with TTL.

func (*Eurekache) SetReadTimeout added in v0.3.2

func (e *Eurekache) SetReadTimeout(d time.Duration)

SetReadTimeout sets read timeout

func (*Eurekache) SetTimeout added in v0.3.2

func (e *Eurekache) SetTimeout(d time.Duration)

SetTimeout sets r/w timeout

func (*Eurekache) SetWriteTimeout added in v0.3.2

func (e *Eurekache) SetWriteTimeout(d time.Duration)

SetWriteTimeout sets write timeout

type Item

type Item struct {
	// unix nanosec of creation time
	CreatedAt int64

	// unix nanosec of expires time
	ExpiredAt int64

	// The actual value stored in this item.
	Value interface{}
}

Item contains actual value and meta data for cache.

func NewItem added in v0.3.0

func NewItem() *Item

NewItem returns initialized *Item

func (*Item) Init added in v0.2.0

func (i *Item) Init()

Init initializes Item

func (*Item) SetExpire

func (i *Item) SetExpire(ttl int64)

SetExpire updates ExpiredAt from given ttl millisec

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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