A thread-safe LRU cache implemented in Golang.
import (
"fmt"
"github.com/RGBli/MyCache"
)
func main() {
// initialize a cache instance
cache := MyCache.New(1 * 1024 * 1024)
// use database "test"
db := cache.Use("test")
// put value in cache
key := "lbw"
value := NewString("23")
db.Set(key, value)
// get value from cache
v, ok := db.GetString(key)
fmt.Println(v, ok)
}
map | MyCache | |
---|---|---|
thread-safe | no | yes |
maximum-capacity | no | yes |
scalable | no | yes |
TTL | no | yes |
value types | implement by yourself | 5 common implemented types |
One thing to notice. RWLock is applied to mycache, so mycache works not as fast as map in Golang, but the gap is not that clear. You can run benchmark_test.go to get more information about it.
1)Data persistence