| 1 |
/* |
| 2 |
* (C) Copyright 2013 ECMWF. |
| 3 |
* |
| 4 |
* This software is licensed under the terms of the Apache Licence Version 2.0 |
| 5 |
* which can be obtained at http://www.apache.org/licenses/LICENSE-2.0. |
| 6 |
* In applying this licence, ECMWF does not waive the privileges and immunities |
| 7 |
* granted to it by virtue of its status as an intergovernmental organisation |
| 8 |
* nor does it submit to any jurisdiction. |
| 9 |
*/ |
| 10 |
|
| 11 |
#include <functional> |
| 12 |
#include <map> |
| 13 |
#include <mutex> |
| 14 |
#include <string> |
| 15 |
|
| 16 |
#include "atlas/runtime/Log.h" |
| 17 |
#include "atlas/util/ObjectHandle.h" |
| 18 |
|
| 19 |
namespace atlas { |
| 20 |
namespace util { |
| 21 |
|
| 22 |
template <typename Key, typename Value> |
| 23 |
class Cache { |
| 24 |
public: |
| 25 |
using key_type = Key; |
| 26 |
using value_type = Value; |
| 27 |
using creator_type = std::function<value_type*()>; |
| 28 |
|
| 29 |
Cache(const std::string& name): name_(name) {} |
| 30 |
|
| 31 |
virtual ~Cache() {} |
| 32 |
|
| 33 |
ObjectHandle<value_type> get_or_create(const key_type& key, const creator_type& creator) { |
| 34 |
return get_or_create(key, key, creator); |
| 35 |
} |
| 36 |
|
| 37 |
ObjectHandle<value_type> get_or_create(const key_type& key, const key_type& remove_key, |
| 38 |
const creator_type& creator) { |
| 39 |
std::lock_guard<std::mutex> guard(lock_); |
| 40 |
auto it = map_.find(key); |
| 41 |
if (it != map_.end()) { |
| 42 |
ObjectHandle<value_type> value = it->second; |
| 43 |
if (value) { |
| 44 |
Log::debug() << "Key \"" << key << "\" was found in cache \"" << name_ << "\"" << std::endl; |
| 45 |
return value; |
| 46 |
} |
| 47 |
else { |
| 48 |
Log::debug() << "Key \"" << key << "\" was found in cache \"" << name_ |
| 49 |
<< "\" but is not valid. Revert to not found." << std::endl; |
| 50 |
} |
| 51 |
} |
| 52 |
Log::debug() << "Key \"" << key << "\" not found in cache \"" << name_ |
| 53 |
<< "\" , creating new, removable with key \"" << remove_key << "\"" << std::endl; |
| 54 |
ObjectHandle<value_type> value(creator()); |
| 55 |
map_[key] = value; |
| 56 |
remove_[remove_key].emplace_back(key); |
| 57 |
return value; |
| 58 |
} |
| 59 |
|
| 60 |
void remove(const key_type& remove_key) { |
| 61 |
std::lock_guard<std::mutex> guard(lock_); |
| 62 |
if (remove_.find(remove_key) != remove_.end()) { |
| 63 |
for (auto& key : remove_[remove_key]) { |
| 64 |
bool erased = map_.erase(key); |
| 65 |
if (erased) { |
| 66 |
if (remove_key == key) { |
| 67 |
Log::debug() << "Erased key \"" << key << "\" from cache \"" << name_ << "\"." << std::endl; |
| 68 |
} |
| 69 |
else { |
| 70 |
Log::debug() << "Erased key \"" << key << "\" via remove_key \"" << remove_key |
| 71 |
<< "\" from cache \"" << name_ << "\"." << std::endl; |
| 72 |
} |
| 73 |
} |
| 74 |
else { |
| 75 |
Log::debug() << "Tried to erase key \"" << key << "\" from cache \"" << name_ |
| 76 |
<< "\" but it was not found." << std::endl; |
| 77 |
} |
| 78 |
} |
| 79 |
} |
| 80 |
else { |
| 81 |
Log::debug() << "Tried to erase key \"" << remove_key << "\" from cache \"" << name_ |
| 82 |
<< "\" but it was not found." << std::endl; |
| 83 |
} |
| 84 |
} |
| 85 |
|
| 86 |
private: |
| 87 |
std::string name_; |
| 88 |
std::mutex lock_; |
| 89 |
std::map<key_type, ObjectHandle<value_type>> map_; |
| 90 |
std::map<key_type, std::vector<key_type>> remove_; |
| 91 |
}; |
| 92 |
|
| 93 |
} // namespace util |
| 94 |
} // namespace atlas |