public bool Success(Tval val, TimeSpan outdated_ttl, out ProCacheEntry <Tval> entry)
            {
                entry = _entry;

                if (_completion is null)
                {
                    entry.Reset(val, outdated_ttl);
                    return(true);
                }

                _completion.SetResult(val);
                entry.Reset(val, null);

                return(false);
            }
            public bool Empty(TimeSpan outdated_ttl, out ProCacheEntry <Tval> entry)
            {
                entry = _entry;

                if (_completion is null)
                {
                    entry.Reset(outdated_ttl);
                    return(true);
                }

                entry.Reset(null);
                _completion.SetResult(default(Tval));

                return(false);
            }
Exemple #3
0
        private async ValueTask <Tval> UpdateAsync(Tkey key, ProCacheEntry <Tval> entry, object state, CancellationToken cancellation)
        {
            try
            {
                var res = await _get(key, state, cancellation).ConfigureAwait(false);

                entry.Reset(res, _outdateTtl);
                _cache.Set(key, entry, _expireTtl);
                return(res);
            }
            catch
            {
                entry.Reset();
                throw;
            }
        }
Exemple #4
0
            public bool Success(Tval val, TimeSpan outdated_ttl, out ProCacheEntry <Tval> entry)
            {
                switch (_state)
                {
                case ResultState.Add:
                    ((TaskCompletionSource <(bool, Tval)>)_completion).SetResult((true, val));
                    entry = null;
                    return(false);

                case ResultState.Outdate:
                    entry = (ProCacheEntry <Tval>)_completion;
                    entry.Reset(val, outdated_ttl);
                    return(true);
                }

                throw new NotImplementedException();
            }
Exemple #5
0
        private ValueTask <Tval> Add(Tkey key, object state, CancellationToken cancellation)
        {
            TaskCompletionSource <(bool, Tval)> completion;

            lock (ProCache <Tkey> .GetLock(key.GetHashCode()))
            {
                if (_cache.TryGet(key, out var res))
                {
                    return(((ProCacheEntry <Tval>)res).GetValue());
                }

                completion = new TaskCompletionSource <(bool, Tval)>();
                var entry = new ProCacheEntry <Tval>(completion.Task, _outdateTtl);
                _cache.Set(key, entry, _expireTtl);
            }

            return(AddAsync(key, completion, state, cancellation));
        }
Exemple #6
0
 public Result(ProCacheEntry <Tval> cache_entry)
 {
     _state      = ResultState.Outdate;
     _completion = cache_entry;
 }
 public Result(ProCacheEntry <Tval> entry, TaskCompletionSource <Tval> completion = null)
 {
     _entry      = entry;
     _completion = completion;
 }