Imgocache - Simple In-memory key-value cache with sync.RWMutex in Go.
- The Imgocache type is a structure with an embedded sync.RWMutex and a map db to store key-value pairs, likely used for an image cache.
- The type SM is a wrapper for the sync.Map type, ensuring safe concurrent read-write operations.
See it in action:
package main
import (
"fmt"
"github.com/apetsko/imgocache"
)
func main(){
var icache imgocache.Cacher
icache = imgocache.New() // Creating a variable of type interface and assigning it a value of type Imgocache
icache.Set("userId", 42, time.Second*5)
userID, exist := icache.Get("userId")
fmt.Println(userID, exist)
icache.Delete("userId")
userID, exist = icache.Get("userId")
fmt.Println(userID, exist)
}
$ go run .
42 true
<nil> false
package main
import (
"fmt"
"github.com/apetsko/imgocache"
)
func main() {
var icache imgocache.Cacher
icache = imgocache.NewSM() // Creating a variable of type interface and assigning it a value of type Imgocache
icache.Set("userId", 42, time.Second*5)
userID, exist := icache.Get("userId")
fmt.Println(userID, exist)
icache.Delete("userId")
userID, exist = icache.Get("userId")
fmt.Println(userID, exist)
}
$ go run .
42 true
<nil> false