actix-web
actix-web copied to clipboard
In-memory file cache in actix-files
It would be nice if there was a feature in actix-files that allows reading static files into memory at startup and serving them from there, instead of reading them from disk. Currently, actix-web-static-files allows to read static files from memory, but it embeds static files into the executable, instead of loading them into memory at runtime. There's probably no need to refresh the files (re-read them from disk) once they're loaded into memory, as static files are rarely updated.
I'm interested in this. What should we do with the thread pool in that case? Also, thoughts on where to keep the cache? We could add a cache: Mutex<HashMap<PathBuf, NamedFile>> to the FilesService since the ServiceRequest::call impl for FilesService takes only &self. Or, we could maybe build the cache on startup based on the directory they are serving from but this has one potential issue, it may use a lot of memory. I'm also not sure the best place to add such changes, I'm unfamiliar with actix, maybe in Files, but I saw that it implements Clone, so it would probably not be ideal.
Almost every OS has file cache that works transparently. If your files don't change often they are served from memory and it's sufficient for 99.9% of cases. I see no win for this feature, only increased complexity and binary size.
Almost every OS has file cache that works transparently. If your files don't change often they are served from memory and it's sufficient for 99.9% of cases. I see no win for this feature, only increased complexity and binary size.
I don't think that most OS do file caching by default, could you elaborate by providing examples of this behavior? Also, as I mentioned in my original message, actix-web-static-files provides file caching, so there's already a need for in-memory caching, which suggests that most OS don't do file caching in memory.
Edit: From looking at several SO threads, it seems that Linux does in fact cache files in memory, but as far as I've read it relies on some filesystem mount options/kernel features, so my opinion is that this sort of thing should be done at the application level instead without relying on how a particular system is set up or what OS is being used.
https://en.wikipedia.org/wiki/Page_cache
as far as I've read it relies on some filesystem mount options/kernel features
And works in 99.99% cases
@mastopgunaf I agree. It is a application level problem. In my case, I just create my ServiceFactory like actix-files and it perform well. But local socket maybe better.
https://queue.acm.org/detail.cfm?id=1814327 seems relevant here. I agree that this is something that the OS takes care of with page-caching. Do people have real-world data (dtrace or similar) which shows this is a problem worth spending engineering effort on?