rippled
CachedSLEs.h
1 //------------------------------------------------------------------------------
2 /*
3  This file is part of rippled: https://github.com/ripple/rippled
4  Copyright (c) 2012, 2013 Ripple Labs Inc.
5 
6  Permission to use, copy, modify, and/or distribute this software for any
7  purpose with or without fee is hereby granted, provided that the above
8  copyright notice and this permission notice appear in all copies.
9 
10  THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11  WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12  MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13  ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14  WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15  ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16  OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
18 //==============================================================================
19 
20 #ifndef RIPPLE_LEDGER_CACHEDSLES_H_INCLUDED
21 #define RIPPLE_LEDGER_CACHEDSLES_H_INCLUDED
22 
23 #include <ripple/basics/chrono.h>
24 #include <ripple/beast/container/aged_unordered_map.h>
25 #include <ripple/protocol/STLedgerEntry.h>
26 #include <memory>
27 #include <mutex>
28 
29 namespace ripple {
30 
33 {
34 public:
36 
38 
39  CachedSLEs(CachedSLEs const&) = delete;
40  CachedSLEs&
41  operator=(CachedSLEs const&) = delete;
42 
43  template <class Rep, class Period>
45  std::chrono::duration<Rep, Period> const& timeToLive,
46  Stopwatch& clock)
47  : timeToLive_(timeToLive), map_(clock)
48  {
49  }
50 
55  void
56  expire();
57 
65  template <class Handler>
67  fetch(digest_type const& digest, Handler const& h)
68  {
69  {
70  std::lock_guard lock(mutex_);
71  auto iter = map_.find(digest);
72  if (iter != map_.end())
73  {
74  ++hit_;
75  map_.touch(iter);
76  return iter->second;
77  }
78  }
79  auto sle = h();
80  if (!sle)
81  return nullptr;
82  std::lock_guard lock(mutex_);
83  ++miss_;
84  auto const [it, inserted] = map_.emplace(digest, std::move(sle));
85  if (!inserted)
86  map_.touch(it);
87  return it->second;
88  }
89 
91  double
92  rate() const;
93 
94 private:
97  std::mutex mutable mutex_;
100  digest_type,
101  value_type,
105 };
106 
107 } // namespace ripple
108 
109 #endif
ripple::CachedSLEs::mutex_
std::mutex mutex_
Definition: CachedSLEs.h:97
std::shared_ptr
STL class.
ripple::CachedSLEs::fetch
value_type fetch(digest_type const &digest, Handler const &h)
Fetch an item from the cache.
Definition: CachedSLEs.h:67
std::chrono::duration
ripple::CachedSLEs::miss_
std::size_t miss_
Definition: CachedSLEs.h:96
std::lock_guard
STL class.
ripple::CachedSLEs::timeToLive_
Stopwatch::duration timeToLive_
Definition: CachedSLEs.h:98
beast::abstract_clock< std::chrono::steady_clock >::clock_type
std::chrono::steady_clock clock_type
Definition: abstract_clock.h:64
ripple::uint256
base_uint< 256 > uint256
Definition: base_uint.h:457
ripple::digest
static Hasher::result_type digest(void const *data, std::size_t size) noexcept
Definition: tokens.cpp:47
ripple::base_uint< 256 >
ripple::CachedSLEs::map_
beast::aged_unordered_map< digest_type, value_type, Stopwatch::clock_type, hardened_hash< strong_hash > > map_
Definition: CachedSLEs.h:104
ripple::CachedSLEs
Caches SLEs by their digest.
Definition: CachedSLEs.h:32
ripple::CachedSLEs::expire
void expire()
Discard expired entries.
Definition: CachedSLEs.cpp:26
ripple::CachedSLEs::operator=
CachedSLEs & operator=(CachedSLEs const &)=delete
ripple::CachedSLEs::rate
double rate() const
Returns the fraction of cache hits.
Definition: CachedSLEs.cpp:48
ripple::hardened_hash
Seed functor once per construction.
Definition: hardened_hash.h:96
ripple::CachedSLEs::CachedSLEs
CachedSLEs(std::chrono::duration< Rep, Period > const &timeToLive, Stopwatch &clock)
Definition: CachedSLEs.h:44
ripple::CachedSLEs::digest_type
uint256 digest_type
Definition: CachedSLEs.h:35
beast::abstract_clock< std::chrono::steady_clock >
memory
beast::detail::aged_unordered_container
Associative container where each element is also indexed by time.
Definition: aged_unordered_container.h:85
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: RCLCensorshipDetector.h:29
ripple::CachedSLEs::hit_
std::size_t hit_
Definition: CachedSLEs.h:95
mutex
std::size_t
ripple::CachedSLEs::value_type
std::shared_ptr< SLE const > value_type
Definition: CachedSLEs.h:37
beast::abstract_clock< std::chrono::steady_clock >::duration
typename std::chrono::steady_clock ::duration duration
Definition: abstract_clock.h:62
ripple::CachedSLEs::CachedSLEs
CachedSLEs(CachedSLEs const &)=delete