Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
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
- Host: GitHub
- URL: https://github.com/morkid/gocache
- Owner: morkid
- License: mit
- Created: 2021-03-06T14:20:06.000Z (almost 4 years ago)
- Default Branch: master
- Last Pushed: 2021-03-08T03:53:38.000Z (almost 4 years ago)
- Last Synced: 2024-10-19T16:49:37.120Z (3 months ago)
- Language: Go
- Size: 11.7 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
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).