Skip to content
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

toJSON() mutates object in cache #6

Open
MZanggl opened this issue Jan 30, 2019 · 1 comment
Open

toJSON() mutates object in cache #6

MZanggl opened this issue Jan 30, 2019 · 1 comment

Comments

@MZanggl
Copy link

MZanggl commented Jan 30, 2019

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 MZanggl changed the title toJSON() mutates object in cache toJSON() mutates object in cache Jan 30, 2019
@helnokaly
Copy link
Owner

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants