adonis-cache icon indicating copy to clipboard operation
adonis-cache copied to clipboard

toJSON() mutates object in cache

Open MZanggl opened this issue 7 years ago • 1 comments

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.

MZanggl avatar Jan 30 '19 13:01 MZanggl

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

helnokaly avatar Feb 17 '19 02:02 helnokaly