userver: components::PostgreCache< PostgreCachePolicy > Class Template Reference
Loading...
Searching...
No Matches
components::PostgreCache< PostgreCachePolicy > Class Template Referencefinal

#include <userver/cache/base_postgres_cache.hpp>

Detailed Description

template<typename PostgreCachePolicy>
class components::PostgreCache< PostgreCachePolicy >

Caching component for PostgreSQL. See Caching Component for PostgreSQL.

See also
Caching Component for PostgreSQL, Basics of Caches
Examples
samples/postgres_auth/user_info_cache.hpp.

Definition at line 348 of file base_postgres_cache.hpp.

Inheritance diagram for components::PostgreCache< PostgreCachePolicy >:

Public Types

using PolicyType = PostgreCachePolicy
using ValueType = pg_cache::detail::ValueType<PolicyType>
using RawValueType = pg_cache::detail::RawValueType<PolicyType>
using DataType = pg_cache::detail::DataCacheContainerType<PolicyType>
using PolicyCheckerType = pg_cache::detail::PolicyChecker<PostgreCachePolicy>
using UpdatedFieldType = pg_cache::detail::UpdatedFieldType<PostgreCachePolicy>
using BaseType = typename PolicyCheckerType::BaseType

Public Member Functions

 PostgreCache (const ComponentConfig &, const ComponentContext &)
utils::SharedReadablePtr< DataCacheContainerType< PostgreCachePolicy > > Get () const final
utils::SharedReadablePtr< DataCacheContainerType< PostgreCachePolicy > > GetUnsafe () const
concurrent::AsyncEventSubscriberScope UpdateAndListen (Class *obj, std::string name, void(Class::*func)(const std::shared_ptr< const DataCacheContainerType< PostgreCachePolicy > > &))
concurrent::AsyncEventChannel< const std::shared_ptr< const DataCacheContainerType< PostgreCachePolicy > > & > & GetEventChannel ()
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.
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 ()

Static Public Attributes

static constexpr bool kIncrementalUpdates = pg_cache::detail::WantIncrementalUpdates<PolicyType>
static constexpr auto kClusterHostTypeFlags = pg_cache::detail::ClusterHostType<PolicyType>()
static constexpr auto kName = PolicyType::kName

Protected Types

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

Protected Member Functions

void Set (std::unique_ptr< const DataCacheContainerType< PostgreCachePolicy > > value_ptr)
 Sets the new value of cache. As a result, Get member function starts returning the new value.
void Set (DataCacheContainerType< PostgreCachePolicy > &&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 DataCacheContainerType< PostgreCachePolicy > > &value_ptr)
void Emplace (Args &&... args)
void Clear ()
 Clears the content of the cache by string a default constructed T.
virtual void PreAssignCheck (const DataCacheContainerType< PostgreCachePolicy > *old_value_ptr, const DataCacheContainerType< PostgreCachePolicy > *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< 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 DataCacheContainerType< PostgreCachePolicy > &contents) const
virtual std::unique_ptr< const DataCacheContainerType< PostgreCachePolicy > > ReadContents (dump::Reader &reader) const

Member Typedef Documentation

◆ BaseType

template<typename PostgreCachePolicy>
using components::PostgreCache< PostgreCachePolicy >::BaseType = typename PolicyCheckerType::BaseType

Definition at line 357 of file base_postgres_cache.hpp.

◆ DataType

template<typename PostgreCachePolicy>
using components::PostgreCache< PostgreCachePolicy >::DataType = pg_cache::detail::DataCacheContainerType<PolicyType>

Definition at line 354 of file base_postgres_cache.hpp.

◆ LoggableComponentBase

Deprecated
use components::ComponentBase instead.

Definition at line 79 of file component_base.hpp.

◆ PolicyCheckerType

template<typename PostgreCachePolicy>
using components::PostgreCache< PostgreCachePolicy >::PolicyCheckerType = pg_cache::detail::PolicyChecker<PostgreCachePolicy>

Definition at line 355 of file base_postgres_cache.hpp.

◆ PolicyType

template<typename PostgreCachePolicy>
using components::PostgreCache< PostgreCachePolicy >::PolicyType = PostgreCachePolicy

Definition at line 351 of file base_postgres_cache.hpp.

◆ RawValueType

template<typename PostgreCachePolicy>
using components::PostgreCache< PostgreCachePolicy >::RawValueType = pg_cache::detail::RawValueType<PolicyType>

Definition at line 353 of file base_postgres_cache.hpp.

◆ UpdatedFieldType

template<typename PostgreCachePolicy>
using components::PostgreCache< PostgreCachePolicy >::UpdatedFieldType = pg_cache::detail::UpdatedFieldType<PostgreCachePolicy>

Definition at line 356 of file base_postgres_cache.hpp.

◆ ValueType

template<typename PostgreCachePolicy>
using components::PostgreCache< PostgreCachePolicy >::ValueType = pg_cache::detail::ValueType<PolicyType>

Definition at line 352 of file base_postgres_cache.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

◆ PostgreCache()

template<typename PostgreCachePolicy>
components::PostgreCache< PostgreCachePolicy >::PostgreCache ( const ComponentConfig & config,
const ComponentContext & context )

Definition at line 413 of file base_postgres_cache.hpp.

Member Function Documentation

◆ Attach()

void components::CachingComponentBase< DataCacheContainerType< PostgreCachePolicy > >::Attach ( const std::shared_ptr< const DataCacheContainerType< PostgreCachePolicy > > & 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.

◆ Get()

utils::SharedReadablePtr< DataCacheContainerType< PostgreCachePolicy > > components::CachingComponentBase< DataCacheContainerType< PostgreCachePolicy > >::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< T >.

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

◆ GetStaticConfigSchema()

template<typename PostgreCachePolicy>
yaml_config::Schema components::PostgreCache< PostgreCachePolicy >::GetStaticConfigSchema ( )
static

Definition at line 695 of file base_postgres_cache.hpp.

◆ GetUnsafe()

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

◆ 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

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

virtual void components::CachingComponentBase< DataCacheContainerType< PostgreCachePolicy > >::PreAssignCheck ( const DataCacheContainerType< PostgreCachePolicy > * old_value_ptr,
const DataCacheContainerType< PostgreCachePolicy > * 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.

◆ Set()

void components::CachingComponentBase< DataCacheContainerType< PostgreCachePolicy > >::Set ( std::unique_ptr< const DataCacheContainerType< PostgreCachePolicy > > 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.

◆ UpdateAndListen()

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

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

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

virtual void components::CachingComponentBase< DataCacheContainerType< PostgreCachePolicy > >::WriteContents ( dump::Writer & writer,
const DataCacheContainerType< PostgreCachePolicy > & contents ) const
protectedvirtualinherited

Override to use custom serialization for cache dumps

Member Data Documentation

◆ kClusterHostTypeFlags

template<typename PostgreCachePolicy>
auto components::PostgreCache< PostgreCachePolicy >::kClusterHostTypeFlags = pg_cache::detail::ClusterHostType<PolicyType>()
staticconstexpr

Definition at line 361 of file base_postgres_cache.hpp.

◆ kIncrementalUpdates

template<typename PostgreCachePolicy>
bool components::PostgreCache< PostgreCachePolicy >::kIncrementalUpdates = pg_cache::detail::WantIncrementalUpdates<PolicyType>
staticconstexpr

Definition at line 360 of file base_postgres_cache.hpp.

◆ kName

template<typename PostgreCachePolicy>
auto components::PostgreCache< PostgreCachePolicy >::kName = PolicyType::kName
staticconstexpr

Definition at line 362 of file base_postgres_cache.hpp.


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