userver: components::MongoCache< MongoCacheTraits > Class Template Reference
Loading...
Searching...
No Matches
components::MongoCache< MongoCacheTraits > Class Template Reference

#include <userver/cache/base_mongo_cache.hpp>

Detailed Description

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. 10 seconds
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 {
// Component name for component
static constexpr std::string_view kName = "mongo-dynamic-config";
// Collection to read from
static constexpr auto kMongoCollectionsField =
&storages::mongo::Collections::config;
// Update field name to use for incremental update (optional).
// When missing, incremental update is disabled.
// Please use reference here to avoid global variables
// initialization order issues.
static constexpr const std::string& kMongoUpdateFieldName =
mongo::db::taxi::config::kUpdated;
// Cache element type
using ObjectType = CachedObject;
// Cache element field name that is used as an index in the cache map
static constexpr auto kKeyField = &CachedObject::name;
// Type of kKeyField
using KeyType = std::string;
// Type of cache map, e.g. unordered_map, map, bimap
using DataType = std::unordered_map<KeyType, ObjectType>;
// Whether the cache prefers to read from replica (if true, you might get stale data)
static constexpr bool kIsSecondaryPreferred = true;
// Optional function that overrides BSON to ObjectType conversion
static constexpr auto DeserializeObject = &CachedObject::FromBson;
// or
static ObjectType DeserializeObject(const formats::bson::Document& doc) {
return doc["value"].As<ObjectType>();
}
// (default implementation calls doc.As<ObjectType>())
// For using default implementation
static constexpr bool kUseDefaultDeserializeObject = true;
// Optional function that overrides data retrieval operation
static storages::mongo::operations::Find GetFindOperation(
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;
}
// (default implementation queries kMongoUpdateFieldName: {$gt: last_update}
// for incremental updates, and {} for full updates)
// For using default implementation
static constexpr bool kUseDefaultFindOperation = true;
// Whether update part of the cache even if failed to parse some documents
static constexpr bool kAreInvalidDocumentsSkipped = false;
// Component to get the collections
using MongoCollectionsComponent = components::MongoCollections;
};

Definition at line 121 of file base_mongo_cache.hpp.

+ Inheritance diagram for components::MongoCache< MongoCacheTraits >:

Public Types

using DataType
 

Public Member Functions

 MongoCache (const ComponentConfig &, const ComponentContext &)
 
const std::string & Name () const
 
const std::string & Name () const
 
void InvalidateAsync (UpdateType update_type)
 Non-blocking forced cache update of specified type.
 
void InvalidateAsync (UpdateType update_type)
 Non-blocking forced cache update of specified type.
 
void InvalidateAsync (UpdateType update_type)
 Non-blocking forced cache update of specified type.
 
utils::SharedReadablePtr< MongoCacheTraits::DataType > Get () const final
 
utils::SharedReadablePtr< MongoCacheTraits::DataType > GetUnsafe () const
 
concurrent::AsyncEventSubscriberScope UpdateAndListen (Class *obj, std::string name, void(Class::*func)(const std::shared_ptr< const MongoCacheTraits::DataType > &))
 
concurrent::AsyncEventChannel< const std::shared_ptr< const MongoCacheTraits::DataType > & > & GetEventChannel ()
 
ComponentHealth GetComponentHealth () const override
 
void OnLoadingCancelled () override
 
void OnAllComponentsAreStopping () override
 
void UpdateSyncDebug (UpdateType update_type)
 Forces a cache update of specified type.
 

Static Public Member Functions

static yaml_config::Schema GetStaticConfigSchema ()
 

Static Public Attributes

static constexpr std::string_view kName = MongoCacheTraits::kName
 

Protected Types

using LoggableComponentBase
 
enum  Flag
 Periodic update flags. More...
 

Protected Member Functions

void Set (std::unique_ptr< const MongoCacheTraits::DataType > value_ptr)
 
void Set (MongoCacheTraits::DataType &&value)
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
 
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 cache update of specified type.
 
AllowedUpdateTypes GetAllowedUpdateTypes () const
 Update types configured for the cache.
 
void StartPeriodicUpdates (utils::Flags< Flag > flags={})
 Starts periodic updates.
 
void StartPeriodicUpdates (utils::Flags< Flag > flags={})
 Starts periodic updates.
 
void StopPeriodicUpdates ()
 Stops periodic updates.
 
void AssertPeriodicUpdateStarted ()
 
void AssertPeriodicUpdateStopped ()
 
void OnCacheModified ()
 
virtual void WriteContents (dump::Writer &writer, const MongoCacheTraits::DataType &contents) const
 
virtual std::unique_ptr< const MongoCacheTraits::DataType > ReadContents (dump::Reader &reader) const
 

Member Typedef Documentation

◆ DataType

using components::CachingComponentBase< MongoCacheTraits::DataType >::DataType
inherited

Definition at line 145 of file caching_component_base.hpp.

◆ LoggableComponentBase

Deprecated
use components::ComponentBase instead.

Definition at line 67 of file component_base.hpp.

Member Enumeration Documentation

◆ Flag

enum cache::CacheUpdateTrait::Flag
strongprotectedinherited

Periodic update flags.

Definition at line 61 of file cache_update_trait.hpp.

Constructor & Destructor Documentation

◆ MongoCache()

template<class MongoCacheTraits>
components::MongoCache< MongoCacheTraits >::MongoCache ( const ComponentConfig & config,
const ComponentContext & context )

Definition at line 162 of file base_mongo_cache.hpp.

◆ ~MongoCache()

template<class MongoCacheTraits>
components::MongoCache< MongoCacheTraits >::~MongoCache ( )

Definition at line 194 of file base_mongo_cache.hpp.

Member Function Documentation

◆ Attach()

void components::CachingComponentBase< MongoCacheTraits::DataType >::Attach ( const std::shared_ptr< const MongoCacheTraits::DataType > & value_ptr)
protectedinherited

Attach the value of cache. As a result the Get() member function starts returning the value passed into this function after the Update() finishes. Does not take over into sole ownership. Do not use unless absolutely necessary. The object must be strictly thread-safe.

Warning
Do not forget to update cache::UpdateStatisticsScope, otherwise the behavior is undefined.

Definition at line 186 of file caching_component_base.hpp.

◆ Clear()

void components::CachingComponentBase< MongoCacheTraits::DataType >::Clear ( )
protectedinherited

Clears the content of the cache by string a default constructed T.

Definition at line 193 of file caching_component_base.hpp.

◆ Emplace()

void components::CachingComponentBase< MongoCacheTraits::DataType >::Emplace ( Args &&... args)
protectedinherited

Definition at line 190 of file caching_component_base.hpp.

◆ Get()

utils::SharedReadablePtr< MongoCacheTraits::DataType > components::CachingComponentBase< MongoCacheTraits::DataType >::Get ( ) const
finalvirtualinherited
Returns
cache contents. May be nullptr if and only if MayReturnNull returns true.
Exceptions
cache::EmptyCacheErrorif the contents are nullptr, and MayReturnNull returns false (which is the default behavior).

Implements cache::DataProvider< MongoCacheTraits::DataType >.

Definition at line 151 of file caching_component_base.hpp.

◆ GetComponentHealth()

ComponentHealth components::ComponentBase::GetComponentHealth ( ) const
inlineoverrideinherited

Override this function to inform the world of the state of your component.

Warning
The function is called concurrently from multiple threads.

Definition at line 35 of file component_base.hpp.

◆ GetEventChannel()

concurrent::AsyncEventChannel< const std::shared_ptr< const MongoCacheTraits::DataType > & > & components::CachingComponentBase< MongoCacheTraits::DataType >::GetEventChannel ( )
inherited

Definition at line 165 of file caching_component_base.hpp.

◆ GetStaticConfigSchema()

template<class MongoCacheTraits>
yaml_config::Schema components::MongoCache< MongoCacheTraits >::GetStaticConfigSchema ( )
static

Definition at line 338 of file base_mongo_cache.hpp.

◆ GetUnsafe()

utils::SharedReadablePtr< MongoCacheTraits::DataType > components::CachingComponentBase< MongoCacheTraits::DataType >::GetUnsafe ( ) const
inherited
Returns
cache contents. May be nullptr regardless of MayReturnNull.

Definition at line 154 of file caching_component_base.hpp.

◆ InvalidateAsync() [1/3]

void cache::CacheUpdateTrait::InvalidateAsync ( UpdateType update_type)
inherited

Non-blocking forced cache update of specified type.

See also
PeriodicTask::ForceStepAsync for behavior details

◆ InvalidateAsync() [2/3]

( UpdateType update_type)
inherited

Non-blocking forced cache update of specified type.

See also
PeriodicTask::ForceStepAsync for behavior details

◆ InvalidateAsync() [3/3]

void cache::CacheUpdateTrait::InvalidateAsync ( UpdateType update_type)
inherited

Non-blocking forced cache update of specified type.

See also
PeriodicTask::ForceStepAsync for behavior details

◆ MayReturnNull()

bool components::CachingComponentBase< MongoCacheTraits::DataType >::MayReturnNull ( ) const
protectedvirtualinherited

Whether Get is expected to return nullptr.

Definition at line 196 of file caching_component_base.hpp.

◆ Name() [1/2]

( ) const
inherited
Returns
name of the component

◆ Name() [2/2]

const std::string & cache::CacheUpdateTrait::Name ( ) const
inherited
Returns
name of the component

◆ OnAllComponentsAreStopping()

void components::ComponentBase::OnAllComponentsAreStopping ( )
inlineoverrideinherited

Component may use this function to stop doing work before the stop of the components that depend on it.

Base components may override it and make final to do some work before the derived object constructor is called. Don't use it otherwise.

Definition at line 58 of file component_base.hpp.

◆ OnCacheModified()

void cache::CacheUpdateTrait::OnCacheModified ( )
protectedinherited

Called in CachingComponentBase::Set during update to indicate that the cached data has been modified

◆ OnLoadingCancelled()

void components::ComponentBase::OnLoadingCancelled ( )
inlineoverrideinherited

Called once if the creation of any other component failed. If the current component expects some other component to take any action with the current component, this call is a signal that such action may never happen due to components loading was cancelled. Application components might not want to override it.

Definition at line 42 of file component_base.hpp.

◆ PreAssignCheck()

void components::CachingComponentBase< MongoCacheTraits::DataType >::PreAssignCheck ( const MongoCacheTraits::DataType * old_value_ptr,
const MongoCacheTraits::DataType * new_value_ptr ) const
protectedvirtualinherited

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.

Note
old_value_ptr and new_value_ptr can be nullptr.

Definition at line 208 of file caching_component_base.hpp.

◆ ReadContents()

std::unique_ptr< const MongoCacheTraits::DataType > components::CachingComponentBase< MongoCacheTraits::DataType >::ReadContents ( dump::Reader & reader) const
protectedvirtualinherited

Definition at line 202 of file caching_component_base.hpp.

◆ Set() [1/2]

void components::CachingComponentBase< MongoCacheTraits::DataType >::Set ( MongoCacheTraits::DataType && value)
protectedinherited

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Definition at line 178 of file caching_component_base.hpp.

◆ Set() [2/2]

void components::CachingComponentBase< MongoCacheTraits::DataType >::Set ( std::unique_ptr< const MongoCacheTraits::DataType > value_ptr)
protectedinherited

Sets the new value of cache. As a result the Get() member function starts returning the value passed into this function after the Update() finishes.

Warning
Do not forget to update cache::UpdateStatisticsScope, otherwise the behavior is undefined.

Definition at line 175 of file caching_component_base.hpp.

◆ StopPeriodicUpdates()

void cache::CacheUpdateTrait::StopPeriodicUpdates ( )
protectedinherited

Stops periodic updates.

Warning
Should be called in destructor of derived class.

◆ UpdateAndListen()

concurrent::AsyncEventSubscriberScope components::CachingComponentBase< MongoCacheTraits::DataType >::UpdateAndListen ( Class * obj,
std::string name,
void(Class::* func )(const std::shared_ptr< const MongoCacheTraits::DataType > &) )
inherited

Subscribes to cache updates using a member function. Also immediately invokes the function with the current cache contents.

Definition at line 159 of file caching_component_base.hpp.

◆ UpdateSyncDebug() [1/2]

void cache::CacheUpdateTrait::UpdateSyncDebug ( UpdateType update_type)
inherited

Forces a cache update of specified type.

Exceptions
IfUpdate throws

◆ UpdateSyncDebug() [2/2]

void cache::CacheUpdateTrait::UpdateSyncDebug ( UpdateType update_type)
inherited

Forces a cache update of specified type.

Exceptions
IfUpdate throws

◆ WriteContents()

void components::CachingComponentBase< MongoCacheTraits::DataType >::WriteContents ( dump::Writer & writer,
const MongoCacheTraits::DataType & contents ) const
protectedvirtualinherited

Override to use custom serialization for cache dumps

Definition at line 200 of file caching_component_base.hpp.

Member Data Documentation

◆ kName

template<class MongoCacheTraits>
std::string_view components::MongoCache< MongoCacheTraits >::kName = MongoCacheTraits::kName
staticconstexpr

Definition at line 125 of file base_mongo_cache.hpp.


The documentation for this class was generated from the following file: