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 getIndexSizeLow() const {
41  // shared lock
42  std::shared_lock<std::shared_timed_mutex> lock(access_mutex_);
43  return constUnderlying().getIndexSizeLowImpl();
44  }
45 
49  uint32_t getCacheItemCount() const {
50  // shared lock
51  std::shared_lock<std::shared_timed_mutex> lock(access_mutex_);
52  return constUnderlying().getCacheItemCountImpl();
53  }
54 
65  void addItem(const KeyType &key, const ValueType &value) {
66  // exclusive lock
67  std::lock_guard<std::shared_timed_mutex> lock(access_mutex_);
68  underlying().addItemImpl(key, value);
69  }
70 
76  boost::optional<ValueType> findItem(const KeyType &key) const {
77  std::shared_lock<std::shared_timed_mutex> lock(access_mutex_);
78  return constUnderlying().findItemImpl(key);
79  }
80 
81  private:
82  const T &constUnderlying() const {
83  return static_cast<const T &>(*this);
84  }
85  T &underlying() {
86  return static_cast<T &>(*this);
87  }
88 
89  mutable std::shared_timed_mutex access_mutex_;
90  };
91  } // namespace cache
92 } // namespace iroha
93 
94 #endif // IROHA_ABSTRACT_CACHE_HPP
void addItem(const KeyType &key, const ValueType &value)
Definition: abstract_cache.hpp:65
uint32_t getIndexSizeLow() const
Definition: abstract_cache.hpp:40
uint32_t getIndexSizeHigh() const
Definition: abstract_cache.hpp:31
boost::optional< ValueType > findItem(const KeyType &key) const
Definition: abstract_cache.hpp:76
uint32_t getCacheItemCount() const
Definition: abstract_cache.hpp:49
Definition: block_query.hpp:16
Definition: abstract_cache.hpp:26