]> git.apps.os.sepia.ceph.com Git - ceph.git/commitdiff
simple_cache.hpp: add pinning
authorSamuel Just <samuel.just@dreamhost.com>
Fri, 27 Apr 2012 17:00:08 +0000 (10:00 -0700)
committerSamuel Just <samuel.just@dreamhost.com>
Fri, 27 Apr 2012 21:28:08 +0000 (14:28 -0700)
Signed-off-by: Samuel Just <samuel.just@dreamhost.com>
src/common/simple_cache.hpp

index 78e4cae43de5e26e3afcf986c9e2bf524f649a3a..7d06235188802f34884e67e70fd22875514f37ef 100644 (file)
@@ -27,6 +27,7 @@ class SimpleLRU {
   size_t max_size;
   map<K, typename list<pair<K, V> >::iterator> contents;
   list<pair<K, V> > lru;
+  map<K, V> pinned;
 
   void trim_cache() {
     while (lru.size() > max_size) {
@@ -35,9 +36,33 @@ class SimpleLRU {
     }
   }
 
+  void _add(K key, V value) {
+    lru.push_front(make_pair(key, value));
+    contents[key] = lru.begin();
+    trim_cache();
+  }
+
 public:
   SimpleLRU(size_t max_size) : lock("SimpleLRU::lock"), max_size(max_size) {}
 
+  void pin(K key, V val) {
+    Mutex::Locker l(lock);
+    pinned.insert(make_pair(key, val));
+  }
+
+  void clear_pinned() {
+    Mutex::Locker l(lock);
+    for (typename map<K, V>::iterator i = pinned.begin();
+        i != pinned.end();
+        ++i) {
+      if (!contents.count(i->first))
+       _add(i->first, i->second);
+      else
+       lru.splice(lru.begin(), lru, contents[i->first]);
+    }
+    pinned.clear();
+  }
+
   void set_size(size_t new_size) {
     Mutex::Locker l(lock);
     max_size = new_size;
@@ -53,14 +78,16 @@ public:
       lru.splice(lru.begin(), lru, loc);
       return true;
     }
+    if (pinned.count(key)) {
+      *out = pinned[key];
+      return true;
+    }
     return false;
   }
 
   void add(K key, V value) {
     Mutex::Locker l(lock);
-    lru.push_front(make_pair(key, value));
-    contents[key] = lru.begin();
-    trim_cache();
+    _add(key, value);
   }
 };