A typical components::PostgreCache usage consists of trait definition:
and registration of the component in components::ComponentList:
See Basics of Caches for introduction into caches.
components::PostgreCache static configuration file should have a PostgreSQL component name specified in pgcomponent configuration parameter.
Optionally the operation timeouts for cache loading can be specified.
For avoiding "memory leaks", see the respective section in components::CachingComponentBase.
| Name | Description | Default value |
|---|---|---|
| full-update-op-timeout | Timeout for a full update. | 1m |
| incremental-update-op-timeout | Timeout for an incremental update. | 1s |
| update-correction | Incremental update window adjustment. | 0 for caches with defined GetLastKnownUpdated |
| chunk-size | Number of rows to request from PostgreSQL, 0 to fetch all rows in one request. | 1000 |
| sleep-between-chunks | Duration to wait between reading chunks from PostgreSQL. | 0ms |
| pgcomponent | PostgreSQL component name. | – |
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 |
Cache policy is the template argument of components::PostgreCache component. Please see the following code snippet for documentation.
The query can be a std::string. But due to non-guaranteed order of static data members initialization, std::string should be returned from a static member function, please see the following code snippet.
Policy may have static function GetLastKnownUpdated. It should be used when new entries from database are taken via revision, identifier, or anything else, but not timestamp of the last update. If this function is supplied, new entries are taken from db with condition 'WHERE kUpdatedField > GetLastKnownUpdated(cache_container)'. Otherwise, condition is 'WHERE kUpdatedField > last_update - correction_'. See the following code snippet for an example of usage
Cache can also store only subset of data. For example for the database that is is defined in the following way:
it is possible to create a cache that stores only the latest value:
In case one provides a custom CacheContainer within Policy, it is notified of Update completion via its public member function OnWritesDone, if any. Custom CacheContainer must provide size method and insert_or_assign method similar to std::unordered_map's one or CacheInsertOrAssign function similar to one defined in namespace utils::impl::projected_set (i.e. used for utils::ProjectedUnorderedSet). See the following code snippet for an example of usage:
To forward declare a cache you can forward declare a trait and include userver/cache/base_postgres_cache_fwd.hpp header. It is also useful to forward declare the cache value type.