v0.1.3
🤷 Introducing MaybeValue<T>
(more)
A new type has been introduced to model a value that may be there or not, just like the standard .NET nullables but for both reference and value types.
It supports implicit convesion to/from T
and is used in a couple of places (see below).
🕹️ Better TryGet[Async]
(more)
The TryGet[Async]
return type is now MaybeValue<T>
for better ease of use (more like the standard .NET nullables).
Example:
var maybeFoo = cache.TryGet<int>("foo");
if (maybeFoo.HasValue) {
// EXPLICIT ACCESS
var foo = maybeFoo.Value;
// OR IMPLICIT CONVERSION
int foo = maybeFoo;
}
🕹️ Better GetOrSet[Async]
(more)
The GetOrSet[Async]
methods now has an additional failSafeDefaultValue
param to handle factory failures (with fail-safe enabled) in case there's no expired/stale value to use (eg: cold start, first use, etc).
Example:
// WITH FAIL-SAFE ENABLED
cache.GetOrSet<int>("foo", _ => GetFooFromDb(), 42);
There's also a new GetOrSet[Async]
overload available that accepts a value directly instead of a factory: this covers the cases when you already have a default value to use so you don't have to allocate a useless lambda.
Example:
// BEFORE
cache.GetOrSet<int>("foo", _ => 42);
// FROM v0.1.3
cache.GetOrSet<int>("foo", 42);
⚠️ Breaking changes
The TryGet[Async]
methods now returns a MaybeValue<T>
instead of a TryGetResult<T>
.
This new type also contains the Success
prop present in the old TryGetResult<T>
type, but marked as [Obsolete]
to better ease the transition.
Also, the old TryGetResult<T>
type is still there, in case you've used that somewhere, but the type as a whole has been marked as [Obsolete]
to warn of the usage.
The only case of an actual breaking change is if you were doing something like this:
// USING IMPLICIT bool CONVERSION
if (cache.TryGet<int>("foo")) {
[...]
}
but it should have been rare, since the value would have been discarded.
Anyway, in that case, you simply have to do this now:
if (cache.TryGet<int>("foo").HasValue) {
[...]
}
or, even better:
var maybeFoo = cache.TryGet<int>("foo");
if (maybeFoo.HasValue) {
// EXPLICIT ACCESS
var foo = maybeFoo.Value;
// OR IMPLICIT CONVERSION
int foo = maybeFoo;
}
🚀 Performance
Optimized cpu and memory usage so that common scenarios will consume less resources.