Skip to content

[deprecated] smart cache support memory insert, queryable and can add hook to remote cache save.

License

Notifications You must be signed in to change notification settings

teng231/smartcache

Repository files navigation

SmartCache

Upgrade from my dumbcache

Go Reference Go

Hope it can be tool to help your works.

Installation

go get github.com/teng231/smartcache

How it work?

flow like that:

┌────────┐       ┌────────┐      ┌───────────────┐
│        │       │        │      │               │
│ Engine ├─────►│ Session├─────►│   Collection  │
│        │       │        │      │               │
└────────┘       └────────┘      └───────────────┘
  • When start app you create engine. Engine define structure of collection.
  • Engine provider select to access module cache.
  • Session process cache or set, get value to cache, from local module or remote module
  • Collection local cache module

Example

You can see full example here: Example

cache := smartcache.Start(
    &smartcache.CollectionConfig{
        Key:      "key1",
        Capacity: 100,
    },
)

if err := cache.Select(context.TODO(), "key1").Upsert("abc", 1); err != nil {
    log.Print(err)
}
var out1 int
cache.Select(context.TODO(), "key1").Get("abc", nil).Exec(&out1)
log.Print(out1)

Benmark Result:

follow link https://www.cloudbees.com/blog/real-life-go-benchmarking

$ go test -cpuprofile=cpu.out -benchmem -memprofile=mem.out -bench=BenchmarkEngineCacheWrite -run=^a

goos: linux
goarch: amd64
pkg: github.com/teng231/smartcache
cpu: Intel(R) Core(TM) i5-10400 CPU @ 2.90GHz
BenchmarkEngineCacheWrite/input_size_100-12         	1000000000	         0.0000460 ns/op	       0 B/op	       0 allocs/op
BenchmarkEngineCacheWrite/input_size_1000-12        	1000000000	         0.0004365 ns/op	       0 B/op	       0 allocs/op
BenchmarkEngineCacheWrite/input_size_74382-12       	1000000000	         0.05490 ns/op	       0 B/op	       0 allocs/op
PASS
ok  	github.com/teng231/smartcache	0.513s
$ go test -cpuprofile=cpu.out -benchmem -memprofile=mem.out -bench=BenchmarkEngineCachex100000W10g -run=^a

goos: linux
goarch: amd64
pkg: github.com/teng231/smartcache
cpu: Intel(R) Core(TM) i5-10400 CPU @ 2.90GHz
BenchmarkEngineCachex100000W10g-12    	1000000000	         0.008111 ns/op	       0 B/op	       0 allocs/op
PASS
ok  	github.com/teng231/smartcache	0.229s

Contributed: Quang Anh

About

[deprecated] smart cache support memory insert, queryable and can add hook to remote cache save.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published