https://github.com/oogway/go-cache
Cache Interface + Implementation
https://github.com/oogway/go-cache
cache golang idempotency redis
Last synced: about 1 year ago
JSON representation
Cache Interface + Implementation
- Host: GitHub
- URL: https://github.com/oogway/go-cache
- Owner: oogway
- Created: 2018-05-07T01:26:50.000Z (about 8 years ago)
- Default Branch: master
- Last Pushed: 2018-06-04T12:05:30.000Z (about 8 years ago)
- Last Synced: 2025-04-16T22:09:09.601Z (about 1 year ago)
- Topics: cache, golang, idempotency, redis
- Language: Go
- Size: 26.4 KB
- Stars: 3
- Watchers: 5
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# go-cache [](https://travis-ci.org/oogway/go-cache)
This repository is a hard-fork of [revel-cache](https://github.com/revel/revel/tree/master/cache)
Motivation to do this:
- light-weight cache library that doesn't download the internet.
- Doesn't use background go-routines for expiration.
- Has both in-memory, Redis and more (to be introduced) backends.
## Quick Start
Installation
$ go get github.com/oogway/go-cache
## Usage
### In-memory
Example:
```go
package main
import (
"fmt"
"time"
cache "github.com/oogway/go-cache"
)
func main() {
// Use in-memory store
store := cache.NewInMemoryCache(time.Hour)
store.Set("key", "value", time.Hour)
var value string
// Set the item
store.Get("key", &value)
fmt.Println("Key:", value)
store.Set("num", 1, time.Hour)
var num int
// Well, lets check it has set correct
store.Get("num", &num)
fmt.Println("Number: ", num)
// No longer need the item in store? DELETE IT!
store.Delete("key")
// Update value
// NOTE: Replace only works iff the key exists in store
store.Replace("num", 100, time.Hour)
store.Get("num", &num)
fmt.Println("Replaced Number: ", num)
// Get rid of all keys at once
store.Flush()
}
```
### Redis
For Redis store just initialize store as follows
```go
store := cache.NewRedisCache(cache.RedisOpts{
Host: "",
Expiration: time.Hour,
})
```
Empty host assumes redis service on local machine (`localhost:6379`)
Following are the options while initializing Redis store
```
MaxIdle int
MaxActive int
Protocol string
Host string
Password string
Expiration time.Duration
TimeoutConnect int
TimeoutRead int
TimeoutWrite int
TimeoutIdle int
```