avatar_cache
The cache lives under {storage.cache_dir}/avatars/ and keys each entry by
an already-hashed Gravatar identifier (typically the lowercase trimmed email
passed through MD5 or SHA-256). Entries are reused for
DEFAULT_TTL before they are refetched.
The HTTP route that surfaces cached bytes to browsers lives in
[crate::web::handlers::avatar] and consumes this cache via
AvatarCache::get_or_fetch.
const DEFAULT_TTL
pub const DEFAULT_TTL: Duration = Duration::from_secs(60 * 60 * 24 * 7);How long a cached avatar remains valid before it is refetched.
Seven days matches typical Gravatar cache headers and keeps the cache small while ensuring profile picture changes propagate in a reasonable window.
const AVATAR_SUBDIR
const AVATAR_SUBDIR: &str = "avatars";Subdirectory (under storage.cache_dir) where avatar bytes are written.
const GRAVATAR_QUERY
const GRAVATAR_QUERY: &str = "?d=identicon&s=160";Default query appended to GRAVATAR_URL when fetching a missing entry.
d=identicon ensures we always receive a valid image even for unknown
emails; s=160 keeps payloads small enough to be cheap to cache.
const GRAVATAR_URL
const GRAVATAR_URL: &str = "https://www.gravatar.com/avatar/";Upstream template for fetching Gravatar bytes by identifier hash.
struct AvatarCache
pub struct AvatarCacheFilesystem-backed Gravatar avatar cache.
Each AvatarCache is scoped to a single storage.cache_dir root. Bytes
are written to {root}/avatars/{hash} so multiple cache flavours can share
the same user-visible cache directory without colliding.
| Field | Type | Description |
|---|---|---|
client |
Client | The HTTP client that fetches missing entries from Gravatar. |
root |
PathBuf | The avatars/ directory under storage.cache_dir that holds one file per hash. |
ttl |
Duration | How old an entry may be before it is refetched. |
AvatarCache::new
pub fn new(cache_dir: impl Into<PathBuf>) -> SelfConstruct a new cache rooted at cache_dir with the default TTL.
The directory is not created
eagerly — it is materialized on the first write in
Self::get_or_fetch.
| Parameter | Type | Description |
|---|---|---|
cache_dir |
impl Into<PathBuf> | cache_dir is the user-facing storage.cache_dir; the module appends its own avatars/ subdirectory internally. |
| returns | Self | A cache with a new HTTP client; nothing is created on disk yet. |
AvatarCache::with_ttl
pub fn with_ttl(cache_dir: impl Into<PathBuf>, ttl: Duration) -> SelfConstruct a new cache with an explicit TTL. Primarily used by tests.
| Parameter | Type | Description |
|---|---|---|
cache_dir |
impl Into<PathBuf> | the storage.cache_dir root; entries go in its avatars/ subdirectory |
ttl |
Duration | how old an entry may be before it is refetched |
| returns | Self | A cache with a new HTTP client. Nothing is created on disk yet. |
AvatarCache::get_or_fetch
pub async fn get_or_fetch(&self, hash: &str) -> Result<(Vec<u8>, Mime), Error>Return the cached bytes for hash, fetching from Gravatar on a miss or
when the on-disk copy is older than Self::ttl.
| Parameter | Type | Description |
|---|---|---|
hash |
&str | the Gravatar identifier hash; it must be non-empty and hexadecimal |
| returns | Result<(Vec<u8>, Mime), Error> | The returned MIME type is derived from the upstream Content-Type header when fetching and defaults to image/jpeg on cache hits, which matches Gravatar’s canonical response format. It is an error when the hash is invalid, the fetch, its status or reading its body fails, or the cache file cannot be read or written. |
AvatarCache::entry_path
fn entry_path(&self, hash: &str) -> PathBufResolve the on-disk path for a cache entry.
| Parameter | Type | Description |
|---|---|---|
hash |
&str | the already validated Gravatar identifier hash |
| returns | PathBuf | The path {root}/{hash}; the file may not exist. |
AvatarCache::read_fresh
fn read_fresh(&self, path: &Path) -> Result<Option<Vec<u8>>, Error>Read a cache entry if it exists and is still within TTL.
| Parameter | Type | Description |
|---|---|---|
path |
&Path | the entry’s file, from [Self::entry_path] |
| returns | Result<Option<Vec<u8>>, Error> | Returns Ok(None) for both misses and expired entries; expired entries are also eagerly removed so the caller always observes a clean slate on the subsequent write. Any other failure to read the file is an I/O error. |
AvatarCache::write
fn write(&self, path: &Path, bytes: &[u8]) -> Result<(), Error>Persist bytes to path, creating parent directories as needed.
| Parameter | Type | Description |
|---|---|---|
path |
&Path | the entry’s file, which is overwritten |
bytes |
&[u8] | the avatar image bytes |
| returns | Result<(), Error> | Ok(()) once the file is written, or the I/O error. |
fn validate_hash
fn validate_hash(hash: &str) -> Result<(), Error>Reject hashes that would escape the cache directory or contain suspicious characters. Gravatar hashes are always lowercase hexadecimal.
| Parameter | Type | Description |
|---|---|---|
hash |
&str | the identifier hash to check |
| returns | Result<(), Error> | Ok(()) when the hash is non-empty and every character is an ASCII hex digit, in either case; otherwise Error::InvalidValue. |