toJSON() mutates object in cache
Simple example:
const user = await Cache.remember('cachekey', 1000, async () => {
return await User.find(1)
})
return response.json({ user: user.toJSON() })
Problem
As soon as I call user.toJSON(), it mutates the user object in the cache as well. Next time this code runs await Cache.remember will not return the model, but simply the toJSON-converted array.
Expected behaviour The object stored in the cache should be immutable.
I am using the object cache store.
Thanks for reporting this.
I wish this library could meet your expected behavior, but I can't deserialize back Class objects to it original form. So what you get is what you would expect from JSON.parse(JSON.stringify(user))
What can I guarantee now if you update the package, is that you get the same value on first run of Cache.remember when value is not cached yet.
@MZanggl