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
// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0
// This file is part of Frontier.
//
// Copyright (c) 2022 Parity Technologies (UK) Ltd.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.

use scale_codec::Encode;
use schnellru::{LruMap, Unlimited};

pub struct LRUCacheByteLimited<K, V> {
	cache: LruMap<K, V, Unlimited>,
	max_size: u64,
	metrics: Option<LRUCacheByteLimitedMetrics>,
	size: u64,
}

impl<K: Eq + core::hash::Hash, V: Encode> LRUCacheByteLimited<K, V> {
	pub fn new(
		cache_name: &'static str,
		max_size: u64,
		prometheus_registry: Option<prometheus_endpoint::Registry>,
	) -> Self {
		let metrics = match prometheus_registry {
			Some(registry) => match LRUCacheByteLimitedMetrics::register(cache_name, &registry) {
				Ok(metrics) => Some(metrics),
				Err(e) => {
					log::error!(target: "eth-cache", "Failed to register metrics: {:?}", e);
					None
				}
			},
			None => None,
		};

		Self {
			cache: LruMap::new(Unlimited),
			max_size,
			metrics,
			size: 0,
		}
	}
	pub fn get(&mut self, k: &K) -> Option<&V> {
		if let Some(v) = self.cache.get(k) {
			// Update metrics
			if let Some(metrics) = &self.metrics {
				metrics.hits.inc();
			}
			Some(v)
		} else {
			// Update metrics
			if let Some(metrics) = &self.metrics {
				metrics.miss.inc();
			}
			None
		}
	}
	pub fn put(&mut self, k: K, v: V) {
		// Handle size limit
		self.size += v.encoded_size() as u64;

		while self.size > self.max_size {
			if let Some((_, v)) = self.cache.pop_oldest() {
				let v_size = v.encoded_size() as u64;
				self.size -= v_size;
			} else {
				break;
			}
		}

		// Add entry in cache
		self.cache.insert(k, v);
		// Update metrics
		if let Some(metrics) = &self.metrics {
			metrics.size.set(self.size);
		}
	}
}

struct LRUCacheByteLimitedMetrics {
	hits: prometheus::IntCounter,
	miss: prometheus::IntCounter,
	size: prometheus_endpoint::Gauge<prometheus_endpoint::U64>,
}

impl LRUCacheByteLimitedMetrics {
	pub(crate) fn register(
		cache_name: &'static str,
		registry: &prometheus_endpoint::Registry,
	) -> Result<Self, prometheus_endpoint::PrometheusError> {
		Ok(Self {
			hits: prometheus_endpoint::register(
				prometheus::IntCounter::new(
					format!("frontier_eth_{}_hits", cache_name),
					format!("Hits of eth {} cache.", cache_name),
				)?,
				registry,
			)?,
			miss: prometheus_endpoint::register(
				prometheus::IntCounter::new(
					format!("frontier_eth_{}_miss", cache_name),
					format!("Misses of eth {} cache.", cache_name),
				)?,
				registry,
			)?,
			size: prometheus_endpoint::register(
				prometheus_endpoint::Gauge::new(
					format!("frontier_eth_{}_size", cache_name),
					format!("Size of eth {} data cache.", cache_name),
				)?,
				registry,
			)?,
		})
	}
}

#[cfg(test)]
mod tests {
	use super::*;

	#[test]
	fn test_size_limit() {
		let mut cache = LRUCacheByteLimited::new("name", 10, None);
		cache.put(0, "abcd");
		assert!(cache.get(&0).is_some());
		cache.put(1, "efghij");
		assert!(cache.get(&1).is_some());
		cache.put(2, "k");
		assert!(cache.get(&2).is_some());
		// Entry (0,  "abcd") should be deleted
		assert!(cache.get(&0).is_none());
		// Size should be 7 now, so we should be able to add a value of size 3
		cache.put(3, "lmn");
		assert!(cache.get(&3).is_some());
	}
}