30

GitHub - fabiorphp/cachego: Golang Cache component - Multiple drivers

 6 years ago
source link: https://github.com/fabiorphp/cachego
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

README.md

Cachego

Build Status Coverage Status GoDoc Go Report Card License

Simple interface for caching

Installation

Cachego requires Go 1.8 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")
}

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

Run tests

// tests
$ make test

// test with coverage
$ make test-coverage

// clean-up
$ make clean

// configure (download dependencies and run docker containers)
$ make configure

License

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


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK