1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312
use std::{
sync::Arc,
time::{Duration, Instant},
};
#[derive(Clone, Debug)]
/// The policy of a cache.
pub struct Policy {
max_capacity: Option<u64>,
num_segments: usize,
time_to_live: Option<Duration>,
time_to_idle: Option<Duration>,
}
impl Policy {
pub(crate) fn new(
max_capacity: Option<u64>,
num_segments: usize,
time_to_live: Option<Duration>,
time_to_idle: Option<Duration>,
) -> Self {
Self {
max_capacity,
num_segments,
time_to_live,
time_to_idle,
}
}
/// Returns the `max_capacity` of the cache.
pub fn max_capacity(&self) -> Option<u64> {
self.max_capacity
}
#[cfg(feature = "sync")]
pub(crate) fn set_max_capacity(&mut self, capacity: Option<u64>) {
self.max_capacity = capacity;
}
/// Returns the number of internal segments of the cache.
pub fn num_segments(&self) -> usize {
self.num_segments
}
#[cfg(feature = "sync")]
pub(crate) fn set_num_segments(&mut self, num: usize) {
self.num_segments = num;
}
/// Returns the `time_to_live` of the cache.
pub fn time_to_live(&self) -> Option<Duration> {
self.time_to_live
}
/// Returns the `time_to_idle` of the cache.
pub fn time_to_idle(&self) -> Option<Duration> {
self.time_to_idle
}
}
/// Calculates when cache entries expire. A single expiration time is retained on
/// each entry so that the lifetime of an entry may be extended or reduced by
/// subsequent evaluations.
///
/// `Expiry` trait provides three methods. They specify the expiration time of an
/// entry by returning a `Some(duration)` until the entry expires:
///
/// - [`expire_after_create`](#method.expire_after_create) — Returns the
/// duration (or none) after the entry's creation.
/// - [`expire_after_read`](#method.expire_after_read) — Returns the duration
/// (or none) after its last read.
/// - [`expire_after_update`](#method.expire_after_update) — Returns the
/// duration (or none) after its last update.
///
/// The default implementations are provided that return `None` (no expiration) or
/// `current_duration: Option<Instant>` (not modify the current expiration time).
/// Override some of them as you need.
///
pub trait Expiry<K, V> {
/// Specifies that the entry should be automatically removed from the cache once
/// the duration has elapsed after the entry's creation. This method is called
/// for cache write methods such as `insert` and `get_with` but only when the key
/// was not present in the cache.
///
/// # Parameters
///
/// - `key` — A reference to the key of the entry.
/// - `value` — A reference to the value of the entry.
/// - `current_time` — The current instant.
///
/// # Returning `None`
///
/// - Returning `None` indicates no expiration for the entry.
/// - The default implementation returns `None`.
///
/// # Notes on `time_to_live` and `time_to_idle` policies
///
/// When the cache is configured with `time_to_live` and/or `time_to_idle`
/// policies, the entry will be evicted after the earliest of the expiration time
/// returned by this expiry, the `time_to_live` and `time_to_idle` policies.
#[allow(unused_variables)]
fn expire_after_create(&self, key: &K, value: &V, current_time: Instant) -> Option<Duration> {
None
}
/// Specifies that the entry should be automatically removed from the cache once
/// the duration has elapsed after its last read. This method is called for cache
/// read methods such as `get` and `get_with` but only when the key is present
/// in the cache.
///
/// # Parameters
///
/// - `key` — A reference to the key of the entry.
/// - `value` — A reference to the value of the entry.
/// - `current_time` — The current instant.
/// - `current_duration` — The remaining duration until the entry expires.
/// - `last_modified_at` — The instant when the entry was created or
/// updated.
///
/// # Returning `None` or `current_duration`
///
/// - Returning `None` indicates no expiration for the entry.
/// - Returning `current_duration` will not modify the expiration time.
/// - The default implementation returns `current_duration` (not modify the
/// expiration time)
///
/// # Notes on `time_to_live` and `time_to_idle` policies
///
/// When the cache is configured with `time_to_live` and/or `time_to_idle`
/// policies, then:
///
/// - The entry will be evicted after the earliest of the expiration time
/// returned by this expiry, the `time_to_live` and `time_to_idle` policies.
/// - The `current_duration` takes in account the `time_to_live` and
/// `time_to_idle` policies.
#[allow(unused_variables)]
fn expire_after_read(
&self,
key: &K,
value: &V,
current_time: Instant,
current_duration: Option<Duration>,
last_modified_at: Instant,
) -> Option<Duration> {
current_duration
}
/// Specifies that the entry should be automatically removed from the cache once
/// the duration has elapsed after the replacement of its value. This method is
/// called for cache write methods such as `insert` but only when the key is
/// already present in the cache.
///
/// # Parameters
///
/// - `key` — A reference to the key of the entry.
/// - `value` — A reference to the value of the entry.
/// - `current_time` — The current instant.
/// - `current_duration` — The remaining duration until the entry expires.
///
/// # Returning `None` or `current_duration`
///
/// - Returning `None` indicates no expiration for the entry.
/// - Returning `current_duration` will not modify the expiration time.
/// - The default implementation returns `current_duration` (not modify the
/// expiration time)
///
/// # Notes on `time_to_live` and `time_to_idle` policies
///
/// When the cache is configured with `time_to_live` and/or `time_to_idle`
/// policies, then:
///
/// - The entry will be evicted after the earliest of the expiration time
/// returned by this expiry, the `time_to_live` and `time_to_idle` policies.
/// - The `current_duration` takes in account the `time_to_live` and
/// `time_to_idle` policies.
#[allow(unused_variables)]
fn expire_after_update(
&self,
key: &K,
value: &V,
current_time: Instant,
current_duration: Option<Duration>,
) -> Option<Duration> {
current_duration
}
}
pub(crate) struct ExpirationPolicy<K, V> {
time_to_live: Option<Duration>,
time_to_idle: Option<Duration>,
expiry: Option<Arc<dyn Expiry<K, V> + Send + Sync + 'static>>,
}
impl<K, V> Default for ExpirationPolicy<K, V> {
fn default() -> Self {
Self {
time_to_live: None,
time_to_idle: None,
expiry: None,
}
}
}
impl<K, V> Clone for ExpirationPolicy<K, V> {
fn clone(&self) -> Self {
Self {
time_to_live: self.time_to_live,
time_to_idle: self.time_to_idle,
expiry: self.expiry.as_ref().map(Arc::clone),
}
}
}
impl<K, V> ExpirationPolicy<K, V> {
#[cfg(test)]
pub(crate) fn new(
time_to_live: Option<Duration>,
time_to_idle: Option<Duration>,
expiry: Option<Arc<dyn Expiry<K, V> + Send + Sync + 'static>>,
) -> Self {
Self {
time_to_live,
time_to_idle,
expiry,
}
}
/// Returns the `time_to_live` of the cache.
pub(crate) fn time_to_live(&self) -> Option<Duration> {
self.time_to_live
}
pub(crate) fn set_time_to_live(&mut self, duration: Duration) {
self.time_to_live = Some(duration);
}
/// Returns the `time_to_idle` of the cache.
pub(crate) fn time_to_idle(&self) -> Option<Duration> {
self.time_to_idle
}
pub(crate) fn set_time_to_idle(&mut self, duration: Duration) {
self.time_to_idle = Some(duration);
}
pub(crate) fn expiry(&self) -> Option<Arc<dyn Expiry<K, V> + Send + Sync + 'static>> {
self.expiry.as_ref().map(Arc::clone)
}
pub(crate) fn set_expiry(&mut self, expiry: Arc<dyn Expiry<K, V> + Send + Sync + 'static>) {
self.expiry = Some(expiry);
}
}
#[cfg(test)]
pub(crate) mod test_utils {
use std::sync::atomic::{AtomicU8, Ordering};
#[derive(Default)]
pub(crate) struct ExpiryCallCounters {
expected_creations: AtomicU8,
expected_reads: AtomicU8,
expected_updates: AtomicU8,
actual_creations: AtomicU8,
actual_reads: AtomicU8,
actual_updates: AtomicU8,
}
impl ExpiryCallCounters {
pub(crate) fn incl_expected_creations(&self) {
self.expected_creations.fetch_add(1, Ordering::Relaxed);
}
pub(crate) fn incl_expected_reads(&self) {
self.expected_reads.fetch_add(1, Ordering::Relaxed);
}
pub(crate) fn incl_expected_updates(&self) {
self.expected_updates.fetch_add(1, Ordering::Relaxed);
}
pub(crate) fn incl_actual_creations(&self) {
self.actual_creations.fetch_add(1, Ordering::Relaxed);
}
pub(crate) fn incl_actual_reads(&self) {
self.actual_reads.fetch_add(1, Ordering::Relaxed);
}
pub(crate) fn incl_actual_updates(&self) {
self.actual_updates.fetch_add(1, Ordering::Relaxed);
}
pub(crate) fn verify(&self) {
assert_eq!(
self.expected_creations.load(Ordering::Relaxed),
self.actual_creations.load(Ordering::Relaxed),
"expected_creations != actual_creations"
);
assert_eq!(
self.expected_reads.load(Ordering::Relaxed),
self.actual_reads.load(Ordering::Relaxed),
"expected_reads != actual_reads"
);
assert_eq!(
self.expected_updates.load(Ordering::Relaxed),
self.actual_updates.load(Ordering::Relaxed),
"expected_updates != actual_updates"
);
}
}
}