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"
|
2024-02-22 00:11:29 -06:00
|
|
|
#include "boost/unordered/concurrent_flat_map.hpp"
|
2024-02-22 00:11:23 -06:00
|
|
|
|
2020-12-19 15:06:08 -05:00
|
|
|
#include <cstdint>
|
2019-01-06 12:52:55 -05:00
|
|
|
#include <string>
|
2024-02-22 00:11:23 -06:00
|
|
|
|
2019-01-06 12:52:55 -05:00
|
|
|
|
|
|
|
|
class PolicyCache
|
|
|
|
|
{
|
2024-02-15 19:53:16 -06:00
|
|
|
public:
|
2024-02-22 00:05:23 -06:00
|
|
|
typedef boost::concurrent_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-16 00:11:45 -06:00
|
|
|
const
|
2024-02-22 00:11:04 -06:00
|
|
|
std::string
|
2024-02-15 19:53:16 -06:00
|
|
|
insert(std::string const &key_,
|
|
|
|
|
std::string &val_)
|
|
|
|
|
{
|
2024-02-22 00:28:20 -06:00
|
|
|
_cache.insert_or_assign(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-22 00:11:56 -06:00
|
|
|
nonstd::optional<std::string>
|
2024-02-22 00:17:01 -06:00
|
|
|
find(std::string const key_)
|
2024-02-15 20:02:29 -06:00
|
|
|
{
|
2024-02-22 00:22:30 -06:00
|
|
|
nonstd::optional<std::string> rv;
|
2024-02-22 00:09:31 -06:00
|
|
|
|
2024-02-22 00:22:30 -06:00
|
|
|
_cache.visit(key_,
|
2024-02-22 00:22:40 -06:00
|
|
|
[&](Map::value_type &v_)
|
2024-02-22 00:22:30 -06:00
|
|
|
{
|
2024-02-22 00:23:09 -06:00
|
|
|
rv = v_.second;
|
2024-02-22 00:22:30 -06:00
|
|
|
});
|
2024-02-22 00:08:11 -06:00
|
|
|
|
|
|
|
|
return rv;
|
2024-02-15 20:02:29 -06:00
|
|
|
}
|
2024-02-15 20:14:42 -06:00
|
|
|
|
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
|
|
|
};
|