pub struct MetadataCache {
pool: Pool<MySql>,
cache: HashMap<String, String>,
}
Expand description
Cache implementation for metadata to reduce database access.
This cache maintains an in-memory copy of metadata values to minimize database queries. It provides methods to get and set values, as well as to refresh the entire cache from the database.
§Fields
pool
- Database connection pool for executing queriescache
- In-memory hash map storing key-value pairs
§Thread Safety
This struct is not thread-safe on its own and should either be used from a
single thread or wrapped in a synchronization primitive like Arc<Mutex<MetadataCache>>
.
§Performance Considerations
The cache can significantly reduce database load for frequently accessed
metadata values, but it can become stale if other processes modify the
metadata table directly. Use refresh_cache
periodically or after expected
external changes.
Fields§
§pool: Pool<MySql>
Database connection pool for executing queries
cache: HashMap<String, String>
In-memory cache of metadata key-value pairs
Implementations§
Source§impl MetadataCache
impl MetadataCache
Sourcepub async fn get(&mut self, key: &str) -> Result<String>
pub async fn get(&mut self, key: &str) -> Result<String>
Retrieves a metadata value by its key, using the cache when possible.
This method first checks the in-memory cache for the requested key. If the key is not found in the cache, it queries the database and updates the cache with the result.
§Arguments
key
- The unique key whose value to retrieve
§Returns
Ok(String)
- Successfully retrieved the value for the keyErr(anyhow::Error)
- Failed to fetch the value or key doesn’t exist
§Cache Behavior
This method:
- Returns cached values without querying the database when possible
- Automatically populates the cache with values fetched from the database
- Does not refresh existing cache entries (use
refresh_cache
for that)
Sourcepub async fn set(&mut self, key: &str, value: &str) -> Result<()>
pub async fn set(&mut self, key: &str, value: &str) -> Result<()>
Sets a metadata value for a specific key and updates the cache.
This method updates both the database and the in-memory cache with
the new key-value pair. This ensures that subsequent get
calls
will return the updated value without requiring a database query.
§Arguments
key
- The unique key to associate with the valuevalue
- The value to store
§Returns
Ok(())
- Successfully stored the key-value pairErr(anyhow::Error)
- Failed to store the key-value pair
§Error Handling
If the database update fails, the cache is not updated, ensuring consistency between the cache and the database.
Sourcepub async fn refresh_cache(&mut self) -> Result<()>
pub async fn refresh_cache(&mut self) -> Result<()>
Refreshes the entire cache from the database.
This method clears the in-memory cache and reloads all metadata entries from the database. It’s useful when the cache might be stale due to external changes to the metadata table.
§Returns
Ok(())
- Successfully refreshed the cacheErr(anyhow::Error)
- Failed to refresh the cache
§Use Cases
This method is particularly useful in scenarios such as:
- After application startup to prime the cache
- After scheduled maintenance that might have modified metadata
- When cache staleness is detected or suspected
- Periodically in long-running applications to ensure cache freshness
Auto Trait Implementations§
impl Freeze for MetadataCache
impl !RefUnwindSafe for MetadataCache
impl Send for MetadataCache
impl Sync for MetadataCache
impl Unpin for MetadataCache
impl !UnwindSafe for MetadataCache
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
§impl<T> IntoCollection<T> for T
impl<T> IntoCollection<T> for T
§fn into_collection<A>(self) -> SmallVec<A>where
A: Array<Item = T>,
fn into_collection<A>(self) -> SmallVec<A>where
A: Array<Item = T>,
self
into a collection.fn mapped<U, F, A>(self, f: F) -> SmallVec<A>where
F: FnMut(T) -> U,
A: Array<Item = U>,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left
is true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left(&self)
returns true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read more§impl<T> Paint for Twhere
T: ?Sized,
impl<T> Paint for Twhere
T: ?Sized,
§fn fg(&self, value: Color) -> Painted<&T>
fn fg(&self, value: Color) -> Painted<&T>
Returns a styled value derived from self
with the foreground set to
value
.
This method should be used rarely. Instead, prefer to use color-specific
builder methods like red()
and
green()
, which have the same functionality but are
pithier.
§Example
Set foreground color to white using fg()
:
use yansi::{Paint, Color};
painted.fg(Color::White);
Set foreground color to white using white()
.
use yansi::Paint;
painted.white();
§fn bright_black(&self) -> Painted<&T>
fn bright_black(&self) -> Painted<&T>
§fn bright_red(&self) -> Painted<&T>
fn bright_red(&self) -> Painted<&T>
§fn bright_green(&self) -> Painted<&T>
fn bright_green(&self) -> Painted<&T>
§fn bright_yellow(&self) -> Painted<&T>
fn bright_yellow(&self) -> Painted<&T>
§fn bright_blue(&self) -> Painted<&T>
fn bright_blue(&self) -> Painted<&T>
§fn bright_magenta(&self) -> Painted<&T>
fn bright_magenta(&self) -> Painted<&T>
§fn bright_cyan(&self) -> Painted<&T>
fn bright_cyan(&self) -> Painted<&T>
§fn bright_white(&self) -> Painted<&T>
fn bright_white(&self) -> Painted<&T>
§fn bg(&self, value: Color) -> Painted<&T>
fn bg(&self, value: Color) -> Painted<&T>
Returns a styled value derived from self
with the background set to
value
.
This method should be used rarely. Instead, prefer to use color-specific
builder methods like on_red()
and
on_green()
, which have the same functionality but
are pithier.
§Example
Set background color to red using fg()
:
use yansi::{Paint, Color};
painted.bg(Color::Red);
Set background color to red using on_red()
.
use yansi::Paint;
painted.on_red();
§fn on_primary(&self) -> Painted<&T>
fn on_primary(&self) -> Painted<&T>
§fn on_magenta(&self) -> Painted<&T>
fn on_magenta(&self) -> Painted<&T>
§fn on_bright_black(&self) -> Painted<&T>
fn on_bright_black(&self) -> Painted<&T>
§fn on_bright_red(&self) -> Painted<&T>
fn on_bright_red(&self) -> Painted<&T>
§fn on_bright_green(&self) -> Painted<&T>
fn on_bright_green(&self) -> Painted<&T>
§fn on_bright_yellow(&self) -> Painted<&T>
fn on_bright_yellow(&self) -> Painted<&T>
§fn on_bright_blue(&self) -> Painted<&T>
fn on_bright_blue(&self) -> Painted<&T>
§fn on_bright_magenta(&self) -> Painted<&T>
fn on_bright_magenta(&self) -> Painted<&T>
§fn on_bright_cyan(&self) -> Painted<&T>
fn on_bright_cyan(&self) -> Painted<&T>
§fn on_bright_white(&self) -> Painted<&T>
fn on_bright_white(&self) -> Painted<&T>
§fn attr(&self, value: Attribute) -> Painted<&T>
fn attr(&self, value: Attribute) -> Painted<&T>
Enables the styling [Attribute
] value
.
This method should be used rarely. Instead, prefer to use
attribute-specific builder methods like bold()
and
underline()
, which have the same functionality
but are pithier.
§Example
Make text bold using attr()
:
use yansi::{Paint, Attribute};
painted.attr(Attribute::Bold);
Make text bold using using bold()
.
use yansi::Paint;
painted.bold();
§fn rapid_blink(&self) -> Painted<&T>
fn rapid_blink(&self) -> Painted<&T>
§fn quirk(&self, value: Quirk) -> Painted<&T>
fn quirk(&self, value: Quirk) -> Painted<&T>
Enables the yansi
[Quirk
] value
.
This method should be used rarely. Instead, prefer to use quirk-specific
builder methods like mask()
and
wrap()
, which have the same functionality but are
pithier.
§Example
Enable wrapping using .quirk()
:
use yansi::{Paint, Quirk};
painted.quirk(Quirk::Wrap);
Enable wrapping using wrap()
.
use yansi::Paint;
painted.wrap();
§fn clear(&self) -> Painted<&T>
👎Deprecated since 1.0.1: renamed to resetting()
due to conflicts with Vec::clear()
.
The clear()
method will be removed in a future release.
fn clear(&self) -> Painted<&T>
resetting()
due to conflicts with Vec::clear()
.
The clear()
method will be removed in a future release.§fn whenever(&self, value: Condition) -> Painted<&T>
fn whenever(&self, value: Condition) -> Painted<&T>
Conditionally enable styling based on whether the [Condition
] value
applies. Replaces any previous condition.
See the crate level docs for more details.
§Example
Enable styling painted
only when both stdout
and stderr
are TTYs:
use yansi::{Paint, Condition};
painted.red().on_yellow().whenever(Condition::STDOUTERR_ARE_TTY);