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/base_uint.h>
24 #include <ripple/basics/KeyCache.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
44  {
46  };
47 
48  using key_type = Key;
49  using size_type = typename CacheType::size_type;
51 
60  beast::insight::Collector::ptr const& collector =
62  std::size_t target_size = defaultCacheTargetSize,
64  : m_cache (name, clock, collector, target_size,
65  expiration)
66  , m_gen (1)
67  {
68  }
69 
72  {
73  return m_cache.clock ();
74  }
75 
80  size_type size () const
81  {
82  return m_cache.size ();
83  }
84 
89  void sweep ()
90  {
91  m_cache.sweep ();
92  }
93 
100  bool touch_if_exists (key_type const& key)
101  {
102  return m_cache.touch_if_exists (key);
103  }
104 
112  void insert (key_type const& key)
113  {
114  m_cache.insert (key);
115  }
116 
119  {
120  return m_gen;
121  }
122 
123  void clear ()
124  {
125  m_cache.clear ();
126  ++m_gen;
127  }
128 
129  void reset ()
130  {
131  m_cache.clear();
132  m_gen = 1;
133  }
134 
135 private:
138 };
139 
140 } // detail
141 
143 
144 }
145 
146 #endif
ripple::detail::BasicFullBelowCache::defaultCacheTargetSize
@ defaultCacheTargetSize
Definition: FullBelowCache.h:45
ripple::KeyCache::size
size_type size() const
Returns the number of items in the container.
Definition: KeyCache.h:144
std::string
STL class.
std::shared_ptr< Collector >
ripple::detail::BasicFullBelowCache
Remembers which tree keys have all descendants resident.
Definition: FullBelowCache.h:37
ripple::KeyCache< Key >::clock_type
beast::abstract_clock< std::chrono::steady_clock > clock_type
Definition: KeyCache.h:48
ripple::detail::BasicFullBelowCache< uint256 >::clock_type
typename CacheType::clock_type clock_type
Definition: FullBelowCache.h:50
std::chrono::seconds
ripple::detail::BasicFullBelowCache::m_gen
std::atomic< std::uint32_t > m_gen
Definition: FullBelowCache.h:137
ripple::detail::BasicFullBelowCache::reset
void reset()
Definition: FullBelowCache.h:129
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:217
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:100
ripple::base_uint< 256 >
ripple::KeyCache::insert
bool insert(Key const &key)
Insert the specified key.
Definition: KeyCache.h:198
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:59
ripple::detail::BasicFullBelowCache::sweep
void sweep()
Remove expired cache items.
Definition: FullBelowCache.h:89
ripple::detail::BasicFullBelowCache::clock
clock_type & clock()
Return the clock associated with the cache.
Definition: FullBelowCache.h:71
std::uint32_t
atomic
ripple::KeyCache::sweep
void sweep()
Remove stale entries from the cache.
Definition: KeyCache.h:248
ripple::KeyCache::clear
void clear()
Empty the cache.
Definition: KeyCache.h:151
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:118
ripple::KeyCache< Key >::size_type
typename map_type::size_type size_type
Definition: KeyCache.h:85
ripple::detail::BasicFullBelowCache< uint256 >::size_type
typename CacheType::size_type size_type
Definition: FullBelowCache.h:49
ripple::detail::BasicFullBelowCache::size
size_type size() const
Return the number of elements in the cache.
Definition: FullBelowCache.h:80
ripple::detail::BasicFullBelowCache::insert
void insert(key_type const &key)
Insert a key into the cache.
Definition: FullBelowCache.h:112
std::size_t
ripple::KeyCache::clock
clock_type & clock()
Return the clock associated with the cache.
Definition: KeyCache.h:138
ripple::KeyCache< uint256 >
ripple::detail::BasicFullBelowCache::m_cache
KeyCache< Key > m_cache
Definition: FullBelowCache.h:136
beast::insight::NullCollector::New
static std::shared_ptr< Collector > New()
Definition: NullCollector.cpp:140
ripple::detail::BasicFullBelowCache::clear
void clear()
Definition: FullBelowCache.h:123
string