---
title: llarp/util/decaying_hashtable.hpp

---

# llarp/util/decaying_hashtable.hpp



## Namespaces

| Name           |
| -------------- |
| **[llarp](Namespaces/namespacellarp.md)** <br>[crypto.hpp] |
| **[llarp::util](Namespaces/namespacellarp_1_1util.md)**  |

## Classes

|                | Name           |
| -------------- | -------------- |
| struct | **[llarp::util::DecayingHashTable](Classes/structllarp_1_1util_1_1DecayingHashTable.md)**  |




## Source code

```cpp
#pragma once

#include "time.hpp"
#include <unordered_map>

namespace llarp::util
{
  template <typename Key_t, typename Value_t, typename Hash_t = std::hash<Key_t>>
  struct DecayingHashTable
  {
    DecayingHashTable(std::chrono::milliseconds cacheInterval = 1h) : m_CacheInterval(cacheInterval)
    {}

    void
    Decay(llarp_time_t now)
    {
      EraseIf([&](const auto& item) { return item.second.second + m_CacheInterval <= now; });
    }

    bool
    Has(const Key_t& k) const
    {
      return m_Values.find(k) != m_Values.end();
    }

    bool
    Put(Key_t key, Value_t value, llarp_time_t now = 0s)
    {
      if (now == 0s)
        now = llarp::time_now_ms();
      return m_Values.try_emplace(std::move(key), std::make_pair(std::move(value), now)).second;
    }

    std::optional<Value_t>
    Get(Key_t k) const
    {
      const auto itr = m_Values.find(k);
      if (itr == m_Values.end())
        return std::nullopt;
      return itr->second.first;
    }

    void
    Remove(const Key_t& key)
    {
      m_Values.erase(key);
    }

   private:
    template <typename Predicate_t>
    void
    EraseIf(Predicate_t pred)
    {
      for (auto i = m_Values.begin(), last = m_Values.end(); i != last;)
      {
        if (pred(*i))
        {
          i = m_Values.erase(i);
        }
        else
        {
          ++i;
        }
      }
    }

    llarp_time_t m_CacheInterval;
    std::unordered_map<Key_t, std::pair<Value_t, llarp_time_t>, Hash_t> m_Values;
  };
}  // namespace llarp::util
```


-------------------------------

Updated on 2023-04-27 at 04:17:18 +0000
