hgzimmerman / rocket-file-cache

An in-memory file cache for the Rocket web framework
MIT License
22 stars 6 forks source link

Equivalent of Rocket static_files example #8

Closed digeratus closed 6 years ago

digeratus commented 6 years ago

I was just looking into caching with Ngnix and Rocket when I found this instead. I’m new to Rust and Rocket itself. I have a working app serving static files that I implemented following the static_files example on Rocket’s github. Can you duplicate the same example using your crate?

https://github.com/SergioBenitez/Rocket/blob/master/examples/static_files/src/main.rs

Thanks.

hgzimmerman commented 6 years ago

While I can't speak towards the performance of this solution versus Ngnix, I can say that you should be able to easily integrate this crate in place of the static files example.

I have a simple example in the Readme that should get you most of the way towards using this crate.

I also have a better example in the docs here: https://docs.rs/rocket-file-cache/0.6.2/rocket_file_cache/struct.Cache.html#example-1 . This example will fall back to getting the file from the filesystem if another thread is busy accessing the cache.

You do need to initialize the cache and "manage" it before my example functions would work.

The Rocket Guide has a good example of state management here: https://rocket.rs/guide/state/#state

If you have any further questions let me know.

hgzimmerman commented 6 years ago

Actually, between the time I made my comment and now, I added a few examples which can be found here: https://github.com/hgzimmerman/rocket-file-cache/tree/master/examples.

If you are looking for the bare minimum replacement using the cache, use the cached_files example.

If you anticipate serving many requests at once, the cached_files_fallback_to_fs example won't slow down your request, waiting for locks guarding the cache to unlock. Overall this is the better solution.

digeratus commented 6 years ago

Thank you so much. Made my day!!!

Thanks, José

On Nov 15, 2017, at 7:57 AM, Henry Zimmerman notifications@github.com wrote:

Actually, between the time I made my comment and now, I added a few examples which can be found here: https://github.com/hgzimmerman/rocket-file-cache/tree/master/examples.

If you are looking for the bare minimum replacement using the cache, use the cached_files example.

If you anticipate serving many requests at once, the cached_files_fallback_to_fs example won't slow down your request, waiting for locks guarding the cache to unlock. Overall this is the better solution.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub, or mute the thread.

hgzimmerman commented 6 years ago

I'm going to go ahead and close this, but before I do, I will let you know about the possibility of pooling multiple caches to make sure that you never need to touch the disk for files that are in the cache.

I have an example for a pool here that groups multiple caches into a pool, and then returns a random one when a request is made. If you are only storing static files, and can handle the memory overhead of multiple caches for effectively the same data, this should be the best option.

That said, I may directly integrate the Pool from that example into this crate or spin it off into another crate where it would become more fully featured, but for now it should be adequate.