rippled
FullBelowCache.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_SHAMAP_FULLBELOWCACHE_H_INCLUDED
21 #define RIPPLE_SHAMAP_FULLBELOWCACHE_H_INCLUDED
22 
23 #include <ripple/basics/KeyCache.h>
24 #include <ripple/basics/base_uint.h>
25 #include <ripple/beast/insight/Collector.h>
26 #include <atomic>
27 #include <string>
28 
29 namespace ripple {
30 
31 namespace detail {
32 
36 template <class Key>
38 {
39 private:
41 
42 public:
43  enum { defaultCacheTargetSize = 0 };
44 
45  using key_type = Key;
46  using size_type = typename CacheType::size_type;
48 
57  std::string const& name,
59  beast::insight::Collector::ptr const& collector =
61  std::size_t target_size = defaultCacheTargetSize,
63  : m_cache(name, clock, collector, target_size, expiration), m_gen(1)
64  {
65  }
66 
68  clock_type&
70  {
71  return m_cache.clock();
72  }
73 
78  size_type
79  size() const
80  {
81  return m_cache.size();
82  }
83 
88  void
90  {
91  m_cache.sweep();
92  }
93 
100  bool
102  {
103  return m_cache.touch_if_exists(key);
104  }
105 
113  void
114  insert(key_type const& key)
115  {
116  m_cache.insert(key);
117  }
118 
121  getGeneration(void) const
122  {
123  return m_gen;
124  }
125 
126  void
128  {
129  m_cache.clear();
130  ++m_gen;
131  }
132 
133  void
135  {
136  m_cache.clear();
137  m_gen = 1;
138  }
139 
140 private:
143 };
144 
145 } // namespace detail
146 
148 
149 } // namespace ripple
150 
151 #endif
ripple::KeyCache::size
size_type size() const
Returns the number of items in the container.
Definition: KeyCache.h:153
std::string
STL class.
std::shared_ptr< Collector >
ripple::detail::BasicFullBelowCache
Remembers which tree keys have all descendants resident.
Definition: FullBelowCache.h:37
ripple::detail::BasicFullBelowCache::defaultCacheTargetSize
@ defaultCacheTargetSize
Definition: FullBelowCache.h:43
ripple::detail::BasicFullBelowCache::clock_type
typename CacheType::clock_type clock_type
Definition: FullBelowCache.h:47
std::chrono::seconds
ripple::detail::BasicFullBelowCache::m_gen
std::atomic< std::uint32_t > m_gen
Definition: FullBelowCache.h:142
ripple::detail::BasicFullBelowCache::reset
void reset()
Definition: FullBelowCache.h:134
ripple::KeyCache::touch_if_exists
bool touch_if_exists(KeyComparable const &key)
Refresh the last access time on a key if present.
Definition: KeyCache.h:234
ripple::detail::BasicFullBelowCache::touch_if_exists
bool touch_if_exists(key_type const &key)
Refresh the last access time of an item, if it exists.
Definition: FullBelowCache.h:101
ripple::KeyCache::insert
bool insert(Key const &key)
Insert the specified key.
Definition: KeyCache.h:213
ripple::detail::BasicFullBelowCache::BasicFullBelowCache
BasicFullBelowCache(std::string const &name, clock_type &clock, beast::insight::Collector::ptr const &collector=beast::insight::NullCollector::New(), std::size_t target_size=defaultCacheTargetSize, std::chrono::seconds expiration=std::chrono::minutes{2})
Construct the cache.
Definition: FullBelowCache.h:56
ripple::detail::BasicFullBelowCache::sweep
void sweep()
Remove expired cache items.
Definition: FullBelowCache.h:89
ripple::KeyCache< Key >::clock_type
beast::abstract_clock< std::chrono::steady_clock > clock_type
Definition: KeyCache.h:47
ripple::detail::BasicFullBelowCache::clock
clock_type & clock()
Return the clock associated with the cache.
Definition: FullBelowCache.h:69
std::uint32_t
atomic
ripple::KeyCache::sweep
void sweep()
Remove stale entries from the cache.
Definition: KeyCache.h:267
ripple::KeyCache::clear
void clear()
Empty the cache.
Definition: KeyCache.h:161
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: RCLCensorshipDetector.h:29
ripple::detail::BasicFullBelowCache::getGeneration
std::uint32_t getGeneration(void) const
generation determines whether cached entry is valid
Definition: FullBelowCache.h:121
ripple::KeyCache< Key >::size_type
typename map_type::size_type size_type
Definition: KeyCache.h:87
ripple::detail::BasicFullBelowCache::size_type
typename CacheType::size_type size_type
Definition: FullBelowCache.h:46
ripple::detail::BasicFullBelowCache::size
size_type size() const
Return the number of elements in the cache.
Definition: FullBelowCache.h:79
ripple::detail::BasicFullBelowCache::insert
void insert(key_type const &key)
Insert a key into the cache.
Definition: FullBelowCache.h:114
std::size_t
ripple::KeyCache::clock
clock_type & clock()
Return the clock associated with the cache.
Definition: KeyCache.h:146
ripple::KeyCache< Key >
ripple::detail::BasicFullBelowCache::m_cache
KeyCache< Key > m_cache
Definition: FullBelowCache.h:141
beast::insight::NullCollector::New
static std::shared_ptr< Collector > New()
Definition: NullCollector.cpp:152
ripple::detail::BasicFullBelowCache::clear
void clear()
Definition: FullBelowCache.h:127
ripple::detail::BasicFullBelowCache::key_type
Key key_type
Definition: FullBelowCache.h:45
string