cjcchen commented on code in PR #170:
URL:
https://github.com/apache/incubator-resilientdb/pull/170#discussion_r1966294250
##########
common/lru/lru_cache.cpp:
##########
@@ -0,0 +1,97 @@
+#include "lru_cache.h"
+
+namespace resdb {
+
+template <typename KeyType, typename ValueType>
+LRUCache<KeyType, ValueType>::LRUCache(int capacity) {
+ capacity_ = capacity;
+ cache_hits_ = 0;
+ cache_misses_ = 0;
+}
+
+template <typename KeyType, typename ValueType>
+LRUCache<KeyType, ValueType>::~LRUCache() {
+ lookup_.clear();
+ key_list_.clear();
+ rlookup_.clear();
+}
+
+template <typename KeyType, typename ValueType>
+ValueType LRUCache<KeyType, ValueType>::Get(KeyType key) {
+ if (lookup_.find(key) == lookup_.end()) {
+ cache_misses_++;
+ return ValueType();
+ }
+
+ // Move accessed key to front of key list. This marks the key as used
+ key_list_.splice(key_list_.begin(), key_list_, rlookup_[key]);
+
+ cache_hits_++;
+ return lookup_[key];
+}
+
+template <typename KeyType, typename ValueType>
+void LRUCache<KeyType, ValueType>::Put(KeyType key, ValueType value) {
+ if (lookup_.find(key) == lookup_.end()) {
+ if (key_list_.size() == capacity_) {
+ KeyType lru_key = key_list_.back();
+ key_list_.pop_back();
+ lookup_.erase(lru_key);
+ rlookup_.erase(lru_key);
+ }
+ key_list_.push_front(key);
+ rlookup_[key] = key_list_.begin();
+ } else {
+ lookup_[key] = value;
Review Comment:
could we remove this? you will set the lookup_ below.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]