proxygen
LRUInMemoryCache.h
Go to the documentation of this file.
1 /*
2  * Copyright 2017-present Facebook, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 #pragma once
17 
18 #include <utility>
19 
20 #include <folly/dynamic.h>
22 #include <folly/Optional.h>
24 
25 namespace wangle {
26 
31 template<typename K, typename V, typename MutexT>
33  public:
37  explicit LRUInMemoryCache(size_t capacity) : cache_(capacity) {}
38  ~LRUInMemoryCache() = default;
39 
40  folly::Optional<V> get(const K& key);
41  void put(const K& key, const V& val);
42  bool remove(const K& key);
43  size_t size() const;
44  void clear();
45 
47 
53 
59 
64  return getVersion() != version;
65  }
66 
67  private:
68 
69  // must be called under a write lock
71  ++version_;
72  // if a uint64_t is incremented a billion times a second, it will still take
73  // 585 years to wrap around, so don't bother
74  }
75 
77  // Version always starts at 1
79  // mutable so we can take read locks in const methods
80  mutable MutexT cacheLock_;
81 
82 };
83 
84 }
85 
void put(const K &key, const V &val)
folly::Optional< std::pair< folly::dynamic, CacheDataVersion > > convertToKeyValuePairs() noexcept
folly::EvictingCacheMap< K, V > cache_
uint64_t CacheDataVersion
CacheDataVersion getVersion() const
bool hasChangedSince(CacheDataVersion version) const
double val
Definition: String.cpp:273
CacheDataVersion version_
requires E e noexcept(noexcept(s.error(std::move(e))))
CacheDataVersion loadData(const folly::dynamic &kvPairs) noexcept
ProtocolVersion version
LRUInMemoryCache(size_t capacity)