Struct rocket_file_cache::Cache
[−]
[src]
pub struct Cache {
pub size_limit: usize,
// some fields omitted
}The cache holds a number of files whose bytes fit into its size_limit. The cache acts as a proxy to the filesystem, returning cached files if they are in the cache, or reading a file directly from the filesystem if the file is not in the cache.
When the cache is full, each file in the cache will have priority score determined by a provided priority function. When a a new file is attempted to be stored, it will calculate the priority of the new score and compare that against the score of the file with the lowest priority in the cache. If the new file's priority is higher, then the file in the cache will be removed and replaced with the new file. If removing the first file doesn't free up enough space for the new file, then the file with the next lowest priority will have its priority added to the other removed file's and the aggregate cached file's priority will be tested against the new file's.
This will repeat until either enough space can be freed for the new file, and the new file is inserted, or until the priority of the cached files is greater than that of the new file, in which case, the new file isn't inserted.
Fields
size_limit: usize
Methods
impl Cache[src]
fn new(size_limit: usize) -> Cache[src]
Creates a new Cache with the given size limit and the default priority function. The min and max file sizes are not set.
Arguments
size_limit- The number of bytes that the Cache is allowed to hold at a given time.
Example
use rocket_file_cache::Cache; let mut cache = Cache::new(1024 * 1024 * 30); // Create a cache that can hold 30 MB of files
fn get(&mut self, pathbuf: &PathBuf) -> Option<ResponderFile>[src]
Either gets the file from the cache if it exists there, gets it from the filesystem and tries to cache it, or fails to find the file and returns None.
Arguments
pathbuf- A pathbuf that represents the path of the file in the filesystem. The pathbuf also acts as a key for the file in the cache. The path will be used to find a cached file in the cache or find a file in the filesystem if an entry in the cache doesn't exist.
Example
#![feature(attr_literals)] #![feature(custom_attribute)] use rocket_file_cache::{Cache, ResponderFile}; use std::sync::Mutex; use std::path::{Path, PathBuf}; use rocket::State; use rocket::response::NamedFile; #[get("/<file..>")] fn files(file: PathBuf, cache: State<Mutex<Cache>> ) -> Option<ResponderFile> { let pathbuf: PathBuf = Path::new("www/").join(file).to_owned(); // Try to lock the cache in order to use it. match cache.try_lock() { Ok(mut cache) => cache.get(&pathbuf), Err(_) => { // Fall back to using the FS if another thread owns the lock. match NamedFile::open(pathbuf).ok() { Some(file) => Some(ResponderFile::from(file)), None => None } } } }
fn refresh(&mut self, pathbuf: &PathBuf) -> bool[src]
If a file has changed on disk, the cache will not automatically know that a change has occurred. Calling this function will check if the file exists, read the new file into memory, replace the old file, and update the priority score to reflect the new size of the file.
# Arguments
pathbuf- A pathbuf that represents the path of the file in the filesystem, and key in the cache. The path will be used to find the new file in the filesystem and find the old file to replace in the cache.
fn remove(&mut self, pathbuf: &PathBuf)[src]
Removes the file from the cache. This will not reset the access count, so the next time the file is accessed, it will be added to the cache again. The access count will have to be reset separately.
Arguments
pathbuf- A pathbuf that acts as a key to the file that should be removed from the cache
Example
use rocket_file_cache::Cache; use std::path::PathBuf; let mut cache = Cache::new(1024 * 1024 * 10); let pathbuf = PathBuf::new(); cache.remove(&pathbuf); assert!(cache.contains_key(&pathbuf) == false);
fn contains_key(&self, pathbuf: &PathBuf) -> bool[src]
Returns a boolean indicating if the cache has an entry corresponding to the given key.
Arguments
pathbuf- A pathbuf that is used as a key to look up the file.
Example
use rocket_file_cache::Cache; use std::path::PathBuf; let mut cache = Cache::new(1024 * 1024 * 20); let pathbuf: PathBuf = PathBuf::new(); cache.get(&pathbuf); assert!(cache.contains_key(&pathbuf) == false);
fn used_bytes(&self) -> usize[src]
Gets the sum of the sizes of the files that are stored in the cache.
Example
use rocket_file_cache::Cache; let cache = Cache::new(1024 * 1024 * 30); assert!(cache.used_bytes() == 0);