Init Scripts in new repo
This commit is contained in:
17
Functional/Cache.cs
Normal file
17
Functional/Cache.cs
Normal file
@@ -0,0 +1,17 @@
|
||||
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;
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user