Builds the cache from the stable memory repository. This method should only be called during init or upgrade hooks to ensure that the cache is up-to-date with the repository and that we have enough instructions to rebuild the cache.
(&self)
| 206 | /// This method should only be called during init or upgrade hooks to ensure that the cache is |
| 207 | /// up-to-date with the repository and that we have enough instructions to rebuild the cache. |
| 208 | pub fn build_cache(&self) { |
| 209 | if self.len() > Self::MAX_CACHE_SIZE { |
| 210 | print(format!( |
| 211 | "Only the first {} users will be added to the cache, the reposity has {} users.", |
| 212 | Self::MAX_CACHE_SIZE, |
| 213 | USER_REPOSITORY.len(), |
| 214 | )); |
| 215 | } |
| 216 | |
| 217 | CACHE.with(|cache| { |
| 218 | cache.borrow_mut().clear(); |
| 219 | |
| 220 | DB.with(|db| { |
| 221 | for (_, user) in db.borrow().iter().take(Self::MAX_CACHE_SIZE) { |
| 222 | cache.borrow_mut().insert(user.id, user); |
| 223 | } |
| 224 | }); |
| 225 | }); |
| 226 | } |
| 227 | |
| 228 | /// Returns the user associated with the given identity if it exists. |
| 229 | pub fn find_by_identity(&self, identity: &Principal) -> Option<User> { |