Golang Cache component - Multiple drivers
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
.gitignore Support files Mar 14, 2018
.travis.yml feat: Removed the old golang version May 19, 2018
CONTRIBUTING.md fix: Golang version May 19, 2018
Gopkg.lock feat: BoltDB support May 20, 2018
Gopkg.toml feat: BoltDB support May 20, 2018
LICENSE Initial Commit Oct 5, 2016
Makefile
README.md feat: BoltDB support May 20, 2018
bolt.go feat: BoltDB support May 20, 2018
bolt_test.go feat: BoltDB support May 20, 2018
cache.go Improve linter and format fixes Mar 21, 2018
chain.go Improve linter and format fixes Mar 21, 2018
chain_test.go Improve linter and format fixes Mar 21, 2018
doc.go Improve linter and format fixes Mar 21, 2018
docker-compose.yml Mongo support Nov 5, 2016
file.go Improve linter and format fixes Mar 21, 2018
file_test.go Improve linter and format fixes Mar 21, 2018
map.go
map_test.go Improve linter and format fixes Mar 21, 2018
memcached.go Improve linter and format fixes Mar 21, 2018
memcached_test.go Improve linter and format fixes Mar 21, 2018
mongo.go Improve linter and format fixes Mar 21, 2018
mongo_test.go Improve linter and format fixes Mar 21, 2018
redis.go Improve linter and format fixes Mar 21, 2018
redis_test.go
sqlite3.go Improve linter and format fixes Mar 21, 2018
sqlite3_test.go update: Improve makefile commands and fix lint May 19, 2018
sync_map.go feat: Sync Map support May 19, 2018
sync_map_test.go feat: Sync Map support May 19, 2018

README.md

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

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

go get gopkg.in/fabiorphp/cachego.v0

Usage Examples

Memcached

package main

import (
    "github.com/fabiorphp/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/fabiorphp/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/fabiorphp/cachego"
)

var cache cachego.Cache

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

Map

package main

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

var cache cachego.Cache

func init() {
    cache = NewMap()
}

MongoDB

package main

import (
    "github.com/fabiorphp/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/fabiorphp/cachego"
)

var cache cachego.Cache

func init() {
    cache = NewSyncMap()
}

BoltDB

package main

import (
    "github.com/fabiorphp/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/fabiorphp/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/fabiorphp/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/fabiorphp/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.