cachego

package module
v0.0.0-...-8e7f04e Latest Latest
Warning

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

Go to latest
Published: Aug 19, 2019 License: MIT Imports: 18 Imported by: 0

README

Cachego

Build Status Coverage Status GoDoc Go Report Card License

Simple interface for caching

Installation

Cachego requires Go 1.9 or later.

go get github.com/faabiosr/cachego

If you want to get an specific version, please use the example below:

go get gopkg.in/faabiosr/cachego.v0

Usage Examples

Memcached
package main

import (
    "github.com/faabiosr/cachego"
    "github.com/bradfitz/gomemcache/memcache"
)

var cache cachego.Cache

func init() {
    cache = cachego.NewMemcached(memcached.New("localhost:11211"))
}
Redis
package main

import (
    "github.com/faabiosr/cachego"
    "gopkg.in/redis.v4"
)

var cache cachego.Cache

func init() {
    cache = cachego.NewRedis(
        redis.NewClient(&redis.Options{
            Addr: ":6379",
        }),
    )
}
File
package main

import (
    "github.com/faabiosr/cachego"
)

var cache cachego.Cache

func init() {
    cache = cachego.NewFile(
        "/cache-dir/",
    )
}
Map
package main

import (
    "github.com/faabiosr/cachego"
)

var cache cachego.Cache

func init() {
    cache = NewMap()
}
MongoDB
package main

import (
    "github.com/faabiosr/cachego"
    "gopkg.in/mgo.v2"
)

var cache cachego.Cache

func init() {
    session, _ := mgo.Dial(address)

    cache = cachego.NewMongo(
        session.DB("cache").C("cache"),
    )
}
Sqlite3
package main

import (
	"database/sql"
	_ "github.com/mattn/go-sqlite3"
)

var cache cachego.Cache

func init() {
	db, _ := sql.Open("sqlite3", "./cache.db")

	cache, _ = NewSqlite3(db, "cache")
}
SyncMap
package main

import (
    "github.com/faabiosr/cachego"
)

var cache cachego.Cache

func init() {
    cache = NewSyncMap()
}
BoltDB
package main

import (
    "github.com/faabiosr/cachego"
    bolt "github.com/coreos/bbolt"
)

var cache cachego.Cache

func init() {
    db, _ := bolt.Open("cache.db", 0600, nil)
    cache = NewBolt(db)
}
Chain
package main

import (
    "github.com/faabiosr/cachego"
)

var cache cachego.Cache

func init() {
    memcached := cachego.NewMemcached(
        memcached.New("localhost:11211"),
    )

    redis := cachego.NewRedis(
        redis.NewClient(&redis.Options{
            Addr: ":6379",
        }),
    )

    file := cachego.NewFile(
        "/cache-dir/"
    )

    cache = cachego.NewChain(
        cachego.NewMap(),
        memcached,
        redis,
        file,
    )
}
Usage
package main

import (
    "github.com/faabiosr/cachego"
    "github.com/bradfitz/gomemcache/memcache"
)

func main() {
    cache.Save("foo", "bar")
    cache.Save("john", "doe")

    value, err := cache.Fetch("foo")

    multiple := cache.FetchMulti([]string{"foo", "john"})

    if cache.Contains("foo") {
        cache.Delete("foo")
    }

    cache.Flush()
}

Documentation

Read the full documentation at https://godoc.org/github.com/faabiosr/cachego.

Development

Requirements
Makefile
// Clean up
$ make clean

// Creates folders and download dependencies
$ make configure

//Run tests and generates html coverage file
make cover

// Download project dependencies
make depend

// Up the docker containers for testing
make docker

// Format all go files
make fmt

//Run linters
make lint

// Run tests
make test

License

This project is released under the MIT licence. See LICENSE for more details.

Documentation

Overview

Package cachego provides a simple way to use cache drivers.

Example Usage

The following is a simple example using memcached driver:

import (
  "fmt"
  "github.com/faabiosr/cachego"
  "github.com/bradfitz/gomemcache/memcache"
)

func main() {

  cache := cachego.NewMemcached(
      memcached.New("localhost:11211"),
  )

  cache.Save("foo", "bar")

  fmt.Println(cache.Fetch("foo"))
}

Index

Constants

This section is empty.

Variables

View Source
var (

	// ErrBoltBucketNotFound returns an error when bucket not found
	ErrBoltBucketNotFound = errors.New("Bucket not found")

	// ErrBoltCacheExpired returns an error when the cache key was expired
	ErrBoltCacheExpired = errors.New("Cache expired")

	// ErrBoltDecodeJSON returns json decoding error message
	ErrBoltDecodeJSON = "Unable to decode json data"

	// ErrBoltFlush returns flush error message
	ErrBoltFlush = "Unable to flush"

	// ErrBoltSave returns save error message
	ErrBoltSave = "Unable to save"
)

Functions

This section is empty.

Types

type Bolt

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

Bolt store for caching data

func NewBolt

func NewBolt(db *bolt.DB) *Bolt

NewBolt creates an instance of BoltDB cache

func (*Bolt) Contains

func (b *Bolt) Contains(key string) bool

Contains checks if the cached key exists into the BoltDB storage

func (*Bolt) Delete

func (b *Bolt) Delete(key string) error

Delete the cached key from BoltDB storage

func (*Bolt) Fetch

func (b *Bolt) Fetch(key string) (string, error)

Fetch retrieves the cached value from key of the BoltDB storage

func (*Bolt) FetchMulti

func (b *Bolt) FetchMulti(keys []string) map[string]string

FetchMulti retrieve multiple cached values from keys of the BoltDB storage

func (*Bolt) Flush

func (b *Bolt) Flush() error

Flush removes all cached keys of the BoltDB storage

func (*Bolt) Save

func (b *Bolt) Save(key string, value string, lifeTime time.Duration) error

Save a value in BoltDB storage by key

type BoltContent

type BoltContent struct {
	Duration int64  `json:"duration"`
	Data     string `json:"data,omitempty"`
}

BoltContent it's a structure of cached value

type Cache

type Cache interface {

	// Contains check if a cached key exists
	Contains(key string) bool

	// Delete remove the cached key
	Delete(key string) error

	// Fetch retrieve the cached key value
	Fetch(key string) (string, error)

	// FetchMulti retrieve multiple cached keys value
	FetchMulti(keys []string) map[string]string

	// Flush remove all cached keys
	Flush() error

	// Save cache a value by key
	Save(key string, value string, lifeTime time.Duration) error
}

Cache is the top-level cache interface

type Chain

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

Chain storage for dealing with multiple cache storage in the same time

func NewChain

func NewChain(drivers ...Cache) *Chain

NewChain creates an instance of Chain cache driver

func (*Chain) Contains

func (c *Chain) Contains(key string) bool

Contains checks if the cached key exists in one of the cache storages

func (*Chain) Delete

func (c *Chain) Delete(key string) error

Delete the cached key in all cache storages

func (*Chain) Fetch

func (c *Chain) Fetch(key string) (string, error)

Fetch retrieves the value of one of the registred cache storages

func (*Chain) FetchMulti

func (c *Chain) FetchMulti(keys []string) map[string]string

FetchMulti retrieves multiple cached values from one of the registred cache storages

func (*Chain) Flush

func (c *Chain) Flush() error

Flush removes all cached keys of the registered cache storages

func (*Chain) Save

func (c *Chain) Save(key string, value string, lifeTime time.Duration) error

Save a value in all cache storages by key

type File

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

File store for caching data

func NewFile

func NewFile(dir string) *File

NewFile creates an instance of File cache

func (*File) Contains

func (f *File) Contains(key string) bool

Contains checks if the cached key exists into the File storage

func (*File) Delete

func (f *File) Delete(key string) error

Delete the cached key from File storage

func (*File) Fetch

func (f *File) Fetch(key string) (string, error)

Fetch retrieves the cached value from key of the File storage

func (*File) FetchMulti

func (f *File) FetchMulti(keys []string) map[string]string

FetchMulti retrieve multiple cached values from keys of the File storage

func (*File) Flush

func (f *File) Flush() error

Flush removes all cached keys of the File storage

func (*File) Save

func (f *File) Save(key string, value string, lifeTime time.Duration) error

Save a value in File storage by key

type FileContent

type FileContent struct {
	Duration int64  `json:"duration"`
	Data     string `json:"data,omitempty"`
}

FileContent it's a structure of cached value

type Map

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

Map store the data in memory without external server

func NewMap

func NewMap() *Map

NewMap creates an instance of Map cache driver

func (*Map) Contains

func (m *Map) Contains(key string) bool

Contains checks if cached key exists in Map storage

func (*Map) Delete

func (m *Map) Delete(key string) error

Delete the cached key from Map storage

func (*Map) Fetch

func (m *Map) Fetch(key string) (string, error)

Fetch retrieves the cached value from key of the Map storage

func (*Map) FetchMulti

func (m *Map) FetchMulti(keys []string) map[string]string

FetchMulti retrieves multiple cached value from keys of the Map storage

func (*Map) Flush

func (m *Map) Flush() error

Flush removes all cached keys of the Map storage

func (*Map) Save

func (m *Map) Save(key string, value string, lifeTime time.Duration) error

Save a value in Map storage by key

type MapItem

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

MapItem structure for managing data and lifetime

type Memcached

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

Memcached it's a wrap around the memcached driver

func NewMemcached

func NewMemcached(driver *memcache.Client) *Memcached

NewMemcached creates an instance of Memcached cache driver

func (*Memcached) Contains

func (m *Memcached) Contains(key string) bool

Contains checks if cached key exists in Memcached storage

func (*Memcached) Delete

func (m *Memcached) Delete(key string) error

Delete the cached key from Memcached storage

func (*Memcached) Fetch

func (m *Memcached) Fetch(key string) (string, error)

Fetch retrieves the cached value from key of the Memcached storage

func (*Memcached) FetchMulti

func (m *Memcached) FetchMulti(keys []string) map[string]string

FetchMulti retrieves multiple cached value from keys of the Memcached storage

func (*Memcached) Flush

func (m *Memcached) Flush() error

Flush removes all cached keys of the Memcached storage

func (*Memcached) Save

func (m *Memcached) Save(key string, value string, lifeTime time.Duration) error

Save a value in Memcached storage by key

type Mongo

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

Mongo it's a wrap around the mgo driver

func NewMongo

func NewMongo(collection *mgo.Collection) *Mongo

NewMongo creates an instance of Mongo cache driver

func (*Mongo) Contains

func (m *Mongo) Contains(key string) bool

Contains checks if cached key exists in Mongo storage

func (*Mongo) Delete

func (m *Mongo) Delete(key string) error

Delete the cached key from Mongo storage

func (*Mongo) Fetch

func (m *Mongo) Fetch(key string) (string, error)

Fetch retrieves the cached value from key of the Mongo storage

func (*Mongo) FetchMulti

func (m *Mongo) FetchMulti(keys []string) map[string]string

FetchMulti retrieves multiple cached value from keys of the Mongo storage

func (*Mongo) Flush

func (m *Mongo) Flush() error

Flush removes all cached keys of the Mongo storage

func (*Mongo) Save

func (m *Mongo) Save(key string, value string, lifeTime time.Duration) error

Save a value in Mongo storage by key

type MongoContent

type MongoContent struct {
	Duration int64
	Key      string `bson:"_id"`
	Value    string
}

MongoContent it's a bson structure of cached value

type Redis

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

Redis it's a wrap around the redis driver

func NewRedis

func NewRedis(driver redis.BaseCmdable) *Redis

NewRedis creates an instance of Redis cache driver

func (*Redis) Contains

func (r *Redis) Contains(key string) bool

Contains checks if cached key exists in Redis storage

func (*Redis) Delete

func (r *Redis) Delete(key string) error

Delete the cached key from Redis storage

func (*Redis) Fetch

func (r *Redis) Fetch(key string) (string, error)

Fetch retrieves the cached value from key of the Redis storage

func (*Redis) FetchMulti

func (r *Redis) FetchMulti(keys []string) map[string]string

FetchMulti retrieves multiple cached value from keys of the Redis storage

func (*Redis) Flush

func (r *Redis) Flush() error

Flush removes all cached keys of the Redis storage

func (*Redis) Save

func (r *Redis) Save(key string, value string, lifeTime time.Duration) error

Save a value in Redis storage by key

type Sqlite3

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

Sqlite3 it's a wrap around the sqlite3 driver

func NewSqlite3

func NewSqlite3(db *sql.DB, table string) (*Sqlite3, error)

NewSqlite3 creates an instance of Sqlite3 cache driver

func (*Sqlite3) Contains

func (s *Sqlite3) Contains(key string) bool

Contains checks if cached key exists in Sqlite3 storage

func (*Sqlite3) Delete

func (s *Sqlite3) Delete(key string) error

Delete the cached key from Sqlite3 storage

func (*Sqlite3) Fetch

func (s *Sqlite3) Fetch(key string) (string, error)

Fetch retrieves the cached value from key of the Sqlite3 storage

func (*Sqlite3) FetchMulti

func (s *Sqlite3) FetchMulti(keys []string) map[string]string

FetchMulti retrieves multiple cached value from keys of the Sqlite3 storage

func (*Sqlite3) Flush

func (s *Sqlite3) Flush() error

Flush removes all cached keys of the Sqlite3 storage

func (*Sqlite3) Save

func (s *Sqlite3) Save(key string, value string, lifeTime time.Duration) error

Save a value in Sqlite3 storage by key

type SyncMap

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

SyncMap store the data in memory without external server

func NewSyncMap

func NewSyncMap() *SyncMap

NewSyncMap creates an instance of SyncMap cache driver

func (*SyncMap) Contains

func (sm *SyncMap) Contains(key string) bool

Contains checks if cached key exists in SyncMap storage

func (*SyncMap) Delete

func (sm *SyncMap) Delete(key string) error

Delete the cached key from SyncMap storage

func (*SyncMap) Fetch

func (sm *SyncMap) Fetch(key string) (string, error)

Fetch retrieves the cached value from key of the SyncMap storage

func (*SyncMap) FetchMulti

func (sm *SyncMap) FetchMulti(keys []string) map[string]string

FetchMulti retrieves multiple cached value from keys of the SyncMap storage

func (*SyncMap) Flush

func (sm *SyncMap) Flush() error

Flush removes all cached keys of the SyncMap storage

func (*SyncMap) Save

func (sm *SyncMap) Save(key string, value string, lifeTime time.Duration) error

Save a value in SyncMap storage by key

type SyncMapItem

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

SyncMapItem structure for managing data and lifetime

Jump to

Keyboard shortcuts

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