Add optional parameter pruneHook to EvictingCacheMap::set(..)
authorDmitry Panin <panin@fb.com>
Sat, 8 Nov 2014 01:31:02 +0000 (17:31 -0800)
committerDave Watson <davejwatson@fb.com>
Wed, 19 Nov 2014 20:52:09 +0000 (12:52 -0800)
Summary:
Inside `set()` we can do pruning, but it will happen
with default pruneHook.
Adding it as an optional param makes API more convenient.
(Instead, the users of API could just call `setPruneHook(pruneHook)` before `set`,
and then `setPruneHook(nullptr)` afterwards -- but it looks too ugly)

Test Plan:
```
fbconfig -r folly/ && fbmake runtests
```
passes:
```
Summary (total time 60.11s):
PASS: 1758
FAIL: 0
SKIP: 0
FATAL: 0
TIMEOUT: 0
```

Reviewed By: njormrod@fb.com

Subscribers: trunkagent, agartrell, njormrod, folly-diffs@

FB internal diff: D1665690

Tasks: 5551091

Signature: t1:1665690:1415391406:e4d2a956f9212aed70ab518159dbb19553764ce4

folly/EvictingCacheMap.h

index 89bc3d5438835ac00c647cce7a8da7b78506faa0..c32f7821a808517f7ac4104290a767810afc3360 100644 (file)
@@ -166,11 +166,14 @@ class EvictingCacheMap : private boost::noncopyable {
    * If you intend to resize dynamically using this, then picking an index size
    * that works well and initializing with corresponding maxSize is the only
    * reasonable option.
+   *
+   * @param maxSize new maximum size of the cache map.
+   * @param pruneHook callback to use on eviction.
    */
-  void setMaxSize(size_t maxSize) {
+  void setMaxSize(size_t maxSize, PruneHookCall pruneHook = nullptr) {
     if (maxSize != 0 && maxSize < size()) {
       // Prune the excess elements with our new constraints.
-      prune(std::max(size() - maxSize, clearSize_));
+      prune(std::max(size() - maxSize, clearSize_), pruneHook);
     }
     maxSize_ = maxSize;
   }
@@ -286,8 +289,12 @@ class EvictingCacheMap : private boost::noncopyable {
    * @param promote boolean flag indicating whether or not to move something
    *     to the front of an LRU.  This only really matters if you're setting
    *     a value that already exists.
+   * @param pruneHook callback to use on eviction (if it occurs).
    */
-  void set(const TKey& key, TValue value, bool promote = true) {
+  void set(const TKey& key,
+           TValue value,
+           bool promote = true,
+           PruneHookCall pruneHook = nullptr) {
     auto it = findInIndex(key);
     if (it != index_.end()) {
       it->pr.second = std::move(value);
@@ -302,7 +309,7 @@ class EvictingCacheMap : private boost::noncopyable {
 
       // no evictions if maxSize_ is 0 i.e. unlimited capacity
       if (maxSize_ > 0 && size() > maxSize_) {
-        prune(clearSize_);
+        prune(clearSize_, pruneHook);
       }
     }
   }
@@ -323,8 +330,8 @@ class EvictingCacheMap : private boost::noncopyable {
     return index_.empty();
   }
 
-  void clear() {
-    prune(size());
+  void clear(PruneHookCall pruneHook = nullptr) {
+    prune(size(), pruneHook);
   }
 
   /**