SmartCache class
A singleton cache management class that provides caching functionality with automatic maintenance.
The SmartCache class implements a caching mechanism with the following features:
- Singleton pattern ensuring only one instance exists
- Automatic periodic maintenance to clear expired entries
- Configurable default expiration time for cached items
- Configurable maintenance interval
Example usage:
final cache = SmartCache(
defaultExpiration: Duration(minutes: 10),
maintenanceInterval: Duration(minutes: 30)
);
// Store data
await cache.set('key', someData);
// Retrieve data
final data = await cache.get<SomeType>('key');
The cache automatically performs maintenance at regular intervals to remove expired entries. You can also manually trigger maintenance by calling maintenance().
Don't forget to call close() when the cache is no longer needed to properly dispose of resources and stop the maintenance timer.
Constructors
- SmartCache.new({Duration defaultExpiration = const Duration(minutes: 10), Duration maintenanceInterval = const Duration(minutes: 30)})
-
factory
Properties
- defaultExpiration → Duration
-
final
- hashCode → int
-
The hash code for this object.
no setterinherited
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
Methods
-
clear(
) → Future< void> - Clears all cached data from the database.
-
close(
) → Future< void> - Closes the cache and releases all resources.
-
get<
T> (String key, {Duration? expiration}) → Future< T?> -
Retrieves a cached value of type
T
associated with the givenkey
. -
getKeysByPrefix(
String prefix) → Future< List< String> > - Returns a list of all cache keys that start with the given prefix.
-
maintenance(
) → Future< void> - Performs maintenance operations on the cache.
-
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
remove(
String key) → Future< void> -
Removes an entry from the database associated with the given
key
. -
removeByPattern(
String pattern) → Future< void> - Removes all entries from the database whose keys match the given pattern.
-
set(
String key, dynamic data) → Future< void> - Stores data in the cache using a key-value pair.
-
toString(
) → String -
A string representation of this object.
inherited
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited