userver: cache::CacheUpdateTrait Class Reference
Loading...
Searching...
No Matches
cache::CacheUpdateTrait Class Referenceabstract

#include <userver/cache/cache_update_trait.hpp>

Detailed Description

Base class for periodically updated caches.

Note
Don't use directly, inherit from components::CachingComponentBase instead

Definition at line 30 of file cache_update_trait.hpp.

+ Inheritance diagram for cache::CacheUpdateTrait:

Public Member Functions

 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
 

Protected Types

enum class  Flag {
  kNone = 0 ,
  kNoFirstUpdate = 1 << 0
}
 Periodic update flags. More...
 

Protected Member Functions

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

Member Enumeration Documentation

◆ Flag

enum class cache::CacheUpdateTrait::Flag
strongprotected

Periodic update flags.

Enumerator
kNoFirstUpdate 

Disable initial update on start.

Definition at line 62 of file cache_update_trait.hpp.

Member Function Documentation

◆ InvalidateAsync()

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

Non-blocking forced cache update of specified type.

See also
PeriodicTask::ForceStepAsync for behavior details

◆ Name()

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

◆ OnCacheModified()

void cache::CacheUpdateTrait::OnCacheModified ( )
protected

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

◆ StopPeriodicUpdates()

void cache::CacheUpdateTrait::StopPeriodicUpdates ( )
protected

Stops periodic updates.

Warning
Should be called in destructor of derived class.

◆ 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 virtual

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
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
Exceptions
std::exceptionon update failure
See also
Basics of Caches

◆ UpdateSyncDebug()

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

Forces a cache update of specified type.

Exceptions
IfUpdate throws

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