userver: components::MongoCache< MongoCacheTraits > Class Template Reference
⚠️ This is the documentation for an old userver version. Click here to switch to the latest version.
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages Concepts
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.

Avoiding memory leaks

See components::CachingComponentBase

Static options:

All options of CachingComponentBase and

Name Description Default value
update-correction adjusts incremental updates window to overlap with previous update 0

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 122 of file base_mongo_cache.hpp.

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

Public Member Functions

 MongoCache (const ComponentConfig &, const ComponentContext &)
 
- Public Member Functions inherited from components::CachingComponentBase< MongoCacheTraits::DataType >
 CachingComponentBase (const ComponentConfig &config, const ComponentContext &)
 
utils::SharedReadablePtr< MongoCacheTraits::DataType > Get () const
 
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 ()
 
- Public Member Functions inherited from components::LoggableComponentBase
 LoggableComponentBase (const ComponentConfig &, const ComponentContext &)
 
 LoggableComponentBase (LoggableComponentBase &&)=delete
 
 LoggableComponentBase (const LoggableComponentBase &)=delete
 
 ~LoggableComponentBase () override=default
 
ComponentHealth GetComponentHealth () const override
 
void OnLoadingCancelled () override
 
void OnAllComponentsLoaded () override
 
void OnAllComponentsAreStopping () override
 

Static Public Member Functions

static yaml_config::Schema GetStaticConfigSchema ()
 
- Static Public Member Functions inherited from components::CachingComponentBase< MongoCacheTraits::DataType >
static yaml_config::Schema GetStaticConfigSchema ()
 
- Static Public Member Functions inherited from components::LoggableComponentBase
static yaml_config::Schema GetStaticConfigSchema ()
 

Static Public Attributes

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

Additional Inherited Members

- Public Types inherited from components::CachingComponentBase< MongoCacheTraits::DataType >
using DataType
 
- Protected Types inherited from cache::CacheUpdateTrait
enum class  Flag {
  kNone = 0 ,
  kNoFirstUpdate = 1 << 0
}
 Periodic update flags. More...
 
- Protected Member Functions inherited from components::CachingComponentBase< MongoCacheTraits::DataType >
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 Emplace (Args &&... args)
 
void Clear ()
 Clears the content of the cache by string a default constructed T.
 
virtual bool MayReturnNull () const
 
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.
 
virtual void WriteContents (dump::Writer &writer, const MongoCacheTraits::DataType &contents) const
 
virtual std::unique_ptr< const MongoCacheTraits::DataType > ReadContents (dump::Reader &reader) const
 
- Protected Member Functions inherited from cache::CacheUpdateTrait
AllowedUpdateTypes GetAllowedUpdateTypes () const
 Update types configured for the cache.
 
void StartPeriodicUpdates (utils::Flags< Flag > flags={})
 Starts periodic updates.
 
void StopPeriodicUpdates ()
 Stops periodic updates.
 
void AssertPeriodicUpdateStarted ()
 
void OnCacheModified ()
 
 CacheUpdateTrait (CacheUpdateTrait &&)=delete
 
CacheUpdateTraitoperator= (CacheUpdateTrait &&)=delete
 
void InvalidateAsync (UpdateType update_type)
 Non-blocking forced cache update of specified type.
 
void UpdateSyncDebug (UpdateType update_type)
 Forces a cache update of specified type.
 
const std::string & Name () const
 

Constructor & Destructor Documentation

◆ MongoCache()

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

Definition at line 164 of file base_mongo_cache.hpp.

◆ ~MongoCache()

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

Definition at line 200 of file base_mongo_cache.hpp.

Member Function Documentation

◆ GetStaticConfigSchema()

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

Definition at line 346 of file base_mongo_cache.hpp.

Member Data Documentation

◆ kName

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

Definition at line 128 of file base_mongo_cache.hpp.


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