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
use crate::instrinsic::*;
use crate::policy::{Command, ExpirePolicy};
use crate::EntryId;
use std::collections::BTreeMap;
use std::sync::Arc;
use std::time::{Duration, Instant};
use atomic::{Atomic, Ordering};
use parking_lot::{Mutex, RwLock};
#[derive(Clone)]
pub struct TTIStorage {
timestamp: Arc<Mutex<Instant>>,
tti: Duration,
}
impl TTIStorage {
fn new(tti: Duration) -> Self {
Self {
timestamp: Arc::new(Mutex::new(Instant::now() + tti)),
tti,
}
}
fn is_expired(&self) -> bool {
let now = Instant::now();
let nxt = now + self.tti;
let mut timestamp = self.timestamp.lock();
if *timestamp > now {
*timestamp = nxt;
return false;
}
return true;
}
}
pub struct TTIPolicy {
tti_records: RwLock<BTreeMap<Instant, Vec<Option<(EntryId, TTIStorage)>>>>,
tti_last_update: Atomic<Instant>,
presision: Duration,
}
impl TTIPolicy {
#[must_use]
pub fn new() -> Self {
Self::with_presision(Duration::from_millis(100))
}
#[must_use]
pub fn with_presision(presision: Duration) -> Self {
Self {
tti_records: RwLock::new(BTreeMap::new()),
tti_last_update: Atomic::new(Instant::now()),
presision,
}
}
}
impl ExpirePolicy for TTIPolicy {
type Info = Duration;
type Storage = TTIStorage;
fn init_storage(&self, tti: Self::Info) -> Self::Storage {
TTIStorage::new(tti)
}
fn clear(&mut self) {
self.tti_records.write().clear();
*self.tti_last_update.get_mut() = Instant::now();
}
fn is_expired(&self, _: EntryId, storage: &Self::Storage) -> bool {
storage.is_expired()
}
fn on_access(&self, _: EntryId, _: &Self::Storage) -> Command {
let now = Instant::now();
loop {
let last_update = self.tti_last_update.load(Ordering::Relaxed);
if likely(last_update + self.presision > now) {
return Command::Noop;
}
if self
.tti_last_update
.compare_exchange_weak(last_update, now, Ordering::Acquire, Ordering::Relaxed)
.is_ok()
{
break;
}
}
let mut records = self.tti_records.write();
let expires_at = if let Some(v) = records.keys().next() {
v.clone()
} else {
return Command::Noop;
};
if expires_at > now {
return Command::Noop;
}
let mut expired = Vec::new();
for record in records.remove(&expires_at).unwrap() {
if let Some((entry, storage)) = record {
if storage.is_expired() {
expired.push(Some(entry));
} else {
let new_slot = align_instant(*storage.timestamp.lock(), self.presision);
records
.entry(new_slot)
.or_insert(Vec::new())
.push(Some((entry, storage)));
}
}
}
Command::RemoveBulk(expired)
}
fn on_insert(&self, entry: EntryId, storage: &Self::Storage) -> Command {
let slot = align_instant(*storage.timestamp.lock(), self.presision);
{
let mut ttl_records = self.tti_records.write();
ttl_records
.entry(slot)
.or_insert(Vec::new())
.push(Some((entry, storage.clone())));
}
self.on_access(entry, storage)
}
fn on_resize(&self) -> Command {
Command::Noop
}
}
#[inline]
fn align_instant(instant: Instant, by: Duration) -> Instant {
use once_cell::sync::Lazy;
static BASE: Lazy<Instant> = Lazy::new(|| Instant::now());
let v = *BASE;
if likely(v < instant) {
let offs = instant - v;
instant + Duration::from_millis((by.as_millis() - offs.as_millis() % by.as_millis()) as u64)
} else {
v
}
}