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/TaggedCache.h>
25 #include <ripple/basics/base_uint.h>
26 #include <ripple/beast/insight/Collector.h>
27 #include <ripple/beast/utility/Journal.h>
28 #include <atomic>
29 #include <string>
30 
31 namespace ripple {
32 
33 namespace detail {
34 
39 {
40 private:
42 
43 public:
44  enum { defaultCacheTargetSize = 0 };
45 
46  using key_type = uint256;
48 
57  std::string const& name,
60  beast::insight::Collector::ptr const& collector =
62  std::size_t target_size = defaultCacheTargetSize,
64  : m_cache(name, target_size, expiration, clock, j, collector), m_gen(1)
65  {
66  }
67 
69  clock_type&
71  {
72  return m_cache.clock();
73  }
74 
80  size() const
81  {
82  return m_cache.size();
83  }
84 
89  void
91  {
92  m_cache.sweep();
93  }
94 
101  bool
103  {
104  return m_cache.touch_if_exists(key);
105  }
106 
114  void
115  insert(key_type const& key)
116  {
117  m_cache.insert(key);
118  }
119 
122  getGeneration(void) const
123  {
124  return m_gen;
125  }
126 
127  void
129  {
130  m_cache.clear();
131  ++m_gen;
132  }
133 
134  void
136  {
137  m_cache.clear();
138  m_gen = 1;
139  }
140 
141 private:
144 };
145 
146 } // namespace detail
147 
149 
150 } // namespace ripple
151 
152 #endif
ripple::TaggedCache::sweep
void sweep()
Definition: TaggedCache.h:204
std::string
STL class.
std::shared_ptr< Collector >
ripple::TaggedCache
Map/cache combination.
Definition: Application.h:64
ripple::detail::BasicFullBelowCache::m_gen
std::atomic< std::uint32_t > m_gen
Definition: FullBelowCache.h:143
ripple::detail::BasicFullBelowCache::size
std::size_t size() const
Return the number of elements in the cache.
Definition: FullBelowCache.h:80
ripple::detail::BasicFullBelowCache
Remembers which tree keys have all descendants resident.
Definition: FullBelowCache.h:38
ripple::TaggedCache::clear
void clear()
Definition: TaggedCache.h:163
std::chrono::seconds
ripple::detail::BasicFullBelowCache::clock
clock_type & clock()
Return the clock associated with the cache.
Definition: FullBelowCache.h:70
ripple::uint256
base_uint< 256 > uint256
Definition: base_uint.h:550
ripple::detail::BasicFullBelowCache::clock_type
typename CacheType::clock_type clock_type
Definition: FullBelowCache.h:47
ripple::KeyCache
TaggedCache< uint256, int, true > KeyCache
Definition: KeyCache.h:28
ripple::base_uint< 256 >
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:102
ripple::detail::BasicFullBelowCache::reset
void reset()
Definition: FullBelowCache.h:135
ripple::TaggedCache::insert
auto insert(key_type const &key, T const &value) -> std::enable_if_t<!IsKeyCache, ReturnType >
Insert the element into the container.
Definition: TaggedCache.h:411
ripple::detail::BasicFullBelowCache::getGeneration
std::uint32_t getGeneration(void) const
generation determines whether cached entry is valid
Definition: FullBelowCache.h:122
ripple::detail::BasicFullBelowCache::insert
void insert(key_type const &key)
Insert a key into the cache.
Definition: FullBelowCache.h:115
ripple::detail::BasicFullBelowCache::sweep
void sweep()
Remove expired cache items.
Definition: FullBelowCache.h:90
beast::Journal
A generic endpoint for log messages.
Definition: Journal.h:58
std::uint32_t
atomic
ripple::detail::BasicFullBelowCache::BasicFullBelowCache
BasicFullBelowCache(std::string const &name, clock_type &clock, beast::Journal j, 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::TaggedCache::size
std::size_t size() const
Returns the number of items in the container.
Definition: TaggedCache.h:98
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: RCLCensorshipDetector.h:29
ripple::TaggedCache::clock_type
beast::abstract_clock< std::chrono::steady_clock > clock_type
Definition: TaggedCache.h:62
std::size_t
ripple::TaggedCache::touch_if_exists
bool touch_if_exists(KeyComparable const &key)
Refresh the last access time on a key if present.
Definition: TaggedCache.h:185
ripple::detail::BasicFullBelowCache::m_cache
CacheType m_cache
Definition: FullBelowCache.h:142
ripple::detail::BasicFullBelowCache::defaultCacheTargetSize
@ defaultCacheTargetSize
Definition: FullBelowCache.h:44
beast::insight::NullCollector::New
static std::shared_ptr< Collector > New()
Definition: NullCollector.cpp:152
ripple::TaggedCache::clock
clock_type & clock()
Return the clock associated with the cache.
Definition: TaggedCache.h:91
ripple::detail::BasicFullBelowCache::clear
void clear()
Definition: FullBelowCache.h:128
string