@@ -36,6 +36,8 @@ pub struct Metadata {
36
36
last_modified : u64 ,
37
37
/// Last time since this entry was accessed, milliseconds since epoch
38
38
last_accessed : u64 ,
39
+ /// Number of times this entry has been HIT (total accesses)
40
+ hits : u64 ,
39
41
/// Md5 hash of the underlying data
40
42
integrity : Md5Bytes ,
41
43
}
@@ -61,6 +63,7 @@ impl Metadata {
61
63
size : data. len ( ) as u64 ,
62
64
last_modified : now_since_epoch ( ) ,
63
65
last_accessed : now_since_epoch ( ) ,
66
+ hits : 0 ,
64
67
integrity : md5:: compute ( data) . into ( ) ,
65
68
}
66
69
}
@@ -109,6 +112,43 @@ impl Metadata {
109
112
self . last_modified
110
113
}
111
114
115
+ /// The total number of times this entry has been read.
116
+ ///
117
+ /// **NOTE:** This will be 0 unless `track_access` is enabled from the [`CacheBuilder`]
118
+ ///
119
+ /// [`CacheBuilder`]: crate::CacheBuilder
120
+ #[ inline]
121
+ pub fn get_hits ( & self ) -> u64 {
122
+ self . hits
123
+ }
124
+
125
+ /// Retrives the last time this entry was accessed (read from).
126
+ ///
127
+ /// **NOTE:** This will be the same as [`get_last_modified`] unless `track_access` is enabled from
128
+ /// the [`CacheBuilder`]
129
+ ///
130
+ /// [`get_last_modified`]: Self::get_last_modified
131
+ /// [`CacheBuilder`]: crate::CacheBuilder
132
+ pub fn get_last_acccessed ( & self ) -> Option < time:: SystemTime > {
133
+ match self . last_accessed {
134
+ 0 => None ,
135
+ millis => Some ( time:: UNIX_EPOCH + time:: Duration :: from_millis ( millis) ) ,
136
+ }
137
+ }
138
+ /// Retrieves the raw `last_accessed` time, which is the milliseconds since
139
+ /// [`time::UNIX_EPOCH`]. If the returned result is `0`, that means there is no `last_accessed`
140
+ /// time.
141
+ ///
142
+ /// **NOTE:** This will be the same as [`get_last_modified_raw`] unless `track_access` is enabled
143
+ /// from the [`CacheBuilder`]
144
+ ///
145
+ /// [`get_last_modified_raw`]: Self::get_last_modified_raw
146
+ /// [`CacheBuilder`]: crate::CacheBuilder
147
+ #[ inline]
148
+ pub fn get_last_accessed_raw ( & self ) -> u64 {
149
+ self . last_accessed
150
+ }
151
+
112
152
/// Retrieves the internal [`Md5Bytes`] integrity of the corresponding metadata entry.
113
153
#[ inline]
114
154
pub fn get_integrity ( & self ) -> & Md5Bytes {
@@ -161,6 +201,22 @@ impl MetaDb {
161
201
}
162
202
}
163
203
204
+ /// Will increment the `hits` counter and set the `last_accessed` value to now for the found
205
+ /// metadata key.
206
+ pub fn track_access_for ( & self , key : & [ u8 ] ) -> Result < Metadata > {
207
+ let mut meta = match self . db . get ( key) {
208
+ Ok ( Some ( entry) ) => Metadata :: deserialize ( & entry[ ..] ) ?,
209
+ Err ( e) => return Err ( ForcepError :: MetaDb ( e) ) ,
210
+ Ok ( None ) => return Err ( ForcepError :: NotFound ) ,
211
+ } ;
212
+ meta. last_accessed = now_since_epoch ( ) ;
213
+ meta. hits += 1 ;
214
+ self . db
215
+ . insert ( key, Metadata :: serialize ( & meta) ?)
216
+ . map_err ( ForcepError :: MetaDb ) ?;
217
+ Ok ( meta)
218
+ }
219
+
164
220
/// Iterator over the entire metadata database
165
221
pub fn metadata_iter ( & self ) -> impl Iterator < Item = Result < ( Vec < u8 > , Metadata ) > > {
166
222
self . db . iter ( ) . map ( |x| match x {
0 commit comments