patrickmn / go-cache

An in-memory key:value store/cache (similar to Memcached) library for Go, suitable for single-machine applications.
https://patrickmn.com/projects/go-cache/
MIT License
8.16k stars 874 forks source link

Ever make a typed cache? #85

Closed cjcjameson closed 6 years ago

cjcjameson commented 6 years ago

Hi there, hearing about the project for the first time. I can see how powerful an un-typed cache would be. I'm just curious if folks have ever looked into making the cache typed -- you pass the type in as an argument at the beginning, and there's some pro-flection (anti-reflection) / metaprogramming under the hood.

Or maybe wrapper methods on look-up that enforce type-safety? Either way, something that reduces the burden on the caller to cast their looked-up values. Maybe if a use-case wanted type-safety, they'd implement their own wrapper method.

larytet commented 6 years ago

Reflection is rather costly and wont fit many applications performance requirements. Choice between reflection vs type assert is a no brainer.

I am thinking about unsafe pointers. Cast is probably cheaper. See, for example, https://golang.org/pkg/runtime/?m=all#noescape

A naive implementation of a memory pool https://github.com/larytet/mcachego/blob/master/unsafepool/unsafepool_test.go