17 lines
473 B
C#
17 lines
473 B
C#
|
|
namespace SJK.Functional;
|
||
|
|
|
||
|
|
public class Cache<T> where T : class
|
||
|
|
{
|
||
|
|
private T? _cache;
|
||
|
|
private Func<T> _provider;
|
||
|
|
private readonly Func<T, bool>? _isvalid;
|
||
|
|
|
||
|
|
public Cache(Func<T> provider, Func<T, bool>? isvalid = null)
|
||
|
|
{
|
||
|
|
_provider = provider;
|
||
|
|
_isvalid = isvalid;
|
||
|
|
}
|
||
|
|
public bool IsCached() => _cache is not null;
|
||
|
|
public T Value => _cache is null || (_isvalid is not null && !_isvalid(_cache)) ? _cache = _provider() : _cache;
|
||
|
|
|
||
|
|
}
|