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

db or cache? #51

Open
jahluwalia opened this issue Mar 26, 2019 · 2 comments
Open

db or cache? #51

jahluwalia opened this issue Mar 26, 2019 · 2 comments

Comments

@jahluwalia
Copy link

Is there a way for us to know if it retrieved the data from the database or from the cache?

This would be useful to know.

Also, i have some queries that use aggregate. I notice that if i specify different match criteria (for example query 1 is looking for name: bob and query 2 is looking for name: jim), I get the same results as the first query because they had the same cachekey. How can we get around this? I could make a long cachekey that contains all the attributes of the query, but that's not ideal. And then am i to invalidate the whole cache everytime a new item is added or removed?

Any insights would be appreciated.

Thanks,
Jas

@boblauer
Copy link
Owner

boblauer commented Apr 1, 2019

Is there a way for us to know if it retrieved the data from the database or from the cache?

Unfortunately there is not. It would be possible to include an extra property in some scenarios, but for example, if you're caching the count, all you get back is a number, so there's no way to add any extra info to that number to denote if it was from the cache or not.

How can we get around this? I could make a long cachekey that contains all the attributes of the query, but that's not ideal.

If your two queries have different match criteria, they should generate different cache keys. Can you include the code for your two queries here?

@jahluwalia
Copy link
Author

Hi Bob,

Below is what my query looks like. I've removed teh cache line.

 mongoReply = await item.aggregate([
                    {
                        $match: matchQuery
                    },
                    {
                        $project: {
                            _id: {$toString: '$_id'},
                            attributes: {
                                $filter: {
                                    input: '$attributes',
                                    as: 'attribute',
                                    cond: {
                                        $or : condArray
                                    }
                                }
                            }
                        }
                    },
                    {
                        $project: {
                            'attributes._id': 0
                        }
                    }
                ]);

The condArray is an array of objects that look something like this:
{ '$eq': [ '$$attribute.name', myAttribute] }

If we do get this working somehow, won't i need to invalidate the whole cache when i add or delete items?

thanks,
jas

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