#CacheModule isGlobal is not working

1 messages · Page 1 of 1 (latest)

modest sphinx
#

So, I have a globally defined Redis cache manager in the apps main module:

@Module({
  imports: [
    ConfigModule.forRoot(),
    DbModule.forRoot(),
    CacheModule.registerAsync({
      imports: [ConfigModule.forRoot()],
      useFactory: async (
        configService: ConfigService,
      ): Promise<CacheModuleOptions> => ({
        isGlobal: true,
        ttl: parseInt(configService.get('CACHE_DEFAULT_TTL', '3600')),
        // eslint-disable-next-line @typescript-eslint/ban-ts-comment
        // @ts-ignore
        store: async () =>
          await redisStore({
            // Store-specific configuration:
            url: configService.get('REDIS_URL', 'redis://localhost:6379'),
          }),
      }),
      inject: [ConfigService],
    }),
    RandomModule,
  ],
  providers: [Logger, ConfigService, RandomService],
})

And then I have the RandomModule that has services that should use this global cache, and per instructions, I added the CacheModule to the imports there <- see below

@Module({
  imports: [ConfigModule.forRoot(), DbModule.forRoot(), CacheModule.register()],
  providers: [
    ConfigService,
    Logger,
  ],
  exports: [
    RandomService,
  ],
})

This runs fine, but as it turns out, the cache instance in the RandomModule is not the same as the one in the main app module. The one in the main app, used the Redis cache, the one in the RandomModule does not, it's just a memory cache.

If as, as per instructions for global usage, do not import the CacheModule, I get a complaint that the CACHE_MANAGER cannot be resolved... So that doesn't work either.

So, how do I do this?

jolly shoal
#

Hello, I believe it would be better to have more infos. BTW this a minimal application I created while exploring monorepos technologies. The application uses the cache module as global and it's working as expected. There are even integration tests https://github.com/andreafspeziale/monorepo/tree/develop/apps/sample-app

As you mentioned being global I didn't imported it in my Cat module.

Hope that helps

GitHub

Opinionated Monorepo Toolkit 🪚. Contribute to andreafspeziale/monorepo development by creating an account on GitHub.

modest sphinx
#

Thanks, let me check that out. we're using monorepo as well, so that may yield some clues... Meanwhile: what more info do you need?

jolly shoal
#

I dont really know honestly. I was actually poiting out to the cachemanager resolve problem because it may be helpful knowing how u using it in (I believe) your service. BTW check that mini app maybe the structure reflects what you need and u can take inspiration

modest sphinx
#

I directly inject it into the service constructor:

 @Inject(CACHE_MANAGER) private cacheManager: Cache,

And then use it in code.

modest sphinx
#

So, Now I make it similar to @jolly shoal s code:

  imports: [
    ConfigModule.forRoot(),
    DbModule.forRoot(),
    CacheModule.registerAsync({
      isGlobal: true,
      useFactory: (cs: ConfigService) =>
        getRedisStore(
          cs.get('REDIS_URL', 'redis://localhost:6379'),
          parseInt(cs.get('CACHE_TTL', '3600')),
        ),
      inject: [ConfigService],
    }),
    UnoModule,
  ],
  providers: [Logger, DbMigrateService, ConfigService, RandomService],
})

const getRedisStore = async (url: string, ttl: number): Promise<any> => {
  const store = await redisStore({
    url,
    ttl,
  });

  return {
    store: () => store,
  };
};

This doesn't seem to work:

[ExceptionHandler] Nest can't resolve dependencies of the CACHE_MODULE_OPTIONS (?). Please make sure that the argument ConfigService at index [0] is available in the CacheModule context.

I don't have any CACHE_MODULE_OPTIONS in my code...

#

Ah, got it now 🙂

#

Made the ConfgModul global as well, that did the trick

#

Well, thanks for the help,

jolly shoal
#

Happy somehow it helped 🙂

trim walrus
#

Do you guys have unit tests written for this?
I use global cache in app.module.ts, and don’t need to import CacheModule in RandomService.module.ts. And the app runs fine. But unit tests are failing saying Nest can’t resolve dependencies of the RandomService.