├── .github └── workflows │ └── rust.yml ├── .gitignore ├── Cargo.toml ├── LICENSE-APACHE ├── LICENSE-MIT ├── README.md ├── src └── lib.rs └── tests └── test.rs /.github/workflows/rust.yml: -------------------------------------------------------------------------------- 1 | on: [push, pull_request] 2 | 3 | name: build 4 | 5 | jobs: 6 | lint: 7 | name: Format and Clippy 8 | strategy: 9 | matrix: 10 | platform: [ubuntu-latest, macos-latest, windows-latest] 11 | runs-on: ${{ matrix.platform }} 12 | 13 | steps: 14 | - name: Checkout sources 15 | uses: actions/checkout@v2 16 | 17 | - name: Install Rust stable toolchain 18 | uses: actions-rs/toolchain@v1 19 | with: 20 | profile: minimal 21 | toolchain: stable 22 | override: true 23 | components: rustfmt, clippy 24 | 25 | - name: Run cargo fmt 26 | uses: actions-rs/cargo@v1 27 | with: 28 | command: fmt 29 | args: --all -- --check 30 | 31 | - name: Run cargo clippy 32 | uses: actions-rs/cargo@v1 33 | with: 34 | command: clippy 35 | args: --all -- -D warnings 36 | 37 | test: 38 | name: Test 39 | strategy: 40 | matrix: 41 | platform: [ubuntu-latest] 42 | runs-on: ${{ matrix.platform }} 43 | 44 | steps: 45 | - name: Checkout sources 46 | uses: actions/checkout@v2 47 | 48 | - name: Install Rust stable toolchain 49 | uses: actions-rs/toolchain@v1 50 | with: 51 | profile: minimal 52 | toolchain: stable 53 | override: true 54 | 55 | - name: Run cargo test 56 | uses: actions-rs/cargo@v1 57 | with: 58 | command: test 59 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | target 2 | Cargo.lock 3 | .idea 4 | -------------------------------------------------------------------------------- /Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "ttl_cache" 3 | version = "0.5.1" 4 | authors = ["Stu Small "] 5 | description = "A cache that will expire values after a TTL" 6 | repository = "https://github.com/stusmall/ttl_cache" 7 | documentation = "https://docs.rs/ttl_cache/" 8 | keywords = ["cache","ttl","expire"] 9 | license = "MIT/Apache-2.0" 10 | 11 | [dependencies] 12 | linked-hash-map = "0.5" 13 | 14 | 15 | [features] 16 | default = [] 17 | stats = [] 18 | -------------------------------------------------------------------------------- /LICENSE-APACHE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "{}" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright {yyyy} {name of copyright owner} 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | 203 | -------------------------------------------------------------------------------- /LICENSE-MIT: -------------------------------------------------------------------------------- 1 | Copyright (c) 2016 Stu Small 2 | 3 | Permission is hereby granted, free of charge, to any 4 | person obtaining a copy of this software and associated 5 | documentation files (the "Software"), to deal in the 6 | Software without restriction, including without 7 | limitation the rights to use, copy, modify, merge, 8 | publish, distribute, sublicense, and/or sell copies of 9 | the Software, and to permit persons to whom the Software 10 | is furnished to do so, subject to the following 11 | conditions: 12 | 13 | The above copyright notice and this permission notice 14 | shall be included in all copies or substantial portions 15 | of the Software. 16 | 17 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF 18 | ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 19 | TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A 20 | PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT 21 | SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 22 | CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 23 | OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR 24 | IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 25 | DEALINGS IN THE SOFTWARE. 26 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # ttl_cache 2 | 3 | [![Actions Status](https://github.com/stusmall/ttl_cache/workflows/build/badge.svg)](https://github.com/stusmall/ttl_cache/actions) 4 | [![Documentation](https://docs.rs/ttl_cache/badge.svg)](https://docs.rs/ttl_cache) 5 | 6 | This crate provides a time sensitive key-value FIFO cache. When the cache is created it is 7 | given a TTL. Any value that are in the cache for longer than this duration are considered 8 | invalid and will not be returned. Supports 1.20 + 9 | -------------------------------------------------------------------------------- /src/lib.rs: -------------------------------------------------------------------------------- 1 | //! This crate provides a time sensitive key-value cache. When an item is inserted it is 2 | //! given a TTL. Any value that are in the cache after their duration are considered invalid 3 | //! and will not be returned on lookups. Entries are not removed from the cache on expiration. 4 | //! Instead the expired values are removed opportunistically as they are reference or if 5 | //! `remove_expired` is explicitly called. 6 | extern crate linked_hash_map; 7 | 8 | use std::borrow::Borrow; 9 | use std::collections::hash_map::RandomState; 10 | use std::hash::{BuildHasher, Hash}; 11 | #[cfg(feature = "stats")] 12 | use std::sync::atomic::{AtomicUsize, Ordering}; 13 | use std::time::{Duration, Instant}; 14 | 15 | use linked_hash_map::Entry as LinkedHashMapEntry; 16 | use linked_hash_map::LinkedHashMap; 17 | use linked_hash_map::OccupiedEntry as OccupiedLinkHashMapEntry; 18 | use linked_hash_map::VacantEntry as VacantLinkHashMapEntry; 19 | 20 | /// A view into a single location in a map, which may be vacant or occupied. 21 | pub enum Entry<'a, K: 'a, V: 'a, S: 'a = RandomState> { 22 | /// An occupied Entry. 23 | Occupied(OccupiedEntry<'a, K, V, S>), 24 | /// A vacant Entry. 25 | Vacant(VacantEntry<'a, K, V, S>), 26 | } 27 | 28 | impl<'a, K: Hash + Eq, V, S: BuildHasher> Entry<'a, K, V, S> { 29 | pub fn key(&self) -> &K { 30 | match *self { 31 | Entry::Occupied(ref e) => e.key(), 32 | Entry::Vacant(ref e) => e.key(), 33 | } 34 | } 35 | } 36 | 37 | /// A view into a single occupied location in the cache that was unexpired at the moment of lookup. 38 | pub struct OccupiedEntry<'a, K: 'a, V: 'a, S: 'a = RandomState> { 39 | entry: OccupiedLinkHashMapEntry<'a, K, InternalEntry, S>, 40 | } 41 | 42 | impl<'a, K: Hash + Eq, V, S: BuildHasher> OccupiedEntry<'a, K, V, S> { 43 | /// Gets a reference to the entry key 44 | /// 45 | /// # Examples 46 | /// 47 | /// ``` 48 | /// use std::time::Duration; 49 | /// use ttl_cache::TtlCache; 50 | /// 51 | /// let mut map = TtlCache::new(10); 52 | /// 53 | /// map.insert("foo".to_string(), 1, Duration::from_secs(30)); 54 | /// assert_eq!("foo", map.entry("foo".to_string()).key()); 55 | /// ``` 56 | pub fn key(&self) -> &K { 57 | self.entry.key() 58 | } 59 | 60 | /// Gets a reference to the value in the entry. 61 | pub fn get(&self) -> &V { 62 | &self.entry.get().value 63 | } 64 | 65 | /// Gets a mutable reference to the value in the entry. 66 | pub fn get_mut(&mut self) -> &mut V { 67 | &mut self.entry.get_mut().value 68 | } 69 | 70 | /// Sets the value of the entry, and returns the entry's old value 71 | pub fn insert(&mut self, value: V, duration: Duration) -> V { 72 | let internal_entry = self.entry.insert(InternalEntry::new(value, duration)); 73 | internal_entry.value 74 | } 75 | } 76 | 77 | /// A view into a single empty location in the cache 78 | pub struct VacantEntry<'a, K: 'a, V: 'a, S: 'a = RandomState> { 79 | entry: VacantLinkHashMapEntry<'a, K, InternalEntry, S>, 80 | } 81 | 82 | impl<'a, K: 'a + Hash + Eq, V: 'a, S: BuildHasher> VacantEntry<'a, K, V, S> { 83 | /// Gets a reference to the entry key 84 | /// 85 | /// # Examples 86 | /// 87 | /// ``` 88 | /// use ttl_cache::TtlCache; 89 | /// 90 | /// let mut map = TtlCache::::new(10); 91 | /// 92 | /// assert_eq!("foo", map.entry("foo".to_string()).key()); 93 | /// ``` 94 | pub fn key(&self) -> &K { 95 | self.entry.key() 96 | } 97 | 98 | /// Sets the value of the entry with the VacantEntry's key, 99 | /// and returns a mutable reference to it 100 | pub fn insert(self, value: V, duration: Duration) -> &'a mut V { 101 | let internal_entry = self.entry.insert(InternalEntry::new(value, duration)); 102 | &mut internal_entry.value 103 | } 104 | } 105 | 106 | #[derive(Clone)] 107 | struct InternalEntry { 108 | value: V, 109 | expiration: Instant, 110 | } 111 | 112 | impl InternalEntry { 113 | fn new(v: V, duration: Duration) -> Self { 114 | InternalEntry { 115 | value: v, 116 | expiration: Instant::now() + duration, 117 | } 118 | } 119 | 120 | fn is_expired(&self) -> bool { 121 | Instant::now() > self.expiration 122 | } 123 | } 124 | 125 | /// A time sensitive cache. 126 | pub struct TtlCache { 127 | map: LinkedHashMap, S>, 128 | max_size: usize, 129 | #[cfg(feature = "stats")] 130 | hits: AtomicUsize, 131 | #[cfg(feature = "stats")] 132 | misses: AtomicUsize, 133 | #[cfg(feature = "stats")] 134 | since: Instant, 135 | } 136 | 137 | impl TtlCache { 138 | /// Creates an empty cache that can hold at most `capacity` items. 139 | /// 140 | /// # Examples 141 | /// 142 | /// ``` 143 | /// use ttl_cache::TtlCache; 144 | /// 145 | /// let mut cache: TtlCache = TtlCache::new(10); 146 | /// ``` 147 | pub fn new(capacity: usize) -> Self { 148 | TtlCache { 149 | map: LinkedHashMap::new(), 150 | max_size: capacity, 151 | #[cfg(feature = "stats")] 152 | hits: AtomicUsize::new(0), 153 | #[cfg(feature = "stats")] 154 | misses: AtomicUsize::new(0), 155 | #[cfg(feature = "stats")] 156 | since: Instant::now(), 157 | } 158 | } 159 | } 160 | 161 | impl TtlCache { 162 | /// Creates an empty cache that can hold at most `capacity` items 163 | /// with the given hash builder. 164 | pub fn with_hasher(capacity: usize, hash_builder: S) -> Self { 165 | TtlCache { 166 | map: LinkedHashMap::with_hasher(hash_builder), 167 | max_size: capacity, 168 | #[cfg(feature = "stats")] 169 | hits: AtomicUsize::new(0), 170 | #[cfg(feature = "stats")] 171 | misses: AtomicUsize::new(0), 172 | #[cfg(feature = "stats")] 173 | since: Instant::now(), 174 | } 175 | } 176 | 177 | /// Check if the cache contains the given key. 178 | /// 179 | /// # Examples 180 | /// ``` 181 | /// use std::time::Duration; 182 | /// use ttl_cache::TtlCache; 183 | /// 184 | /// let mut cache = TtlCache::new(10); 185 | /// cache.insert(1, "a", Duration::from_secs(30)); 186 | /// assert_eq!(cache.contains_key(&1), true); 187 | /// ``` 188 | pub fn contains_key(&self, key: &Q) -> bool 189 | where 190 | K: Borrow, 191 | Q: Hash + Eq, 192 | { 193 | // Expiration check is handled by get 194 | self.get(key).is_some() 195 | } 196 | 197 | /// Inserts a key-value pair into the cache with an individual ttl for the key. If the key 198 | /// already existed and hasn't expired, the old value is returned. 199 | /// 200 | /// # Examples 201 | /// 202 | /// ``` 203 | /// use std::time::Duration; 204 | /// use ttl_cache::TtlCache; 205 | /// 206 | /// let mut cache = TtlCache::new(2); 207 | /// 208 | /// cache.insert(1, "a", Duration::from_secs(20)); 209 | /// cache.insert(2, "b", Duration::from_secs(60)); 210 | /// assert_eq!(cache.get(&1), Some(&"a")); 211 | /// assert_eq!(cache.get(&2), Some(&"b")); 212 | /// ``` 213 | pub fn insert(&mut self, k: K, v: V, ttl: Duration) -> Option { 214 | let to_insert = InternalEntry::new(v, ttl); 215 | let old_val = self.map.insert(k, to_insert); 216 | if self.len() > self.capacity() { 217 | self.remove_oldest(); 218 | } 219 | old_val.and_then(|x| if x.is_expired() { None } else { Some(x.value) }) 220 | } 221 | 222 | /// Returns a reference to the value corresponding to the given key in the cache, if 223 | /// it contains an unexpired entry. 224 | /// 225 | /// # Examples 226 | /// 227 | /// ``` 228 | /// use std::time::Duration; 229 | /// use ttl_cache::TtlCache; 230 | /// 231 | /// let mut cache = TtlCache::new(2); 232 | /// let duration = Duration::from_secs(30); 233 | /// 234 | /// cache.insert(1, "a", duration); 235 | /// cache.insert(2, "b", duration); 236 | /// cache.insert(2, "c", duration); 237 | /// cache.insert(3, "d", duration); 238 | /// 239 | /// assert_eq!(cache.get(&1), None); 240 | /// assert_eq!(cache.get(&2), Some(&"c")); 241 | /// ``` 242 | pub fn get(&self, k: &Q) -> Option<&V> 243 | where 244 | K: Borrow, 245 | Q: Hash + Eq, 246 | { 247 | #[allow(clippy::let_and_return)] 248 | let to_ret = self 249 | .map 250 | .get(k) 251 | .and_then(|x| if x.is_expired() { None } else { Some(&x.value) }); 252 | #[cfg(feature = "stats")] 253 | { 254 | if to_ret.is_some() { 255 | self.hits.fetch_add(1, Ordering::Relaxed); 256 | } else { 257 | self.misses.fetch_add(1, Ordering::Relaxed); 258 | } 259 | } 260 | to_ret 261 | } 262 | 263 | /// Returns a mutable reference to the value corresponding to the given key in the cache, if 264 | /// it contains an unexpired entry. 265 | /// 266 | /// # Examples 267 | /// 268 | /// ``` 269 | /// use std::time::Duration; 270 | /// use ttl_cache::TtlCache; 271 | /// 272 | /// let mut cache = TtlCache::new(2); 273 | /// let duration = Duration::from_secs(30); 274 | /// 275 | /// cache.insert(1, "a", duration); 276 | /// cache.insert(2, "b", duration); 277 | /// cache.insert(2, "c", duration); 278 | /// cache.insert(3, "d", duration); 279 | /// 280 | /// assert_eq!(cache.get_mut(&1), None); 281 | /// assert_eq!(cache.get_mut(&2), Some(&mut "c")); 282 | /// ``` 283 | pub fn get_mut(&mut self, k: &Q) -> Option<&mut V> 284 | where 285 | K: Borrow, 286 | Q: Hash + Eq, 287 | { 288 | #[warn(clippy::let_and_return)] 289 | let to_ret = self.map.get_mut(k).and_then(|x| { 290 | if x.is_expired() { 291 | None 292 | } else { 293 | Some(&mut x.value) 294 | } 295 | }); 296 | #[cfg(feature = "stats")] 297 | { 298 | if to_ret.is_some() { 299 | self.hits.fetch_add(1, Ordering::Relaxed); 300 | } else { 301 | self.misses.fetch_add(1, Ordering::Relaxed); 302 | } 303 | } 304 | to_ret 305 | } 306 | 307 | /// Removes the given key from the cache and returns its corresponding value. 308 | /// 309 | /// # Examples 310 | /// 311 | /// ``` 312 | /// use std::time::Duration; 313 | /// use ttl_cache::TtlCache; 314 | /// 315 | /// let mut cache = TtlCache::new(2); 316 | /// 317 | /// cache.insert(2, "a", Duration::from_secs(30)); 318 | /// 319 | /// assert_eq!(cache.remove(&1), None); 320 | /// assert_eq!(cache.remove(&2), Some("a")); 321 | /// assert_eq!(cache.remove(&2), None); 322 | /// ``` 323 | pub fn remove(&mut self, k: &Q) -> Option 324 | where 325 | K: Borrow, 326 | Q: Hash + Eq, 327 | { 328 | self.map 329 | .remove(k) 330 | .and_then(|x| if x.is_expired() { None } else { Some(x.value) }) 331 | } 332 | 333 | /// Returns the maximum number of key-value pairs the cache can hold. 334 | /// 335 | /// # Examples 336 | /// 337 | /// ``` 338 | /// use std::time::Duration; 339 | /// use ttl_cache::TtlCache; 340 | /// 341 | /// let mut cache: TtlCache = TtlCache::new(2); 342 | /// assert_eq!(cache.capacity(), 2); 343 | /// ``` 344 | pub fn capacity(&self) -> usize { 345 | self.max_size 346 | } 347 | 348 | /// Sets the number of key-value pairs the cache can hold. Removes 349 | /// oldest key-value pairs if necessary. 350 | /// 351 | /// # Examples 352 | /// 353 | /// ``` 354 | /// use std::time::Duration; 355 | /// use ttl_cache::TtlCache; 356 | /// 357 | /// let mut cache = TtlCache::new(2); 358 | /// let duration = Duration::from_secs(30); 359 | /// 360 | /// cache.insert(1, "a", duration); 361 | /// cache.insert(2, "b", duration); 362 | /// cache.insert(3, "c", duration); 363 | /// 364 | /// assert_eq!(cache.get(&1), None); 365 | /// assert_eq!(cache.get(&2), Some(&"b")); 366 | /// assert_eq!(cache.get(&3), Some(&"c")); 367 | /// 368 | /// cache.set_capacity(3); 369 | /// cache.insert(1, "a", duration); 370 | /// cache.insert(2, "b", duration); 371 | /// 372 | /// assert_eq!(cache.get(&1), Some(&"a")); 373 | /// assert_eq!(cache.get(&2), Some(&"b")); 374 | /// assert_eq!(cache.get(&3), Some(&"c")); 375 | /// 376 | /// cache.set_capacity(1); 377 | /// 378 | /// assert_eq!(cache.get(&1), None); 379 | /// assert_eq!(cache.get(&2), Some(&"b")); 380 | /// assert_eq!(cache.get(&3), None); 381 | /// ``` 382 | pub fn set_capacity(&mut self, capacity: usize) { 383 | for _ in capacity..self.len() { 384 | self.remove_oldest(); 385 | } 386 | self.max_size = capacity; 387 | } 388 | 389 | /// Clears all values out of the cache 390 | pub fn clear(&mut self) { 391 | self.map.clear(); 392 | } 393 | 394 | pub fn entry(&mut self, k: K) -> Entry { 395 | let should_remove = self 396 | .map 397 | .get(&k) 398 | .map(|value| value.is_expired()) 399 | .unwrap_or(false); 400 | if should_remove { 401 | self.map.remove(&k); 402 | } 403 | match self.map.entry(k) { 404 | LinkedHashMapEntry::Occupied(entry) => Entry::Occupied(OccupiedEntry { entry }), 405 | LinkedHashMapEntry::Vacant(entry) => Entry::Vacant(VacantEntry { entry }), 406 | } 407 | } 408 | 409 | /// Returns an iterator over the cache's key-value pairs in oldest to youngest order. 410 | /// 411 | /// # Examples 412 | /// 413 | /// ``` 414 | /// use std::time::Duration; 415 | /// use ttl_cache::TtlCache; 416 | /// 417 | /// let mut cache = TtlCache::new(2); 418 | /// let duration = Duration::from_secs(30); 419 | /// 420 | /// cache.insert(1, 10, duration); 421 | /// cache.insert(2, 20, duration); 422 | /// cache.insert(3, 30, duration); 423 | /// 424 | /// let kvs: Vec<_> = cache.iter().collect(); 425 | /// assert_eq!(kvs, [(&2, &20), (&3, &30)]); 426 | /// ``` 427 | pub fn iter(&mut self) -> Iter { 428 | self.remove_expired(); 429 | Iter(self.map.iter()) 430 | } 431 | 432 | /// Returns an iterator over the cache's key-value pairs in oldest to youngest order with 433 | /// mutable references to the values. 434 | /// 435 | /// 436 | /// # Examples 437 | /// 438 | /// ``` 439 | /// use std::time::Duration; 440 | /// use ttl_cache::TtlCache; 441 | /// 442 | /// let mut cache = TtlCache::new(2); 443 | /// let duration = Duration::from_secs(30); 444 | /// 445 | /// cache.insert(1, 10, duration); 446 | /// cache.insert(2, 20, duration); 447 | /// cache.insert(3, 30, duration); 448 | /// 449 | /// let mut n = 2; 450 | /// 451 | /// for (k, v) in cache.iter_mut() { 452 | /// assert_eq!(*k, n); 453 | /// assert_eq!(*v, n * 10); 454 | /// *v *= 10; 455 | /// n += 1; 456 | /// } 457 | /// 458 | /// assert_eq!(n, 4); 459 | /// assert_eq!(cache.get(&2), Some(&200)); 460 | /// assert_eq!(cache.get(&3), Some(&300)); 461 | /// ``` 462 | pub fn iter_mut(&mut self) -> IterMut { 463 | self.remove_expired(); 464 | IterMut(self.map.iter_mut()) 465 | } 466 | 467 | /// This will evict all expired records in the cache. Typically expired values will remain in 468 | /// cache until they are accessed and then they are cleaned up. Calling `remove_expired` 469 | /// will have no impact on what values we can access in the cache, it will only hurry up the 470 | /// removal of expired values. One reason why you would want to call this is if the values 471 | /// stored in the cache implement Drop and you want to trigger that behavior (i.e. free up file 472 | /// handles sooner). 473 | pub fn remove_expired(&mut self) { 474 | let should_pop_head = |map: &LinkedHashMap, S>| match map.front() { 475 | Some(entry) => entry.1.is_expired(), 476 | None => false, 477 | }; 478 | while should_pop_head(&self.map) { 479 | self.map.pop_front(); 480 | } 481 | } 482 | 483 | /// The cache will keep track of some basic stats during its usage that can be helpful 484 | /// for performance tuning or monitoring. This method will reset these counters. 485 | /// # Examples 486 | /// 487 | /// ``` 488 | /// use std::thread::sleep; 489 | /// use std::time::Duration; 490 | /// use ttl_cache::TtlCache; 491 | /// 492 | /// let mut cache = TtlCache::new(2); 493 | /// 494 | /// cache.insert(1, "a", Duration::from_secs(20)); 495 | /// cache.insert(2, "b", Duration::from_millis(1)); 496 | /// sleep(Duration::from_millis(10)); 497 | /// let _ = cache.get(&1); 498 | /// let _ = cache.get(&2); 499 | /// let _ = cache.get(&3); 500 | /// assert_eq!(cache.miss_count(), 2); 501 | /// cache.reset_stats_counter(); 502 | /// assert_eq!(cache.miss_count(), 0); 503 | #[cfg(feature = "stats")] 504 | pub fn reset_stats_counter(&mut self) { 505 | self.hits = AtomicUsize::new(0); 506 | self.misses = AtomicUsize::new(0); 507 | self.since = Instant::now(); 508 | } 509 | 510 | /// Returns the number of unexpired cache hits since the last time the counters were reset. 511 | /// # Examples 512 | /// 513 | /// ``` 514 | /// use std::thread::sleep; 515 | /// use std::time::Duration; 516 | /// use ttl_cache::TtlCache; 517 | /// 518 | /// let mut cache = TtlCache::new(2); 519 | /// 520 | /// cache.insert(1, "a", Duration::from_secs(20)); 521 | /// cache.insert(2, "b", Duration::from_millis(1)); 522 | /// sleep(Duration::from_millis(10)); 523 | /// assert!(cache.get(&1).is_some()); 524 | /// assert!(cache.get(&2).is_none()); 525 | /// assert!(cache.get(&3).is_none()); 526 | /// assert_eq!(cache.hit_count(), 1); 527 | #[cfg(feature = "stats")] 528 | pub fn hit_count(&self) -> usize { 529 | self.hits.load(Ordering::Relaxed) 530 | } 531 | 532 | /// Returns the number of cache misses since the last time the counters were reset. Entries 533 | /// that have expired count as a miss. 534 | /// # Examples 535 | /// 536 | /// ``` 537 | /// use std::thread::sleep; 538 | /// use std::time::Duration; 539 | /// use ttl_cache::TtlCache; 540 | /// 541 | /// let mut cache = TtlCache::new(2); 542 | /// 543 | /// cache.insert(1, "a", Duration::from_secs(20)); 544 | /// cache.insert(2, "b", Duration::from_millis(1)); 545 | /// sleep(Duration::from_millis(10)); 546 | /// let _ = cache.get(&1); 547 | /// let _ = cache.get(&2); 548 | /// let _ = cache.get(&3); 549 | /// assert_eq!(cache.miss_count(), 2); 550 | #[cfg(feature = "stats")] 551 | pub fn miss_count(&self) -> usize { 552 | self.misses.load(Ordering::Relaxed) 553 | } 554 | 555 | /// Returns the Instant when we started gathering stats. This is either when the cache was 556 | /// created or when it was last reset, whichever happened most recently. 557 | #[cfg(feature = "stats")] 558 | pub fn stats_since(&self) -> Instant { 559 | self.since 560 | } 561 | 562 | // This isn't made pubic because the length returned isn't exact. It can include expired values. 563 | // If people find that they want this then I can include a length method that trims expired 564 | // entries then returns the size, but I'd rather now. One wouldn't expect a len() operation 565 | // to change the contents of the structure. 566 | fn len(&self) -> usize { 567 | self.map.len() 568 | } 569 | 570 | fn remove_oldest(&mut self) { 571 | self.map.pop_front(); 572 | } 573 | } 574 | 575 | impl Clone for TtlCache 576 | where 577 | K: Clone, 578 | V: Clone, 579 | { 580 | fn clone(&self) -> TtlCache { 581 | TtlCache { 582 | map: self.map.clone(), 583 | max_size: self.max_size, 584 | #[cfg(feature = "stats")] 585 | hits: AtomicUsize::new(self.hits.load(Ordering::Relaxed)), 586 | #[cfg(feature = "stats")] 587 | misses: AtomicUsize::new(self.misses.load(Ordering::Relaxed)), 588 | #[cfg(feature = "stats")] 589 | since: self.since, 590 | } 591 | } 592 | } 593 | 594 | pub struct Iter<'a, K: 'a, V: 'a>(linked_hash_map::Iter<'a, K, InternalEntry>); 595 | 596 | impl<'a, K, V> Clone for Iter<'a, K, V> { 597 | fn clone(&self) -> Iter<'a, K, V> { 598 | Iter(self.0.clone()) 599 | } 600 | } 601 | 602 | impl<'a, K, V> Iterator for Iter<'a, K, V> { 603 | type Item = (&'a K, &'a V); 604 | 605 | fn next(&mut self) -> Option<(&'a K, &'a V)> { 606 | match self.0.next() { 607 | Some(entry) => { 608 | if entry.1.is_expired() { 609 | self.next() 610 | } else { 611 | Some((entry.0, &entry.1.value)) 612 | } 613 | } 614 | None => None, 615 | } 616 | } 617 | 618 | fn size_hint(&self) -> (usize, Option) { 619 | self.0.size_hint() 620 | } 621 | } 622 | 623 | impl<'a, K, V> DoubleEndedIterator for Iter<'a, K, V> { 624 | fn next_back(&mut self) -> Option<(&'a K, &'a V)> { 625 | match self.0.next_back() { 626 | Some(entry) => { 627 | if entry.1.is_expired() { 628 | // The entries are in order of time. So if the previous entry is expired, every 629 | // else before it will be expired too. 630 | None 631 | } else { 632 | Some((entry.0, &entry.1.value)) 633 | } 634 | } 635 | None => None, 636 | } 637 | } 638 | } 639 | 640 | pub struct IterMut<'a, K: 'a, V: 'a>(linked_hash_map::IterMut<'a, K, InternalEntry>); 641 | 642 | impl<'a, K, V> Iterator for IterMut<'a, K, V> { 643 | type Item = (&'a K, &'a mut V); 644 | fn next(&mut self) -> Option<(&'a K, &'a mut V)> { 645 | match self.0.next() { 646 | Some(entry) => { 647 | if entry.1.is_expired() { 648 | self.next() 649 | } else { 650 | Some((entry.0, &mut entry.1.value)) 651 | } 652 | } 653 | None => None, 654 | } 655 | } 656 | fn size_hint(&self) -> (usize, Option) { 657 | self.0.size_hint() 658 | } 659 | } 660 | 661 | impl<'a, K, V> DoubleEndedIterator for IterMut<'a, K, V> { 662 | fn next_back(&mut self) -> Option<(&'a K, &'a mut V)> { 663 | match self.0.next_back() { 664 | Some(entry) => { 665 | if entry.1.is_expired() { 666 | None 667 | } else { 668 | Some((entry.0, &mut entry.1.value)) 669 | } 670 | } 671 | None => None, 672 | } 673 | } 674 | } 675 | -------------------------------------------------------------------------------- /tests/test.rs: -------------------------------------------------------------------------------- 1 | extern crate ttl_cache; 2 | 3 | use std::thread::sleep; 4 | use std::time::Duration; 5 | use ttl_cache::TtlCache; 6 | 7 | #[test] 8 | fn test_put_and_get() { 9 | let duration = Duration::from_secs(60 * 60); 10 | let mut cache = TtlCache::new(2); 11 | cache.insert(1, 10, duration); 12 | cache.insert(2, 20, duration); 13 | assert_eq!(cache.get(&1), Some(&10)); 14 | assert_eq!(cache.get(&2), Some(&20)); 15 | } 16 | 17 | #[test] 18 | fn test_put_and_get_mut() { 19 | let duration = Duration::from_secs(60 * 60); 20 | let mut cache = TtlCache::new(2); 21 | cache.insert(1, 10, duration); 22 | cache.insert(2, 20, duration); 23 | assert_eq!(cache.get_mut(&1), Some(&mut 10)); 24 | assert_eq!(cache.get_mut(&2), Some(&mut 20)); 25 | } 26 | 27 | #[test] 28 | fn test_get_mut_change() { 29 | let duration = Duration::from_secs(60 * 60); 30 | let mut cache = TtlCache::new(2); 31 | cache.insert(1, 10, duration); 32 | assert_eq!(cache.get(&1), Some(&10)); 33 | cache.get_mut(&1).map(|v| *v = 20); 34 | assert_eq!(cache.get(&1), Some(&20)); 35 | } 36 | 37 | #[test] 38 | fn test_put_update() { 39 | let duration = Duration::from_secs(60 * 60); 40 | let mut cache = TtlCache::new(1); 41 | cache.insert("1", 10, duration); 42 | cache.insert("1", 19, duration); 43 | assert_eq!(cache.get("1"), Some(&19)); 44 | } 45 | 46 | #[test] 47 | fn test_contains_key() { 48 | let duration = Duration::from_secs(60 * 60); 49 | let mut cache = TtlCache::new(1); 50 | cache.insert("1", 10, duration); 51 | assert_eq!(cache.contains_key("1"), true); 52 | } 53 | 54 | #[test] 55 | fn test_expire_value() { 56 | let duration = Duration::from_millis(1); 57 | let mut cache = TtlCache::new(1); 58 | cache.insert("1", 10, duration); 59 | sleep(Duration::from_millis(10)); 60 | assert_eq!(cache.contains_key("1"), false); 61 | } 62 | 63 | #[test] 64 | fn test_pop() { 65 | let duration = Duration::from_secs(60 * 60); 66 | let mut cache = TtlCache::new(2); 67 | cache.insert(1, 10, duration); 68 | cache.insert(2, 20, duration); 69 | let opt1 = cache.remove(&1); 70 | assert!(opt1.is_some()); 71 | assert_eq!(opt1.unwrap(), 10); 72 | assert!(cache.get(&1).is_none()); 73 | } 74 | 75 | #[test] 76 | fn test_change_capacity() { 77 | let duration = Duration::from_secs(60 * 60); 78 | let mut cache = TtlCache::new(2); 79 | assert_eq!(cache.capacity(), 2); 80 | cache.insert(1, 10, duration); 81 | cache.insert(2, 20, duration); 82 | cache.set_capacity(1); 83 | assert!(cache.get(&1).is_none()); 84 | assert_eq!(cache.capacity(), 1); 85 | } 86 | 87 | #[test] 88 | fn test_remove() { 89 | let duration = Duration::from_secs(60 * 60); 90 | let mut cache = TtlCache::new(3); 91 | cache.insert(1, 10, duration); 92 | cache.insert(2, 20, duration); 93 | cache.insert(3, 30, duration); 94 | cache.insert(4, 40, duration); 95 | cache.insert(5, 50, duration); 96 | cache.remove(&3); 97 | cache.remove(&4); 98 | assert!(cache.get(&3).is_none()); 99 | assert!(cache.get(&4).is_none()); 100 | cache.insert(6, 60, duration); 101 | cache.insert(7, 70, duration); 102 | cache.insert(8, 80, duration); 103 | assert!(cache.get(&5).is_none()); 104 | assert_eq!(cache.get(&6), Some(&60)); 105 | assert_eq!(cache.get(&7), Some(&70)); 106 | assert_eq!(cache.get(&8), Some(&80)); 107 | } 108 | 109 | #[test] 110 | fn test_clear() { 111 | let duration = Duration::from_secs(60 * 60); 112 | let mut cache = TtlCache::new(2); 113 | cache.insert(1, 10, duration); 114 | cache.insert(2, 20, duration); 115 | cache.clear(); 116 | assert!(cache.get(&1).is_none()); 117 | assert!(cache.get(&2).is_none()); 118 | } 119 | 120 | #[test] 121 | fn test_iter() { 122 | let duration = Duration::from_secs(60 * 60); 123 | let mut cache = TtlCache::new(3); 124 | cache.insert(1, 10, duration); 125 | cache.insert(2, 20, duration); 126 | cache.insert(3, 30, duration); 127 | cache.insert(4, 40, duration); 128 | cache.insert(5, 50, duration); 129 | assert_eq!( 130 | cache.iter().collect::>(), 131 | [(&3, &30), (&4, &40), (&5, &50)] 132 | ); 133 | assert_eq!( 134 | cache.iter_mut().collect::>(), 135 | [(&3, &mut 30), (&4, &mut 40), (&5, &mut 50)] 136 | ); 137 | assert_eq!( 138 | cache.iter().rev().collect::>(), 139 | [(&5, &50), (&4, &40), (&3, &30)] 140 | ); 141 | assert_eq!( 142 | cache.iter_mut().rev().collect::>(), 143 | [(&5, &mut 50), (&4, &mut 40), (&3, &mut 30)] 144 | ); 145 | } 146 | 147 | #[test] 148 | fn test_iter_w_expired() { 149 | let duration = Duration::from_millis(100); 150 | let mut cache = TtlCache::new(3); 151 | cache.insert(1, 10, duration); 152 | sleep(Duration::from_millis(200)); 153 | cache.insert(2, 20, duration); 154 | cache.insert(3, 30, duration); 155 | assert_eq!(cache.iter().collect::>(), [(&2, &20), (&3, &30)]); 156 | assert_eq!( 157 | cache.iter_mut().collect::>(), 158 | [(&2, &mut 20), (&3, &mut 30)] 159 | ); 160 | assert_eq!( 161 | cache.iter().rev().collect::>(), 162 | [(&3, &30), (&2, &20)] 163 | ); 164 | assert_eq!( 165 | cache.iter_mut().rev().collect::>(), 166 | [(&3, &mut 30), (&2, &mut 20)] 167 | ); 168 | } 169 | 170 | #[test] 171 | fn test() { 172 | let mut cache = TtlCache::new(3); 173 | cache.insert(1, 10, Duration::from_millis(300)); 174 | cache.insert(2, 20, Duration::from_millis(10)); 175 | cache.insert(3, 30, Duration::from_millis(300)); 176 | sleep(Duration::from_millis(20)); 177 | assert_eq!(cache.iter().collect::>(), [(&1, &10), (&3, &30)]); 178 | } 179 | --------------------------------------------------------------------------------