template<class MongoCacheTraits>
class components::MongoCache< MongoCacheTraits >
Base class for all caches polling mongo collection
You have to provide a traits class in order to use this.
For avoiding "memory leaks", see the respective section in components::CachingComponentBase.
Static options of components::MongoCache :
| Name | Description | Default value |
| update-correction | Adjusts incremental updates window to overlap with previous update. | 0 |
Options inherited from components::CachingComponentBase :
| Name | Description | Default value |
| update-types | Specifies whether incremental and/or full updates are used. Possible values: full-and-incremental, only-full, only-incremental. | – |
| update-interval | (required) interval between Update invocations. | – |
| update-jitter | Max. amount of time by which update-interval may be adjusted for requests dispersal. | update_interval / 10 |
| updates-enabled | If false, cache updates are disabled (except for the first one if !first-update-fail-ok). | true |
| full-update-interval | Interval between full updates. | – |
| full-update-jitter | Max. amount of time by which full-update-interval may be adjusted for requests dispersal. | full-update-interval / 10 |
| exception-interval | Sleep interval after an unhandled exception. | update_interval |
| first-update-fail-ok | Whether first update failure is non-fatal. | false |
| task-processor | The name of the TaskProcessor for running DoWork. | main-task-processor |
| config-settings | Enables dynamic reconfiguration with CacheConfigSet. | true |
| additional-cleanup-interval | How often to run background RCU garbage collector, in milliseconds. | 10000 |
| is-strong-period | Whether to include Update execution time in update-interval. | false |
| has-pre-assign-check | Enables the check before changing the value in the cache, by default it is the check that the new value is not empty. | false |
| testsuite-force-periodic-update | Override testsuite-periodic-update-enabled in TestsuiteSupport component config. | – |
| failed-updates-before-expiration | The number of consecutive failed updates for data expiration. | – |
| alert-on-failing-to-update-times | Fire an alert if the cache update failed specified amount of times in a row. If zero - alerts are disabled. Value from dynamic config takes priority over static. | 0 |
| safe-data-lifetime | Enables awaiting data destructors in the component's destructor. Can be set to false if the stored data does not refer to the component and its dependencies. | true |
| dump | Manages cache behavior after dump load. | – |
| dump.first-update-mode | Behavior of update after successful load from dump. skip - after successful load from dump, do nothing; required - make a synchronous update of type first-update-type, stop the service on failure; best-effort - make a synchronous update of type first-update-type, keep working and use data from dump on failure. Possible values: skip, required, best-effort. | skip |
| dump.first-update-type | Update type after successful load from dump. Possible values: full, incremental, incremental-then-async-full. | full |
Options inherited from components::ComponentBase :
| Name | Description | Default value |
| load-enabled | Set to false to disable loading of the component. | true |
Traits example:
All fields below (except for function overrides) are mandatory.
struct MongoCacheTraitsExample {
static constexpr std::string_view kName = "mongo-dynamic-config";
static constexpr auto kMongoCollectionsField =
&storages::mongo::Collections::config;
static constexpr const std::string& kMongoUpdateFieldName =
mongo::db::taxi::config::kUpdated;
using ObjectType = CachedObject;
static constexpr auto kKeyField = &CachedObject::name;
using KeyType = std::string;
using DataType = std::unordered_map<KeyType, ObjectType>;
static constexpr bool kIsSecondaryPreferred = true;
static constexpr auto DeserializeObject = &CachedObject::FromBson;
return doc[
"value"].
As<ObjectType>();
}
static constexpr bool kUseDefaultDeserializeObject = true;
const std::chrono::system_clock::time_point& last_update,
const std::chrono::system_clock::time_point& now,
const std::chrono::system_clock::duration& correction) {
mongo::operations::Find find_op({});
find_op.SetOption(mongo::options::Projection{"key", "value"});
return find_op;
}
static constexpr bool kUseDefaultFindOperation = true;
static constexpr bool kAreInvalidDocumentsSkipped = false;
using MongoCollectionsComponent = components::MongoCollections;
};
Building the query at runtime:
A query defined in the traits is fixed at compile time. If the query depends on anything that is only known at runtime (the dynamic config, other components, the static config, the state of the cache itself and so on), then specify no find operation in the traits and override components::MongoCache::MakeFindOperation in a derived component instead.
Traits with neither GetFindOperation nor kUseDefaultFindOperation make MakeFindOperation pure virtual, so you must override it in a derived component.
struct RuntimeQueryCacheTraits {
static constexpr std::string_view kName = "runtime-query-mongo-cache";
static constexpr auto kMongoCollectionsField = &MongoCollections::Collections::cached_documents;
using MongoCollectionsComponent = MongoCollections;
using ObjectType = CachedObject;
static constexpr auto kKeyField = &CachedObject::key;
using DataType = std::unordered_map<KeyType, ObjectType>;
static constexpr bool kIsSecondaryPreferred = false;
static constexpr bool kAreInvalidDocumentsSkipped = false;
static ObjectType DeserializeObject(const formats::bson::Document& doc) {
return CachedObject{doc[
"key"].
As<std::string>(), doc[
"value"].As<int>()};
}
};
class RuntimeQueryCache final : public components::MongoCache<RuntimeQueryCacheTraits> {
public:
RuntimeQueryCache(const components::ComponentConfig& config, const components::ComponentContext& context)
: MongoCache(config, context),
config_source_(context.FindComponent<components::DynamicConfig>().GetSource())
{}
std::size_t GetMakeFindOperationCount() const { return make_find_operation_count_.load(); }
protected:
storages::mongo::operations::Find MakeFindOperation(
const std::chrono::system_clock::time_point& ,
const std::chrono::system_clock::time_point& ,
const std::chrono::system_clock::duration&
) override {
++make_find_operation_count_;
last_update_type_ = type;
const auto config = config_source_.GetSnapshot();
}
private:
const dynamic_config::Source config_source_;
std::atomic<std::size_t> make_find_operation_count_{0};
};
Definition at line 191 of file base_mongo_cache.hpp.
|
| virtual storages::mongo::operations::Find | MakeFindOperation (cache::UpdateType type, const std::chrono::system_clock::time_point &last_update, const std::chrono::system_clock::time_point &now, const std::chrono::system_clock::duration &correction) |
| void | Set (std::unique_ptr< const MongoCacheTraits::DataType > value_ptr) |
| | Sets the new value of cache. As a result, Get member function starts returning the new value.
|
| void | Attach (const std::shared_ptr< const MongoCacheTraits::DataType > &value_ptr) |
| void | Emplace (Args &&... args) |
| void | Clear () |
| | Clears the content of the cache by string a default constructed T.
|
| virtual bool | MayReturnNull () const |
| | Whether Get is expected to return nullptr.
|
| virtual void | PreAssignCheck (const MongoCacheTraits::DataType *old_value_ptr, const MongoCacheTraits::DataType *new_value_ptr) const |
| | If the option has-pre-assign-check is set true in static config, this function is called before assigning the new value to the cache.
|
| void | UpdateSyncDebug (UpdateType update_type) |
| | Forces a synchronous cache update of specified type.
|
|
AllowedUpdateTypes | GetAllowedUpdateTypes () const |
| | Update types configured for the cache.
|
| void | OnCacheModified () noexcept |
|
virtual utils::Flags< Flag > | GetStartFlags () const |
| | Returns flags for cache start.
|
|
void | EarlyStartPeriodicUpdates (utils::Flags< Flag > flags) |
| | Call this to start periodic updates just now, not after the constructor.
|
|
void | EarlyStartPeriodicUpdates (utils::Flags< Flag > flags) |
| | Call this to start periodic updates just now, not after the constructor.
|
| virtual void | WriteContents (dump::Writer &writer, const MongoCacheTraits::DataType &contents) const |
| virtual std::unique_ptr< const MongoCacheTraits::DataType > | ReadContents (dump::Reader &reader) const |