summaryrefslogtreecommitdiff
path: root/src/mongo/util/invalidating_lru_cache.h
diff options
context:
space:
mode:
Diffstat (limited to 'src/mongo/util/invalidating_lru_cache.h')
-rw-r--r--src/mongo/util/invalidating_lru_cache.h56
1 files changed, 18 insertions, 38 deletions
diff --git a/src/mongo/util/invalidating_lru_cache.h b/src/mongo/util/invalidating_lru_cache.h
index 2852313ef22..c8ead4adecc 100644
--- a/src/mongo/util/invalidating_lru_cache.h
+++ b/src/mongo/util/invalidating_lru_cache.h
@@ -67,18 +67,6 @@ struct CacheNotCausallyConsistent {
};
/**
- * Helper for determining if a given type is CacheNotCausallyConsistent or not.
- */
-template <typename T>
-struct isCausallyConsistentImpl : std::true_type {};
-
-template <>
-struct isCausallyConsistentImpl<CacheNotCausallyConsistent> : std::false_type {};
-
-template <class T>
-inline constexpr bool isCausallyConsistent = isCausallyConsistentImpl<T>::value;
-
-/**
* Specifies the desired causal consistency for calls to 'get' (and 'acquire', respectively in the
* ReadThroughCache, which is its main consumer).
*/
@@ -212,8 +200,12 @@ public:
// doesn't support pinning items. Their only usage must be in the authorization mananager
// for the internal authentication user.
explicit ValueHandle(Value&& value)
- : _value(std::make_shared<StoredValue>(
- nullptr, 0, boost::none, std::move(value), Time(), Time())) {}
+ : _value(std::make_shared<StoredValue>(nullptr,
+ 0,
+ boost::none,
+ std::move(value),
+ CacheNotCausallyConsistent(),
+ CacheNotCausallyConsistent())) {}
explicit ValueHandle(Value&& value, const Time& t)
: _value(
@@ -226,15 +218,9 @@ public:
}
bool isValid() const {
- invariant(bool(*this));
return _value->isValid.loadRelaxed();
}
- const Time& getTime() const {
- invariant(bool(*this));
- return _value->time;
- }
-
Value* get() {
invariant(bool(*this));
return &_value->value;
@@ -274,16 +260,13 @@ public:
* was called, it will become invalidated.
*
* The 'time' parameter is mandatory for causally-consistent caches, but not needed otherwise
- * (since the time never changes).
+ * (since the time never changes). Using a default of '= CacheNotCausallyConsistent()' allows
+ * non-causally-consistent users to not have to pass a second parameter, but would fail
+ * compilation if causally-consistent users forget to pass it.
*/
- void insertOrAssign(const Key& key, Value&& value) {
- MONGO_STATIC_ASSERT_MSG(
- !isCausallyConsistent<Time>,
- "Time must be passed to insertOrAssign on causally consistent caches");
- insertOrAssign(key, std::move(value), Time());
- }
-
- void insertOrAssign(const Key& key, Value&& value, const Time& time) {
+ void insertOrAssign(const Key& key,
+ Value&& value,
+ const Time& time = CacheNotCausallyConsistent()) {
LockGuardWithPostUnlockDestructor guard(_mutex);
Time currentTime, currentTimeInStore;
_invalidate(&guard, key, _cache.find(key), &currentTime, &currentTimeInStore);
@@ -324,16 +307,13 @@ public:
* destroyed.
*
* The 'time' parameter is mandatory for causally-consistent caches, but not needed otherwise
- * (since the time never changes).
+ * (since the time never changes). Using a default of '= CacheNotCausallyConsistent()' allows
+ * non-causally-consistent users to not have to pass a second parameter, but would fail
+ * compilation if causally-consistent users forget to pass it.
*/
- ValueHandle insertOrAssignAndGet(const Key& key, Value&& value) {
- MONGO_STATIC_ASSERT_MSG(
- !isCausallyConsistent<Time>,
- "Time must be passed to insertOrAssignAndGet on causally consistent caches");
- return insertOrAssignAndGet(key, std::move(value), Time());
- }
-
- ValueHandle insertOrAssignAndGet(const Key& key, Value&& value, const Time& time) {
+ ValueHandle insertOrAssignAndGet(const Key& key,
+ Value&& value,
+ const Time& time = CacheNotCausallyConsistent()) {
LockGuardWithPostUnlockDestructor guard(_mutex);
Time currentTime, currentTimeInStore;
_invalidate(&guard, key, _cache.find(key), &currentTime, &currentTimeInStore);