@@ -132,23 +132,60 @@ pub struct SelfProfileKey {
132132 pub scenario : database:: Scenario ,
133133}
134134
135+ #[ derive( Default ) ]
136+ pub struct SelfProfileCacheStats {
137+ hits : u64 ,
138+ misses : u64 ,
139+ }
140+
141+ impl SelfProfileCacheStats {
142+ pub fn get_hits ( & self ) -> u64 {
143+ self . hits
144+ }
145+ pub fn get_misses ( & self ) -> u64 {
146+ self . misses
147+ }
148+
149+ fn hit ( & mut self ) {
150+ self . hits += 1 ;
151+ }
152+ fn miss ( & mut self ) {
153+ self . misses += 1 ;
154+ }
155+ }
156+
135157/// Stores a cache of N most recently used self profiles.
136158/// The profiles are downloaded from S3 and analysed on each request to the detailed compare result
137159/// page, but the post-processed results aren't very large in memory (~50 KiB), so it makes sense
138160/// to cache them.
139161pub struct SelfProfileCache {
140162 profiles : LruCache < SelfProfileKey , SelfProfileWithAnalysis > ,
163+ stats : SelfProfileCacheStats ,
141164}
142165
143166impl SelfProfileCache {
144167 pub fn new ( cache_size : usize ) -> Self {
145168 Self {
146169 profiles : LruCache :: new ( NonZeroUsize :: new ( cache_size) . unwrap ( ) ) ,
170+ stats : Default :: default ( ) ,
147171 }
148172 }
149173
174+ pub fn get_stats ( & self ) -> & SelfProfileCacheStats {
175+ & self . stats
176+ }
177+
150178 pub fn get ( & mut self , key : & SelfProfileKey ) -> Option < SelfProfileWithAnalysis > {
151- self . profiles . get ( key) . cloned ( )
179+ match self . profiles . get ( key) {
180+ Some ( value) => {
181+ self . stats . hit ( ) ;
182+ Some ( value. clone ( ) )
183+ }
184+ None => {
185+ self . stats . miss ( ) ;
186+ None
187+ }
188+ }
152189 }
153190
154191 pub fn insert ( & mut self , key : SelfProfileKey , profile : SelfProfileWithAnalysis ) {
0 commit comments