Cache account profiles in the profile service
C++ · C++ · advanced · modification
Every profile page and every order confirmation reads the account's profile, and the profile database is a slow hop of those requests. This PR puts a small per-worker cache in front of the store. `src/account/profile_cache.hpp` and `src/account/profile_cache.cpp` (new) add `ProfileCache`, an `std::unordered_map` of entries that each hold the shared profile and its expiry time, with `find`, `put`, `erase`, the capacity rule and argument checks (a TTL from 1 second to 24 hours, a capacity of at least 1, a non-null profile, a clock that is not a temporary). `src/account/profile_service.hpp` gives `ProfileService` a `ProfileCache` member (60 seconds, 100,000 entries) and an optional `Clock` parameter that defaults to `real_clock()`, and `src/account/profile_service.cpp` answers `get` from the cache, fills it on a miss and drops the entry after a successful `update`. `tests/profile_cache_test.cpp` (new) drives `ProfileService` with a fake store and clock (repeat reads inside and past the TTL, two accounts read in turn, an update, an unknown account) and checks `ProfileCache`'s capacity rule and argument checks.
C++20, standard library only, built with CMake (unchanged: it builds `src/**/*.cpp` into the service and every `tests/*_test.cpp` together with `tests/check_main.cpp` into the test runner) by Apple clang 17 and GCC 13 in CI, both with `-Wall -Wextra -Wconversion -Werror`. Targets are 64-bit Linux and macOS (`unsigned int` and `std::uint32_t` are 32 bits). Account ids: `account::AccountId` (`src/account/account_id.hpp`, unchanged) is a `std::uint64_t` whose high 32 bits are the account's shard number and whose low 32 bits are its sequence number within that shard; `make_account_id(shard, sequence)` builds one and `shard_of(id)` reads the shard. There are 64 shards (numbers 0 to 63, more are added as they fill), every shard numbers its accounts from 1 upward, and each shard holds from about 2 to 9 million accounts, so every sequence number up to about 2 million exists on every shard and higher ones on many. Threading: the service runs one worker thread per core, and each worker constructs and owns its own `ProfileStore` connection and `ProfileService`; nothing in `src/account/` is shared between threads. Existing and unchanged: `Profile` (`src/account/profile.hpp`) holds `id` (the account it belongs to), `display_name`, `email` and `postal_address`. `ProfileStore` (`src/account/profile_store.hpp`) is an interface: `load(id)` returns a `std::shared_ptr<const Profile>` of that account (`nullptr` if it does not exist), `save(profile)` writes the profile of `profile.id` and returns `false` on a failed write. `Clock` (`src/account/clock.hpp`) has `time_point now() const` on `std::chrono::steady_clock`, and `real_clock()` returns a process-wide steady clock; its readings count from boot, so they stay far below `time_point::max()` (the test clocks start at zero). The HTTP handlers (unchanged) check that the signed-in user owns account `id` (or is support staff) before calling `ProfileService::get(id)`, then render the returned profile's display name, email and postal address into the page. `tests/check.hpp` is the repo's tiny test helper: `TEST(name) { ... }` registers a test, `CHECK(expr)` and `CHECK_EQ(a, b)` record a failure with file and line, and the `main` in `tests/check_main.cpp` runs every registered test and exits non-zero if any check failed.
Requirements
- New: `account::ProfileCache` (`src/account/profile_cache.hpp`) holds loaded profiles for `ttl` after they are put. `find(id)` returns the profile cached for account `id`, or `nullptr` when that account has no entry or its entry is `ttl` old or older (an expired entry is dropped). `put(id, profile)` caches `profile` for `id`, replacing an earlier entry for the same account. `erase(id)` drops the account's entry, if any. When the cache already holds `capacity` entries and `id` has none, `put` drops every entry before adding the new one, so the cache never holds more than `capacity` entries. `capacity` is at least 1 and `ttl` is from 1 second to `ProfileCache::max_ttl` (24 hours); the constructor throws `std::invalid_argument` otherwise. `profile` must not be null; `put` throws `std::invalid_argument` on a null one and leaves the cache unchanged. The cache keeps a reference to the `Clock` it is given, so that clock must outlive the cache; constructing it from a temporary clock does not compile.
- `ProfileService::get(id)` (existing) must keep returning the profile of account `id`, or `nullptr` when the account does not exist. It now answers from its cache (a TTL of 60 seconds, a capacity of 100,000 entries) and loads from the store only on a miss, caching what it loaded. A missing account is not cached.
- `ProfileService::update(profile)` (existing) saves through the store as before; when the save succeeds, it drops the cached entry of `profile.id` from this service's cache, so the next `get` on the same service reads the saved profile. Other workers' caches may keep serving the previous profile for up to the TTL; that staleness is accepted. It returns what the save returned.
- The `ProfileService` constructor keeps working with only a store (the existing call site in `src/main.cpp` stays unchanged); tests may pass their own `Clock`, which must outlive the service (a temporary clock does not compile).
- Ship unit tests for the cache in `ProfileService`, using the repo's `tests/check.hpp` helper.
Files touched
- src/account/profile_cache.hpp
- src/account/profile_cache.cpp
- src/account/profile_service.hpp
- src/account/profile_service.cpp
- tests/profile_cache_test.cpp