2019-01-06 12:52:55 -05:00
|
|
|
/*
|
|
|
|
|
ISC License
|
|
|
|
|
|
|
|
|
|
Copyright (c) 2019, Antonio SJ Musumeci <trapexit@spawn.link>
|
|
|
|
|
|
|
|
|
|
Permission to use, copy, modify, and/or distribute this software for any
|
|
|
|
|
purpose with or without fee is hereby granted, provided that the above
|
|
|
|
|
copyright notice and this permission notice appear in all copies.
|
|
|
|
|
|
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
|
|
|
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
|
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
|
|
|
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
|
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
|
|
|
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
|
|
|
|
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
2024-02-15 19:50:54 -06:00
|
|
|
#include "nonstd/optional.hpp"
|
2019-01-06 12:52:55 -05:00
|
|
|
|
2020-12-19 15:06:08 -05:00
|
|
|
#include <cstdint>
|
2019-01-06 12:52:55 -05:00
|
|
|
#include <string>
|
2024-02-15 18:11:59 -06:00
|
|
|
#include <unordered_map>
|
2024-02-15 23:54:31 -06:00
|
|
|
#include <boost/unordered/unordered_flat_map.hpp>
|
2019-01-06 12:52:55 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
class PolicyCache
|
|
|
|
|
{
|
2024-02-15 19:53:16 -06:00
|
|
|
public:
|
2024-02-15 23:55:46 -06:00
|
|
|
typedef boost::unordered_flat_map<std::string,std::string> Map;
|
2024-02-15 20:14:42 -06:00
|
|
|
|
2019-01-06 12:52:55 -05:00
|
|
|
public:
|
2024-02-15 20:14:32 -06:00
|
|
|
PolicyCache()
|
|
|
|
|
: _max_size(256)
|
|
|
|
|
{
|
|
|
|
|
}
|
2019-01-06 12:52:55 -05:00
|
|
|
|
|
|
|
|
public:
|
2024-02-15 19:54:10 -06:00
|
|
|
void
|
2024-02-15 19:53:16 -06:00
|
|
|
insert(std::string const &key_,
|
|
|
|
|
std::string &val_)
|
|
|
|
|
{
|
2024-02-15 19:54:10 -06:00
|
|
|
_cache.insert({key_,val_});
|
2024-02-15 19:53:16 -06:00
|
|
|
}
|
2024-02-15 20:02:29 -06:00
|
|
|
|
2024-02-15 20:17:32 -06:00
|
|
|
const
|
2024-02-15 20:02:29 -06:00
|
|
|
std::string*
|
|
|
|
|
find(char const *key_)
|
|
|
|
|
{
|
2024-02-15 20:03:11 -06:00
|
|
|
Map::const_iterator i;
|
|
|
|
|
|
2024-02-15 20:03:14 -06:00
|
|
|
i = _cache.find(key_);
|
2024-02-15 20:05:10 -06:00
|
|
|
if(i == _cache.end())
|
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
|
|
return &i->second;
|
2024-02-15 20:02:29 -06:00
|
|
|
}
|
2024-02-15 20:14:42 -06:00
|
|
|
|
2024-02-15 19:54:37 -06:00
|
|
|
void
|
|
|
|
|
erase(const char *key_)
|
|
|
|
|
{
|
|
|
|
|
_cache.erase(key_);
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-15 20:02:29 -06:00
|
|
|
void
|
|
|
|
|
cleanup()
|
|
|
|
|
{
|
2024-02-15 20:14:16 -06:00
|
|
|
while(_cache.size() > _max_size)
|
2024-02-15 20:40:51 -06:00
|
|
|
_cache.erase(_cache.begin());
|
2024-02-15 20:02:29 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
clear()
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
}
|
2019-01-06 12:52:55 -05:00
|
|
|
|
|
|
|
|
private:
|
2024-02-15 19:48:28 -06:00
|
|
|
unsigned _max_size;
|
2024-02-16 00:05:23 -06:00
|
|
|
Map _cache;
|
2019-01-06 12:52:55 -05:00
|
|
|
};
|