public Task <(bool, T)> TryGet([NotNull] THash key)
 {
     return(_cache.TryGetValue(IWeakCacheProvider <T, THash> .HashKey(key), out var weakRef) &&
            weakRef.Target is { } target
             ? Task.FromResult((true, target))
             : Task.FromResult((false, (T)null)));
 }
예제 #2
0
        public void Detach(THash associateWith)
        {
            using var sem = new SemaphoreSlim(1);
            var path = Path.Combine(initDirectory, IWeakCacheProvider <T, THash> .HashKey(associateWith) + ".");

            if (File.Exists(path))
            {
                fileMapping.TryRemove(associateWith, out _);
                File.Delete(path);
            }
        }
예제 #3
0
        public void Attach(ref T key, THash associateWith)
        {
            if (associateWith == null || key == null)
            {
                return;
            }
            var weakRef = new WeakEntry <T>(key);

            key = null;
            _cache.TryAdd(IWeakCacheProvider <T, THash> .HashKey(associateWith), weakRef);
        }
예제 #4
0
        public void Attach(ref T key, THash associateWith)
        {
            if (associateWith == null || key == null)
            {
                return;
            }
            var path = Path.Combine(initDirectory, IWeakCacheProvider <T, THash> .HashKey(associateWith) + ".tmp");

            if (!File.Exists(path))
            {
                var s = cachingPolicy(key);
                key = null;
                Task.Run(() =>
                {
                    fileMapping.TryAdd(associateWith, path);
                    WriteFile(path, s);
                });
            }
        }
예제 #5
0
 public void Detach(THash associateWith)
 {
     _cache.TryRemove(IWeakCacheProvider <T, THash> .HashKey(associateWith), out _);
 }