Skip to content

morkid/gocache

Repository files navigation

gocache - simple key value cache adapter for golang

Go Reference Go Build Status Go Report Card GitHub release (latest SemVer)

Installation

go get -d github.com/morkid/gocache

In Memory cache example

package main
import (
    "time"
    "fmt"
    "github.com/morkid/gocache"
)

func main() {
    config := gocache.InMemoryCacheConfig{
        ExpiresIn: 10 * time.Second,
    }

    adapter := *gocache.NewInMemoryCache(config)
    adapter.Set("foo", "bar")

    if adapter.IsValid("foo") {
        value, err := adapter.Get("foo")
        if nil != err {
            fmt.Println(err.Error())
        } else if value != "bar" {
            fmt.Println("value not equals to bar")
        }
        adapter.Clear("foo")
    }
}

Disk cache example

package main
import (
    "os"
    "time"
    "fmt"
    "github.com/morkid/gocache"
)

func main() {
    config := gocache.DiskCacheConfig{
        Directory: os.TempDir(),
        ExpiresIn: 10 * time.Second,
    }

    adapter := *gocache.NewDiskCache(config)
    adapter.Set("foo", "bar")

    if adapter.IsValid("foo") {
        value, err := adapter.Get("foo")
        if nil != err {
            fmt.Println(err.Error())
        } else if value != "bar" {
            fmt.Println("value not equals to bar")
        }
        adapter.Clear("foo")
    }
}

Custom cache adapter

You can create your custom cache adapter by implementing the AdapterInterface:

type AdapterInterface interface {

    Set(key string, value string) error

    Get(key string) (string, error)
    
    IsValid(key string) bool
    
    Clear(key string) error
    
    ClearPrefix(keyPrefix string) error
    
    ClearAll() error
}

License

Published under the MIT License.

About

Simple key value cache adapter for golang

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages