add<T> static method
Stores a value in the cache with the specified key if the key does not exist.
If the key is already present in the cache, this method does nothing and returns false.
If the key is not present, the value is stored, and it returns true.
- Parameter
keyThe unique identifier for the cached value. - Parameter
valueThe value to be stored in the cache. - Parameter
expiredAfterAn optional duration after which the cached value should expire. - Returns:
trueif the value was successfully stored in the cache, otherwisefalse. - Throws: An error if there is an issue during the cache storage process. Usage:
var success = Cache.add<User>("user", myUser, expiredAfter: Duration(days: 1));
Implementation
static Future<bool> add<T>(String key, T value,
{Duration? expiredAfter}) async {
final exist = has(key);
if (!exist) return put<T>(key, value, expiredAfter: expiredAfter);
return false;
}