I read the source, find staticCache cannot detect file change #366

Closed
kaven276 opened this Issue Sep 14, 2011 · 8 comments

Projects

None yet

4 participants

@kaven276

if we use both staticCache and static,
when a file a cached, after we modified the file, the cache is there valided,
I read the code, and find no mechanism to discovery the change of file.

If it's true, it's a big problem, we cannot use staticCache in a production system that will change the files at running.

so if we can position staticCache a helper to static, or a general purpose cache that will serve any http get requests,
and any content provider module can use the general purpose cache to store the result response in memory for speed.
and can detect change at it's will.

Member
tj commented Sep 14, 2011

that's why there's a TODO in there :) it'll definitely be going in there i'm just a busy person. node-static and most others also just blindly cache, it's not a great thing obviously but like i said we just need to move some of the logic over

I know you are a very busy man. But you are hero in area of node, so we have high expatation for you, We want better node's modules.

If we are not to treat node as a toy, the cache is fatal, especially in a production environment, especially in a single threaded node.
If static-cache is not reliable, it's better not to develop it than provide it.
To ensure cache freshness, thress method could be applid

Method1:
The static.js module provide 304 for browser, so it can provide similar flag to static-cache.
first route to static-cache, static-cache will set corresponding cache's mtime/etag to req/header like browser do
then route to static, static will see if the static-cache's cache is valid, if it is, emit msg static-cache to use it to provide service
We can split the code in 'static-cache.js' " return function staticCache(req, res, next){" into two parts
upper part change to set the mtime/etag flag, lower part change to respond to static's emited msg

Method2:
route to static first (because static determine the freshness)
static may use static-cache as a utility,
static may ask static cache if it has a corresponding cache, if cache is fresh, then call static-cache to output the cache.
but this method may not confirm to the spirit of connect, connect is aimed to decoupled layers design

Method3:
the cache will hold the cache's corresponding filepath, and use fs module to watch the cached files
so if the cached files changed, the cache can invalid it.
I donnot know if watching too much file is a good idea.

method1/2 will fs.stats for every static file req, that may suffer some proformance.
method3 may have the best proformance, but it's constrained to a file cache like method2,it's not for dynamic content cache.
method 1 can design to a general cache who's key is url path and not file path, and can support any res result.

I know that you are very busy, if I have time, I'll make effort to improve it.

Member
tj commented Sep 15, 2011

thanks, it'll get in there I promise, hopefully soonish. We wont need to worry about dynamic content with static(), so we just need to chuck the typical freshness logic in there

Thank you for your efforts, I support you.

mitar commented Feb 12, 2013

Just to add, that sometimes static files are really static: they do not change during node.js run. So in such cases staticCache does its job.

Contributor

staticCache will be removed in v3

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment