|
| 1 | +use std::any::Any; |
| 2 | +use std::convert::Infallible; |
| 3 | + |
| 4 | +use futures::Future; |
| 5 | +use serde::{Deserialize, Serialize}; |
| 6 | +use tokio::fs::{create_dir_all, File}; |
| 7 | +use tokio::io::{AsyncReadExt, AsyncWriteExt}; |
| 8 | + |
| 9 | +/// Runs the given function once and then caches the result to the filesystem for future execution. Think of this as filesystem-level memoizing. In future, this will be broken out into |
| 10 | +/// its own crate and wrapped by Perseus. The second parameter to this allows forcing the function to re-fetch data every time, which is useful if you want to revalidate data or test |
| 11 | +/// your fetching logic again. Note that a change to the logic will not trigger a reload unless you make it do so. For this reason, it's recommended to only use this wrapper once |
| 12 | +/// you've tested your fetching logic. |
| 13 | +/// |
| 14 | +/// When running automated tests, you may wish to set `force_run` to the result of an environment variable check that you'll use when testing. |
| 15 | +/// |
| 16 | +/// This function expects to be run in the context of `.perseus/`, or any directory in which a folder `cache/` is available. If you're using Perseus without the CLI and you don't want |
| 17 | +/// that directory to exist, you shouldn't use this function. |
| 18 | +/// |
| 19 | +/// # Panics |
| 20 | +/// If this filesystem operations fail, this function will panic. It can't return a graceful error since it's expected to return the type you requested. |
| 21 | +pub async fn cache_res<D, F, Ft>(name: &str, f: F, force_run: bool) -> D |
| 22 | +where |
| 23 | + // By making this `Any`, we can downcast it to manage errors intuitively |
| 24 | + D: Serialize + for<'de> Deserialize<'de> + Any, |
| 25 | + F: Fn() -> Ft, |
| 26 | + Ft: Future<Output = D>, |
| 27 | +{ |
| 28 | + let f_res = || async { Ok::<D, Infallible>(f().await) }; |
| 29 | + // This can't fail, we just invented an error type for an infallible function |
| 30 | + cache_fallible_res(name, f_res, force_run).await.unwrap() |
| 31 | +} |
| 32 | + |
| 33 | +/// Same as `cache_res`, but takes a function that returns a `Result`, allowing you to use `?` and the like inside your logic. |
| 34 | +pub async fn cache_fallible_res<D, E, F, Ft>(name: &str, f: F, force_run: bool) -> Result<D, E> |
| 35 | +where |
| 36 | + // By making this `Any`, we can downcast it to manage errors intuitively |
| 37 | + D: Serialize + for<'de> Deserialize<'de>, |
| 38 | + E: std::error::Error, |
| 39 | + F: Fn() -> Ft, |
| 40 | + Ft: Future<Output = Result<D, E>>, |
| 41 | +{ |
| 42 | + // Replace any slashes with dashes to keep a flat directory structure |
| 43 | + let name = name.replace("/", "-"); |
| 44 | + // In production, we'll just run the function directly |
| 45 | + if cfg!(debug_assertions) { |
| 46 | + // Check if the cache file exists |
| 47 | + let filename = format!("cache/{}.json", &name); |
| 48 | + match File::open(&filename).await { |
| 49 | + Ok(mut file) => { |
| 50 | + if force_run { |
| 51 | + let res = f().await?; |
| 52 | + // Now cache the result |
| 53 | + let str_res = serde_json::to_string(&res).unwrap_or_else(|err| { |
| 54 | + panic!( |
| 55 | + "couldn't serialize result of entry '{}' for caching: {}", |
| 56 | + &filename, err |
| 57 | + ) |
| 58 | + }); |
| 59 | + let mut file = File::create(&filename).await.unwrap_or_else(|err| { |
| 60 | + panic!( |
| 61 | + "couldn't create cache file for entry '{}': {}", |
| 62 | + &filename, err |
| 63 | + ) |
| 64 | + }); |
| 65 | + file.write_all(str_res.as_bytes()) |
| 66 | + .await |
| 67 | + .unwrap_or_else(|err| { |
| 68 | + panic!( |
| 69 | + "couldn't write cache to file for entry '{}': {}", |
| 70 | + &filename, err |
| 71 | + ) |
| 72 | + }); |
| 73 | + |
| 74 | + Ok(res) |
| 75 | + } else { |
| 76 | + let mut contents = String::new(); |
| 77 | + file.read_to_string(&mut contents) |
| 78 | + .await |
| 79 | + .unwrap_or_else(|err| { |
| 80 | + panic!( |
| 81 | + "couldn't read cache from file for entry '{}': {}", |
| 82 | + &filename, err |
| 83 | + ) |
| 84 | + }); |
| 85 | + let res: D = match serde_json::from_str(&contents) { |
| 86 | + Ok(cached_res) => cached_res, |
| 87 | + // If the stuff in the cache can't be deserialized, we'll force a recreation (we don't recurse because that requires boxing the future) |
| 88 | + Err(_) => { |
| 89 | + let res = f().await?; |
| 90 | + // Now cache the result |
| 91 | + let str_res = serde_json::to_string(&res).unwrap_or_else(|err| { |
| 92 | + panic!( |
| 93 | + "couldn't serialize result of entry '{}' for caching: {}", |
| 94 | + &filename, err |
| 95 | + ) |
| 96 | + }); |
| 97 | + let mut file = File::create(&filename).await.unwrap_or_else(|err| { |
| 98 | + panic!( |
| 99 | + "couldn't create cache file for entry '{}': {}", |
| 100 | + &filename, err |
| 101 | + ) |
| 102 | + }); |
| 103 | + file.write_all(str_res.as_bytes()) |
| 104 | + .await |
| 105 | + .unwrap_or_else(|err| { |
| 106 | + panic!( |
| 107 | + "couldn't write cache to file for entry '{}': {}", |
| 108 | + &filename, err |
| 109 | + ) |
| 110 | + }); |
| 111 | + |
| 112 | + res |
| 113 | + } |
| 114 | + }; |
| 115 | + |
| 116 | + Ok(res) |
| 117 | + } |
| 118 | + } |
| 119 | + Err(err) if err.kind() == std::io::ErrorKind::NotFound => { |
| 120 | + // The file doesn't exist yet, create the parent cache directory |
| 121 | + create_dir_all("cache") |
| 122 | + .await |
| 123 | + .unwrap_or_else(|err| panic!("couldn't create cache directory: {}", err)); |
| 124 | + // We have no cache, so we'll have to run the function |
| 125 | + let res = f().await?; |
| 126 | + // Now cache the result |
| 127 | + let str_res = serde_json::to_string(&res).unwrap_or_else(|err| { |
| 128 | + panic!( |
| 129 | + "couldn't serialize result of entry '{}' for caching: {}", |
| 130 | + &filename, err |
| 131 | + ) |
| 132 | + }); |
| 133 | + let mut file = File::create(&filename).await.unwrap_or_else(|err| { |
| 134 | + panic!( |
| 135 | + "couldn't create cache file for entry '{}': {}", |
| 136 | + &filename, err |
| 137 | + ) |
| 138 | + }); |
| 139 | + file.write_all(str_res.as_bytes()) |
| 140 | + .await |
| 141 | + .unwrap_or_else(|err| { |
| 142 | + panic!( |
| 143 | + "couldn't write cache to file for entry '{}': {}", |
| 144 | + &filename, err |
| 145 | + ) |
| 146 | + }); |
| 147 | + |
| 148 | + Ok(res) |
| 149 | + } |
| 150 | + // Any other filesystem errors are unacceptable |
| 151 | + Err(err) => panic!( |
| 152 | + "filesystem error occurred while trying to read cache file for entry '{}': {}", |
| 153 | + &filename, err |
| 154 | + ), |
| 155 | + } |
| 156 | + } else { |
| 157 | + f().await |
| 158 | + } |
| 159 | +} |
0 commit comments