cachego

package module
v0.10.2 Latest Latest
Warning

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

Go to latest
Published: Mar 10, 2020 License: MIT Imports: 15 Imported by: 32

README

Cachego

Build Status Codecov branch 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

//Run tests and generates html coverage file
make cover

// 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

View Source
const (
	// ErrCacheExpired returns an error when the cache key was expired.
	ErrCacheExpired = err("cache expired")

	// ErrFlush returns an error when flush fails.
	ErrFlush = err("unable to flush")

	// ErrSave returns an error when save fails.
	ErrSave = err("unable to save")

	// ErrDelete returns an error when deletion fails.
	ErrDelete = err("unable to delete")

	// ErrDecode returns an errors when decode fails.
	ErrDecode = err("unable to decode")
)
View Source
const ErrBoltBucketNotFound = err("Bucket not found")

ErrBoltBucketNotFound returns an error when bucket not found

View Source
const ErrFileOpen = err("unable to open file")

ErrFileOpen returns an error when try to open a file.

View Source
const ErrMapKeyNotFound = err("key not found")

ErrMapNotFound returns an error when the key is not found.

View Source
const ErrSyncMapKeyNotFound = err("key not found")

ErrSyncMapNotFound returns an error when the key is not found.

Variables

This section is empty.

Functions

func Wrap added in v0.9.0

func Wrap(err, additionalErr error) error

Types

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

func NewBolt

func NewBolt(db *bt.DB) Cache

NewBolt creates an instance of BoltDB cache

func NewChain

func NewChain(drivers ...Cache) Cache

NewChain creates an instance of Chain cache driver

func NewFile

func NewFile(dir string) Cache

NewFile creates an instance of File cache

func NewMap

func NewMap() Cache

NewMap creates an instance of Map cache driver

func NewMemcached

func NewMemcached(driver *memcache.Client) Cache

NewMemcached creates an instance of Memcached cache driver

func NewMongo

func NewMongo(collection *mgo.Collection) Cache

NewMongo creates an instance of Mongo cache driver

func NewRedis

func NewRedis(driver rd.BaseCmdable) Cache

NewRedis creates an instance of Redis cache driver

func NewSqlite3

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

NewSqlite3 creates an instance of Sqlite3 cache driver

func NewSyncMap

func NewSyncMap() Cache

NewSyncMap creates an instance of SyncMap cache driver

Jump to

Keyboard shortcuts

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