Say I have 20 modules, 10 of which form a circular dependency system, and 10 of which require each other in not a circular dependency fashion. The 10 modules that don't form circular dependencies can be compiled into an output format independently (say we are compiling from a custom language to JavaScript). The circular dependency ones, however, need to be compiled into a single file essentially (for my purposes). Not too much of a problem there.
But say we remove one of the 10 circular dependency cycles, and now we have only 9 modules in the circular dependency relationship. That should get compiled to one output module file now.
The problem is caching and naming.
It's somewhat straightforward to recompile a non-circular module, just look in the cache for a key named after this file, and replace its content. Well actually, it does get tricky here, because what if you rename the file? Without doing file watching, there would be no efficient way to clear the old cached file if you renamed it. Maybe after every save of a file or recompilation, you gather up all the file names recursively, and delete the keys not matching that list. That still seems less than ideal, but maybe it works well enough.
But for circular dependencies, what is the name of the file? The name of all 10 modules at first? That file name might start getting too long (if we had 100 or 1000 modules forming a cycle). So we perhaps hash the concatenated names and use that? That would work if we changed a cyclic module without a rename, looking up the cache by key. But what if we renamed a cyclic file? The has wouldn't work now, and we would be back to having to gather up all file names (and hashes for cyclic systems) and clearing out the ones that no longer exist, for every change to a file or recompilation?
Is there a better way to solve this problem?
- How do you clear the cache and keep it relevant efficiently? Specifically when you rename (or even delete) a file/module.
- What is the standard/recommended approach to keep the cache in sync to optimize performance in this sort of scenario?