Cachita
Cachita is a golang file and memory cache library
- Simple caching with auto type assertion included.
- In memory file cache index to avoid unneeded I/O.
- Msgpack based binary serialization using msgpack library for file caching.
API docs: https://godoc.org/github.com/gadelkareem/cachita.
Examples: https://godoc.org/github.com/gadelkareem/cachita#pkg-examples.
Installation
Install:
go get -u github.com/gadelkareem/cachita
Quickstart
func ExampleCache() {
cache := cachita.Memory()
err := cache.Put("cache_key", "some data", 1*time.Minute)
if err != nil {
panic(err)
}
if cache.Exists("cache_key") {
//do something
}
var holder string
err = cache.Get("cache_key", &holder)
if err != nil && err != cachita.ErrNotFound {
panic(err)
}
fmt.Printf("%s", holder) //prints "some data"
err = cache.Invalidate("cache_key")
if err != nil {
panic(err)
}
//Output: some data
}
Benchmark
> go test -v -bench=. -benchmem
BenchmarkMemoryCacheWithInt-8 300000 5434 ns/op 729 B/op 14 allocs/op
BenchmarkMemoryCacheWithString-8 300000 5489 ns/op 757 B/op 14 allocs/op
BenchmarkMemoryCacheWithMapInterface-8 300000 5819 ns/op 1125 B/op 16 allocs/op
BenchmarkMemoryCacheWithStruct-8 200000 6523 ns/op 1321 B/op 21 allocs/op
BenchmarkFileCacheWithInt-8 300000 5423 ns/op 729 B/op 14 allocs/op
BenchmarkFileCacheWithString-8 5000 362113 ns/op 3812 B/op 55 allocs/op
Howto
Please go through examples to get an idea how to use this package.