Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/morkid/gocache

Simple key value cache adapter for golang
https://github.com/morkid/gocache

Last synced: about 1 month ago
JSON representation

Simple key value cache adapter for golang

Awesome Lists containing this project

README

        

# gocache - simple key value cache adapter for golang
[![Go Reference](https://pkg.go.dev/badge/github.com/morkid/gocache.svg)](https://pkg.go.dev/github.com/morkid/gocache)
[![Go](https://github.com/morkid/gocache/actions/workflows/go.yml/badge.svg)](https://github.com/morkid/gocache/actions/workflows/go.yml)
[![Build Status](https://travis-ci.com/morkid/gocache.svg?branch=master)](https://travis-ci.com/morkid/gocache)
[![Go Report Card](https://goreportcard.com/badge/github.com/morkid/gocache)](https://goreportcard.com/report/github.com/morkid/gocache)
[![GitHub release (latest SemVer)](https://img.shields.io/github/v/release/morkid/gocache)](https://github.com/morkid/gocache/releases)

## Installation
```bash
go get -d github.com/morkid/gocache
```

## In Memory cache example
```go
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
```go
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`:

```go
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](https://github.com/morkid/gocache/blob/master/LICENSE).