#CacheModule registerAsync cant resolve

24 messages · Page 1 of 1 (latest)

wanton imp
#

Hi all, in a project when we load cacheModule with registerAsync intending to resolve its config using configService. We get errors thrown while other modules which make use of cache cant resolve their dependency. When we switch to cacheModule.register with hardcodes values the issue goes away.

mild bridge
#

Can you provide any errors and related configurations?

mystic ibexBOT
#

Suggestion for @wanton imp:
When asking for help with a problem, don't just say, "X doesn't work" or "Y did not fix it" actually describe the error.
If there is an error message, share it. If it did not function as expected, describe how it behaved and how you expected it to behave.
If you haven't already, or haven't recently, share the code that is not working and any code we may need to help diagnose the error.

wanton imp
dusky tendon
wanton imp
#

@dusky tendon that is just one of many other modules that make use of Cache manager, so in *Modules there is no reimporting of CacheModule which is imported globally on app module, and I'd expect it becomes available to all further modules, since registering it globally.

#

All what other modules do is: at constructor inject a cacheManager instance to make use of it. Basically: @Inject(CACHE_MANAGER) private readonly cacheManager: Cache

vague flower
#

In the async version, you're missing setting isGlobal. Also, it is better practice to create a cache service with the module, to then export it and use it (import it) where you actually need it (i.e. don't make it global).

wanton imp
#

@vague flower thanks for your feedback, it did not change the outcome, still getting same error.

vague flower
#

Did you try creating a service?

wanton imp
#

No, I understand what you're suggesting. If our team will agree will try having it implemented like that.

#

However this seems to be an issue in Nestjs, hope it gets fixed anytime soon.

vague flower
#

You could also try adding @Global above the module definition for your cache module.

wanton imp
#

My guess is, modules are imported faster than cache config factory resolved.

#

@vague flower I'll try that too now.

vague flower
#

But, again. The "normal" way to introduce the cache module is to build your own cache module with a service (which is smarter, because you can control the logic better in what is stored and how).

#

And then import that module only where you need it.

wanton imp
#

I'm just stuck on registering cache module with factory evaluation and hardcoded mode.

#

Hardcoded mode with global flag works instantly.

#

Factory mode with registerAsync fails to provide cacheService where used, no matter if there is global flag or not.

#

So plain simple, hardcoded works, env vars via configService does not work.

vague flower
#

Also, if it must be, it is better to have your additionally created cache module (with your service) made global.

#

My point being, the cache module Nest offers is very rudimentary. You'll find yourself wiring in logic in its usage and that should actually be in its own service.