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

Data cache leaking? #5

Closed
bramus opened this issue Dec 7, 2012 · 4 comments
Closed

Data cache leaking? #5

bramus opened this issue Dec 7, 2012 · 4 comments

Comments

@bramus
Copy link
Contributor

bramus commented Dec 7, 2012

Consider these models:

var Movie = db.define('movies', {
    id: Number,
    title: String,
    year: Number,
    rating: Number
});

var Genre = db.define('genres', {
    id: Number,
    title: String
});

Movie.hasMany('genres', Genre); // link genres, no autofetch

When calling this piece of code (I'll name it snippet A, as I'll refer back to it later on) ...

Movie.find({}, 100, function(err, movies) {
    // ...
}

... and then outputting movies on screen in the callback you get this dataset for example:

[
    {
        "id": 1,
        "title": "Futurama: Bender's Big Score",
        "year": 2007,
        "rating": 7.6
    },
    {
        "id": 2,
        "title": "Futurama: The Beast with a Billion Backs",
        "year": 2008,
        "rating": 7.2,
    }
]

When then calling a different URL on the app to fetch one single movie with genres ...

Movie.get(id, function(err, movie) {
    movie.getGenres(function(err, genres) {
        movie.genres = genres;
        // ...
    }
});

... we get back this when outputting movie from within the second callback

{
    "id": 1,
    "title": "Futurama: Bender's Big Score",
    "year": 2007,
    "rating": 7.6,
    "genres": [
        {"id": 1, "title: "Animation"},
        {"id": 2, "title: "Action"},
        {"id": 3, "title: "Comedy"}
    ]
}

Now comes the kicker: when now visiting the URL that executes snippet A again, you get back this dataset:

[
    {
        "id": 1,
        "title": "Futurama: Bender's Big Score",
        "year": 2007,
        "rating": 7.6,
        "genres": [
            {"id": 1, "title: "Animation"},
            {"id": 2, "title: "Action"},
            {"id": 3, "title: "Comedy"}
        ]
    },
    {
        "id": 2,
        "title": "Futurama: The Beast with a Billion Backs",
        "year": 2008,
        "rating": 7.2,
    }
]

As you can see the genres mysteriously appear here now, which shouldn't be the case.

@dresende
Copy link
Owner

dresende commented Dec 9, 2012

Hmmm... interesting.. I never thought about that. I try to use singletons when fetching data to avoid having 2 instances in memory and changing one and the other getting outdated.. the only way of avoiding this is having an option to disable singletons.

@dresende
Copy link
Owner

You can now do this for the entire Model or for a specific Model.get() or Model.find().

Examples:

var Person = db.define('person', {
    // ...
}, {
    cache: false // disable globally
});

Person.find({ ... }, { cache: false }, cb); // disable locally

I still have to check the associations but I think that's simple now.

@dresende
Copy link
Owner

Associations for now are affected by the global parameter.

@bramus
Copy link
Contributor Author

bramus commented Dec 11, 2012

Tested and verified, thanks!

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