{"id":16585393,"url":"https://github.com/xatuke/rocket-file-cache","last_synced_at":"2026-04-21T14:03:00.380Z","repository":{"id":123710276,"uuid":"448857211","full_name":"xatuke/rocket-file-cache","owner":"xatuke","description":null,"archived":false,"fork":false,"pushed_at":"2022-01-17T11:07:17.000Z","size":12041,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"feature-ageout","last_synced_at":"2025-03-05T15:07:35.008Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Rust","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/xatuke.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null}},"created_at":"2022-01-17T10:58:20.000Z","updated_at":"2022-01-17T16:23:44.000Z","dependencies_parsed_at":"2023-10-03T13:49:20.695Z","dependency_job_id":null,"html_url":"https://github.com/xatuke/rocket-file-cache","commit_stats":null,"previous_names":["xatuke/rocket-file-cache"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xatuke%2Frocket-file-cache","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xatuke%2Frocket-file-cache/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xatuke%2Frocket-file-cache/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xatuke%2Frocket-file-cache/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/xatuke","download_url":"https://codeload.github.com/xatuke/rocket-file-cache/tar.gz/refs/heads/feature-ageout","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":242158574,"owners_count":20081289,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-10-11T22:47:53.013Z","updated_at":"2026-04-21T14:03:00.337Z","avatar_url":"https://github.com/xatuke.png","language":"Rust","funding_links":[],"categories":[],"sub_categories":[],"readme":"[![Current Crates.io Version](https://img.shields.io/crates/v/rocket-file-cache.svg)](https://crates.io/crates/rocket-file-cache)\n\n# Rocket File Cache\nA concurrent, in-memory file cache for the Rocket web framework.\n\nRocket File Cache can be used as a drop in replacement for Rocket's NamedFile when serving files.\n\nThis code from the [static_files](https://github.com/SergioBenitez/Rocket/blob/master/examples/static_files/src/main.rs) example from Rocket:\n```rust\n#[get(\"/\u003cfile..\u003e\")]\nfn files(file: PathBuf) -\u003e Option\u003cNamedFile\u003e {\n    NamedFile::open(Path::new(\"static/\").join(file)).ok()\n}\n\nfn main() {\n    rocket::ignite().mount(\"/\", routes![files]).launch();\n}\n```\nCan be sped up by getting files via a cache instead:\n```rust\n#[get(\"/\u003cfile..\u003e\")]\nfn files(file: PathBuf, cache: State\u003cCache\u003e ) -\u003e CachedFile {\n    CachedFile::open(Path::new(\"static/\").join(file), cache.inner())\n}\n\n\nfn main() {\n    let cache: Cache = CacheBuilder::new()\n        .size_limit(1024 * 1024 * 40) // 40 MB\n        .build()\n        .unwrap();\n\n    rocket::ignite()\n        .manage(cache)\n        .mount(\"/\", routes![files])\n        .launch();\n}\n```\n\n\n# Use case \nRocket File Cache keeps a set of frequently accessed files in memory so your webserver won't have to wait for your disk to read the files.\nThis should improve latency and throughput on systems that are bottlenecked on disk I/O.\n\nIf you are serving a known size of static files (index.html, js bundle, a couple of assets),\nyou should try to set the maximum size of the cache to let them all fit,\nespecially if all of these are served every time someone visits your website.\n\nIf you serve static files with a larger aggregate size than what would nicely fit into memory, \nbut you have some content that is visited more often than others, you should specify enough space for the cache\nso that the most popular content will fit.\nIf your popular content changes over time, and you want the cache to reflect what is currently most popular,\nit is possible to use the `alter_all_access_counts()` method to reduce the access count of all items currently in the cache,\nmaking it easier for newer content to find its way into the cache.\n\n\nIf you serve user created files, the same logic regarding file popularity applies,\nonly that you may want to spawn a thread every 10000 or so requests that will use `alter_all_access_counts()` \nto reduce the access counts of the items in the cache.\n\n### Performance\n\nThe bench tests try to get the file from whatever source, either cache or filesystem, and read it once into memory.\nThe misses measure the time it takes for the cache to realize that the file is not stored, and to read the file from disk.\nRunning the bench tests on an AWS EC2 t2 micro instance (82 MB/s HDD) returned these results:\n```\ntest cache::tests::cache_get_10mb                       ... bench:   1,444,068 ns/iter (+/- 251,467)\ntest cache::tests::cache_get_1mb                        ... bench:      79,397 ns/iter (+/- 4,613)\ntest cache::tests::cache_get_1mb_from_1000_entry_cache  ... bench:      79,038 ns/iter (+/- 1,751)\ntest cache::tests::cache_get_5mb                        ... bench:     724,262 ns/iter (+/- 7,751)\ntest cache::tests::cache_miss_10mb                      ... bench:   3,184,473 ns/iter (+/- 299,657)\ntest cache::tests::cache_miss_1mb                       ... bench:     806,821 ns/iter (+/- 19,731)\ntest cache::tests::cache_miss_1mb_from_1000_entry_cache ... bench:   1,379,925 ns/iter (+/- 25,118)\ntest cache::tests::cache_miss_5mb                       ... bench:   1,542,059 ns/iter (+/- 27,063)\ntest cache::tests::cache_miss_5mb_from_1000_entry_cache ... bench:   2,090,871 ns/iter (+/- 37,040)\ntest cache::tests::in_memory_file_read_10mb             ... bench:   7,222,402 ns/iter (+/- 596,325)\ntest cache::tests::named_file_read_10mb                 ... bench:   4,908,544 ns/iter (+/- 581,408)\ntest cache::tests::named_file_read_1mb                  ... bench:     893,447 ns/iter (+/- 18,354)\ntest cache::tests::named_file_read_5mb                  ... bench:   1,605,741 ns/iter (+/- 41,418)\n```\n\nIt can be seen that on a server with slow disk reads, small file access times are vastly improved versus the disk.\nLarger files also seem to benefit, although to a lesser degree.\nMinimum and maximum file sizes can be set to keep files in the cache within size bounds.\n\nFor queries that will retrieve an entry from the cache, there is no time penalty for each additional file in the cache.\nThe more items in the cache, the larger the time penalty for a cache miss.\n\n\n\n### Requirements\n* Rocket \u003e= 0.3.5\n* Nightly Rust\n  * Known to work on (2017-12-20). `rustup default nightly-2017-12-20`\n    * Rocket will not compile on newer nightlies as of the time of writing due to the crate `ring`.\n    * This crate doesn't use any special features of nightly, but is dependent on Rocket, which currently requires nightly.\n\n\n# Notes\nIf you have any feature requests, notice any bugs, or if anything in the documentation is unclear, please open an Issue and I will respond ASAP.\n\nDevelopment on this crate has slowed, but you should expect a couple more breaking changes before this reaches a 1.0.0 release.\nYou can keep up to date with these changes with the [changelog](CHANGELOG.md).\n\n# Alternatives \n* [Nginx](http://nginx.org/)\n* Write your own.\nMost of the work here focuses on when to replace items in the cache.\nIf you know that you will never grow or shrink your cache of files, all you need is a \n`Mutex\u003cHashMap\u003cPathBuf, Vec\u003cu8\u003e\u003e\u003e`, an `impl Responder\u003c'static\u003e for Vec\u003cu8\u003e {...}`, and some glue logic\nto hold your files in memory and serve them as responses.\n* Rely on setting the cache-control HTTP header to cause the files to be cached in end-user's browsers\n and internet infrastructure between them and the server. This strategy can be used in conjunction with \n Rocket File Cache as well.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fxatuke%2Frocket-file-cache","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fxatuke%2Frocket-file-cache","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fxatuke%2Frocket-file-cache/lists"}