hyperledger/iroha
Iroha - A simple, decentralized ledger http://iroha.tech
abstract_cache.hpp
Go to the documentation of this file.
1 
6 #ifndef IROHA_ABSTRACT_CACHE_HPP
7 #define IROHA_ABSTRACT_CACHE_HPP
8 
9 #include <boost/optional.hpp>
10 #include <list>
11 #include <shared_mutex>
12 #include <string>
13 
14 namespace iroha {
15  namespace cache {
25  template <typename KeyType, typename ValueType, typename T>
26  class AbstractCache {
27  public:
31  uint32_t getIndexSizeHigh() const {
32  // shared lock
33  std::shared_lock<std::shared_timed_mutex> lock(access_mutex_);
34  return constUnderlying().getIndexSizeHighImpl();
35  }
36 
40  uint32_t getCacheItemCount() const {
41  // shared lock
42  std::shared_lock<std::shared_timed_mutex> lock(access_mutex_);
43  return constUnderlying().getCacheItemCountImpl();
44  }
45 
56  void addItem(const KeyType &key, const ValueType &value) {
57  // exclusive lock
58  std::lock_guard<std::shared_timed_mutex> lock(access_mutex_);
59  underlying().addItemImpl(key, value);
60  }
61 
67  boost::optional<ValueType> findItem(const KeyType &key) const {
68  std::shared_lock<std::shared_timed_mutex> lock(access_mutex_);
69  return constUnderlying().findItemImpl(key);
70  }
71 
72  private:
73  const T &constUnderlying() const {
74  return static_cast<const T &>(*this);
75  }
76  T &underlying() {
77  return static_cast<T &>(*this);
78  }
79 
80  mutable std::shared_timed_mutex access_mutex_;
81  };
82  } // namespace cache
83 } // namespace iroha
84 
85 #endif // IROHA_ABSTRACT_CACHE_HPP
void addItem(const KeyType &key, const ValueType &value)
Definition: abstract_cache.hpp:56
boost::optional< ValueType > findItem(const KeyType &key) const
Definition: abstract_cache.hpp:67
uint32_t getIndexSizeHigh() const
Definition: abstract_cache.hpp:31
Definition: block_query.hpp:15
Definition: abstract_cache.hpp:26
uint32_t getCacheItemCount() const
Definition: abstract_cache.hpp:40