New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
make Marshaler and Loadable work together #72
Comments
I faced a similar problem when trying to use Marshal with Loadable for Redis, they both do not work together. I exteded Loadable with Marshal, but I'm not sure if it is the correct way to do that. package cache
import (
"github.com/vmihailenco/msgpack"
)
type Marshaler struct {
LoadableCache
}
func NewMarshaler(loadFunc loadFunction, cache CacheInterface) *Marshaler {
m := Marshaler{}
loadable := &LoadableCache{
loadFunc: loadFunc,
cache: cache,
}
m.LoadableCache = *loadable
m.setChannel = make(chan *loadableKeyValue, 10000)
go m.setter()
return &m
}
func (c *Marshaler) setter() {
for item := range c.setChannel {
bytes, err := msgpack.Marshal(item.value)
if err != nil {
continue
}
c.Set(item.key, bytes, nil)
}
}
// Get obtains a value from cache and unmarshal value with given object
func (m *Marshaler) Get(key interface{}, returnObj interface{}) (interface{}, error) {
result, err := m.cache.Get(key)
if err != nil {
// Unable to find in cache, try to load it from load function
result, err = m.loadFunc(key)
if err != nil {
return nil, err
}
// Then, put it back in cache
m.setChannel <- &loadableKeyValue{key, result}
return result, nil
}
switch v := result.(type) {
case []byte:
err = msgpack.Unmarshal(v, returnObj)
case string:
err = msgpack.Unmarshal([]byte(v), returnObj)
}
if err != nil {
return nil, err
}
return returnObj, nil
} |
my solution is make load function return a bc, err := bigcache.NewBigCache(bigcache.DefaultConfig(time.Minute))
if err != nil {
panic(err)
}
return marshaler.New(cache.NewLoadable(func(key interface{}) (interface{}, error) {
value, err := loadFunction(key)
if err != nil {
return nil, err
}
return msgpack.Marshal(value)
}, cache.New(store.NewBigcache(bc, nil)))), nil but it's depend on that |
Now that Loadable cache support generics, I think we should make Marshaler support for generics too. |
nice workaround. Although this couples the loader function with the marshaling process, which isn't ideal. Ideally, loader function should just return (struct, error), nothing less nothing more. @okhowang
Any timeline we can expect this to be supported? Also can we support |
I use gocache as a cachable mysql wrapper.
but there is panic as below
Loadable
set value directly to bigcache which accept[]byte
only.make
Marshaler
asCacheInterface
may be ok.The text was updated successfully, but these errors were encountered: