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

#include <userver/cache/base_mongo_cache.hpp>

Detailed Description

template<class MongoCacheTraits>
class components::MongoCacheFindOperationBase< MongoCacheTraits >

Intermediate base of components::MongoCache that declares components::MongoCache::MakeFindOperation.

The traits define no query, so it can only be built from the runtime state of a specific cache. The method is pure virtual, which makes the compiler require an implementation in a derived component.

Definition at line 57 of file base_mongo_cache.hpp.

Inheritance diagram for components::MongoCacheFindOperationBase< MongoCacheTraits >:

Public Types

using DataType

Public Member Functions

const std::string & Name () const noexcept
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 OnAllComponentsLoaded () override
void OnGracefulShutdown (engine::Deadline serving_shutdown_deadline) override
void OnAllComponentsAreStopping () override
void UpdateSyncDebug (UpdateType update_type)
 Forces a synchronous cache update of specified type.

Static Public Member Functions

static yaml_config::Schema GetStaticConfigSchema ()

Protected Types

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

Protected Member Functions

 MongoCacheFindOperationBase (const ComponentConfig &config, const ComponentContext &context)
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)=0
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 void Update (UpdateType type, const std::chrono::system_clock::time_point &last_update, const std::chrono::system_clock::time_point &now, UpdateStatisticsScope &stats_scope)=0
 Should be overridden in a derived class to align the stored data with some data source.
virtual utils::Flags< FlagGetStartFlags () 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

Member Typedef Documentation

◆ DataType

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

Definition at line 144 of file caching_component_base.hpp.

◆ LoggableComponentBase

Deprecated
use components::ComponentBase instead.

Definition at line 79 of file component_base.hpp.

Member Enumeration Documentation

◆ Flag

enum cache::CacheUpdateTrait::Flag
strongprotectedinherited

Periodic update flags.

Definition at line 89 of file cache_update_trait.hpp.

Constructor & Destructor Documentation

◆ MongoCacheFindOperationBase()

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

Definition at line 59 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 150 of file caching_component_base.hpp.

◆ GetComponentHealth()

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 164 of file caching_component_base.hpp.

◆ GetStaticConfigSchema()

yaml_config::Schema components::CachingComponentBase< MongoCacheTraits::DataType >::GetStaticConfigSchema ( )
staticinherited

Definition at line 166 of file caching_component_base.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 153 of file caching_component_base.hpp.

◆ InvalidateAsync() [1/2]

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/2]

( 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()

( ) const
inherited
Returns
name of the component

◆ OnAllComponentsAreStopping()

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 70 of file component_base.hpp.

◆ OnAllComponentsLoaded()

void components::ComponentBase::OnAllComponentsLoaded ( )
inlineoverrideinherited

Component may use this function to finalize registration of other components that depend on it (for example, handler components register in server component, and the latter uses OnAllComponentsLoaded() to start processing requests).

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

Definition at line 51 of file component_base.hpp.

◆ OnCacheModified()

void cache::CacheUpdateTrait::OnCacheModified ( )
protectednoexceptinherited

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

◆ OnGracefulShutdown()

void components::ComponentBase::OnGracefulShutdown ( engine::Deadline serving_shutdown_deadline)
overrideinherited

Serving components like HTTP and gRPC servers may use this function to stop accepting new requests and shutdown serving in the given time interval. Application components likely do not need to override it.

Parameters
[in]serving_shutdown_deadlineThe deadline until already running requests should be allowed to complete. The component is supposed to stop accepting new requests and continue processing of already active requests until this deadline (unless those requests finish earlier). And it might completely shutdown requests processing when the deadline is reached (or when no active requests left).

◆ 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()

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

Sets the new value of cache. As a result, Get member function starts returning the new value.

Notifies subscribers after setting the new value, see UpdateAndListen. Should only be called from Update normally.

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

Definition at line 175 of file caching_component_base.hpp.

◆ Update()

virtual void cache::CacheUpdateTrait::Update ( UpdateType type,
const std::chrono::system_clock::time_point & last_update,
const std::chrono::system_clock::time_point & now,
UpdateStatisticsScope & stats_scope )
protectedpure virtualinherited

Should be overridden in a derived class to align the stored data with some data source.

Update implementation should do one of the following:

A. If the update succeeded and has changes...

  1. call CachingComponentBase::Set to update the stored value and send a notification to subscribers
  2. call UpdateStatisticsScope::Finish
  3. return normally (an exception is allowed in edge cases)

B. If the update succeeded and verified that there are no changes...

  1. DON'T call CachingComponentBase::Set
  2. call UpdateStatisticsScope::FinishNoChanges
  3. return normally (an exception is allowed in edge cases)

C. If the update failed...

  1. DON'T call CachingComponentBase::Set
  2. call UpdateStatisticsScope::FinishWithError, or...
  3. throw an exception, which will be logged nicely (if there already is an exception, prefer rethrowing it instead of calling UpdateStatisticsScope::FinishWithError)
Parameters
typetype of the update
last_updatetime of the last update (value of now from previous invocation of Update or default constructed value if this is the first Update).
nowcurrent time point
stats_scopethe scope that expects one of Finish, FinishNoChanges, FinishWithError or an exception.
Exceptions
std::exceptionon update failure
Warning
If Update returns without throwing an exception and without calling one of the Finish* methods, the behavior is undefined.
See also
Basics of Caches

◆ 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 158 of file caching_component_base.hpp.

◆ UpdateSyncDebug() [1/2]

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

Forces a synchronous cache update of specified type.

Warning
This method is intended for tests and debugging only. In production code use InvalidateAsync instead. The reasons are:
  1. InvalidateAsync shifts the time of the next update as if a periodic update has just happened. UpdateSyncDebug does not do that: it locks the mutex and performs an additional update on the side, which puts extra load on the CPU and on the data source.
  2. The cache we subscribe to sends events as part of its own update. If the callback does something lengthy, e.g. updating another cache, this affects the cache we subscribe to: it may disrupt its updates and cause a traffic jam in the whole chain of caches. InvalidateAsync is not subject to this. On top of that, UpdateSyncDebug will block the cache we subscribe to for the time we wait for the previous update of the dependent cache to finish (if one was in progress).
  3. InvalidateAsync robustly handles situations with repeated update requests, see the docs of PeriodicTask::ForceStepAsync, on top of which it is implemented. UpdateSyncDebug tries to lock the mutex over and over and performs as many updates as were requested, even if they were requested many times in a row.
  4. If UpdateSyncDebug is called before the periodic updates of the current cache have started (they start after the constructor of the derived cache finishes), the behavior is undefined, because the state of the periodic updates is not set up yet, while an update was already requested. InvalidateAsync handles this case: in that situation nothing happens.
See also
InvalidateAsync
Exceptions
IfUpdate throws

◆ UpdateSyncDebug() [2/2]

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

Forces a synchronous cache update of specified type.

Warning
This method is intended for tests and debugging only. In production code use InvalidateAsync instead. The reasons are:
  1. InvalidateAsync shifts the time of the next update as if a periodic update has just happened. UpdateSyncDebug does not do that: it locks the mutex and performs an additional update on the side, which puts extra load on the CPU and on the data source.
  2. The cache we subscribe to sends events as part of its own update. If the callback does something lengthy, e.g. updating another cache, this affects the cache we subscribe to: it may disrupt its updates and cause a traffic jam in the whole chain of caches. InvalidateAsync is not subject to this. On top of that, UpdateSyncDebug will block the cache we subscribe to for the time we wait for the previous update of the dependent cache to finish (if one was in progress).
  3. InvalidateAsync robustly handles situations with repeated update requests, see the docs of PeriodicTask::ForceStepAsync, on top of which it is implemented. UpdateSyncDebug tries to lock the mutex over and over and performs as many updates as were requested, even if they were requested many times in a row.
  4. If UpdateSyncDebug is called before the periodic updates of the current cache have started (they start after the constructor of the derived cache finishes), the behavior is undefined, because the state of the periodic updates is not set up yet, while an update was already requested. InvalidateAsync handles this case: in that situation nothing happens.
See also
InvalidateAsync
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.


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