ramiel / mikro-orm-cache-adapter-redis

A redis cache adapter for mikro-orm
MIT License
17 stars 5 forks source link

Difiiculty storing cache #2

Closed amtarunsingh closed 2 years ago

amtarunsingh commented 2 years ago

I have made the connecting and updated init config in the in the mikrorm.init ({}). but my cache is not reflecting in the redis UI, is there anything that I have to do at the time of finding?

My cache config:

resultCache: {
            adapter: RedisCacheAdapter,
            options: {
              client: redis
            }
        }
ramiel commented 2 years ago

Can you check if you're using version 2.0.0? Previous version had a bug

amtarunsingh commented 2 years ago

The version is 2.0.0.

ramiel commented 2 years ago

Do you think you can provide a reproduction? A repo or some simple code? Without more information I don't know what's going on there.
Can you also explain more what do you mean by

cache is not reflecting in the redis UI

amtarunsingh commented 2 years ago

this is how am connecting: const { MikroORM } = require('@mikro-orm/core/MikroORM'); const {RedisCacheAdapter} = require('mikro-orm-cache-adapter-redis') const Redis = require('ioredis');

let redisClient = new Redis('MY_REDIS_STRING_HERE'); const config = { resultCache: { enable:true, adapter: RedisCacheAdapter, options: { client: redisClient } } };

const orm = await MikroORM.init(config)

Do i need to do anything else apart from this in the find() method of mikrorm?

currently my find method is like : let [products, count] = await orm.em.findAndCount(Product, findQuery, paginationQuery); Product is the name of my entity(repo).

amtarunsingh commented 2 years ago

but am not able to see the data in my Redis database. is there something am doing wrong or missing out something?

ramiel commented 2 years ago

Oh yes, you have to tell MikroORM you want to cache results: https://mikro-orm.io/docs/caching

amtarunsingh commented 2 years ago

await orm.em.findAndCount(Product, findQuery, paginationQuery, {cache : true}); Like This?

ramiel commented 2 years ago

Yes. Consider the default cache time is 1 second, so you may still miss it on redis UI if you're not fast enough

amtarunsingh commented 2 years ago

I have changed my find query to this : let [products, count] = await coll.findAndCount(query, paginationQuery, {cache:true});

and has increased the cache time to 100000:

resultCache: {
        enable:true,
        expiration:100000,
        adapter: RedisCacheAdapter,
        options: {
          client: redisClient
        }
    }

but still the data is not visible in the redis database.

ramiel commented 2 years ago

but still the data is visible in the redis database.

Do you mean is NOT visible?

Anyway, I think I need some reproduction code

amtarunsingh commented 2 years ago

mikrormCache.zip Sir this is the code. Please have a look.

ramiel commented 2 years ago

Hello. I finally had tie to look at your code. I found the error

let [products, count] = await coll.findAndCount(query,paginationQuery,{
      cache: true,
    });

is wrong, there's not a third option. The correct signature of the function is

let [products, count] = await coll.findAndCount(query, {
      ...paginationQuery,
      cache: true,
    });

You should use typescript to avoid these errors

ramiel commented 2 years ago

I also released a new version of the cache adapter that can be used with mikro-orm v5

ramiel commented 2 years ago

Also, if you want to see what the cache is doing, you can pass debug: true to the options

amtarunsingh commented 2 years ago

Thank you .