Gets cached data if state matches, otherwise calls `compute`. If the cache is disabled or no cached data is found then `compute` is called to calculate the data. If the data was found in cache it is passed to `deserialize`, which if successful will be the returned value. When computed the `serialize` function is used to generate the bytes from the returned value.
(
&self,
state: &T,
// NOTE: These are function pointers instead of closures so that they
// don't accidentally close over something not accounted in the cache.
| 179 | /// When computed the `serialize` function is used to generate the bytes |
| 180 | /// from the returned value. |
| 181 | pub fn get_data_raw<T, U, E>( |
| 182 | &self, |
| 183 | state: &T, |
| 184 | // NOTE: These are function pointers instead of closures so that they |
| 185 | // don't accidentally close over something not accounted in the cache. |
| 186 | compute: fn(&T) -> Result<U, E>, |
| 187 | serialize: fn(&T, &U) -> Option<Vec<u8>>, |
| 188 | deserialize: fn(&T, Vec<u8>) -> Option<U>, |
| 189 | ) -> Result<U, E> |
| 190 | where |
| 191 | T: Hash, |
| 192 | { |
| 193 | let inner = match &self.0 { |
| 194 | Some(inner) => inner, |
| 195 | None => return compute(state), |
| 196 | }; |
| 197 | |
| 198 | let mut hasher = Sha256Hasher(Sha256::new()); |
| 199 | state.hash(&mut hasher); |
| 200 | let hash: [u8; 32] = hasher.0.finalize().into(); |
| 201 | // standard encoding uses '/' which can't be used for filename |
| 202 | let hash = base64::engine::general_purpose::URL_SAFE_NO_PAD.encode(&hash); |
| 203 | |
| 204 | if let Some(cached_val) = inner.get_data(&hash) { |
| 205 | if let Some(val) = deserialize(state, cached_val) { |
| 206 | let mod_cache_path = inner.root_path.join(&hash); |
| 207 | inner.cache.on_cache_get_async(&mod_cache_path); // call on success |
| 208 | return Ok(val); |
| 209 | } |
| 210 | } |
| 211 | let val_to_cache = compute(state)?; |
| 212 | if let Some(bytes) = serialize(state, &val_to_cache) { |
| 213 | if inner.update_data(&hash, &bytes).is_some() { |
| 214 | let mod_cache_path = inner.root_path.join(&hash); |
| 215 | inner.cache.on_cache_update_async(&mod_cache_path); // call on success |
| 216 | } |
| 217 | } |
| 218 | Ok(val_to_cache) |
| 219 | } |
| 220 | } |
| 221 | |
| 222 | impl<'cache> ModuleCacheEntryInner<'cache> { |
no test coverage detected