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

How to cache queries? #22

Open
oander11 opened this issue Feb 16, 2021 · 0 comments
Open

How to cache queries? #22

oander11 opened this issue Feb 16, 2021 · 0 comments

Comments

@oander11
Copy link

Hi,
this is for sure not an issue, just me not knowing how to use the lib correctly but not sure where to ask besides here... hope it's ok. For some reason I have no problem getting keys (gck:xxx) into Redis but cannot get queries (gcq:xxx) to be cached.
(Part of) my test code is as follows:

const NsqlCache = require('nsql-cache'); 
const dsAdapter = require('nsql-cache-datastore'); 
const redisStore = require('cache-manager-redis-store');
const {Datastore} = require('@google-cloud/datastore');
const datastore = new Datastore();

const REDISHOST = process.env.REDISHOST || 'localhost';
const REDISPORT = process.env.REDISPORT || 6379;

const cache = new NsqlCache({
    db: dsAdapter(datastore),
    stores: [{
        store: redisStore,
        host: REDISHOST, 
        port: REDISPORT
    }],
    config: {
        ttl: {
            keys: 60 * 60, // 1 hour
            queries: 0// infinite
        }
    },
});

ProviderController.list = async function (req, res) {
    try {
       const providerKey = datastore.key(['V2Provider', '04cb463f-e567-41da-810c-6b918b0130a1']);
       const query = datastore.createQuery('V2Provider');
       await datastore.get(providerKey); // works fine, stored in Redis DB
       const [provs] = await datastore.runQuery(query); //query runs fine but nothing stored in Redis :-(
       console.log(provs.length); //60 or so objects, not too much...
        res.send({testrun:'ok'});
    } catch (error) {
        console.log(error);
    }
};

I've been through all documentation (afaik) but cannot find what is missing here, so any help would be appreciated!

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

1 participant