rippled
CachedSLEs.cpp
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 #include <ripple/ledger/CachedSLEs.h>
21 #include <vector>
22 
23 namespace ripple {
24 
25 void
27 {
29  {
30  auto const expireTime = map_.clock().now() - timeToLive_;
31  std::lock_guard lock(mutex_);
32  for (auto iter = map_.chronological.begin();
33  iter != map_.chronological.end();
34  ++iter)
35  {
36  if (iter.when() > expireTime)
37  break;
38  if (iter->second.unique())
39  {
40  trash.emplace_back(std::move(iter->second));
41  iter = map_.erase(iter);
42  }
43  }
44  }
45 }
46 
47 double
49 {
50  std::lock_guard lock(mutex_);
51  auto const tot = hit_ + miss_;
52  if (tot == 0)
53  return 0;
54  return double(hit_) / tot;
55 }
56 
57 } // namespace ripple
ripple::CachedSLEs::mutex_
std::mutex mutex_
Definition: CachedSLEs.h:97
vector
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
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::expire
void expire()
Discard expired entries.
Definition: CachedSLEs.cpp:26
ripple::CachedSLEs::rate
double rate() const
Returns the fraction of cache hits.
Definition: CachedSLEs.cpp:48
std::vector::emplace_back
T emplace_back(T... args)
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