예제 #1
0
            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);
            }
예제 #2
0
            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);
            }
예제 #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;
            }
        }
예제 #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();
            }
예제 #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));
        }
예제 #6
0
 public Result(ProCacheEntry <Tval> cache_entry)
 {
     _state      = ResultState.Outdate;
     _completion = cache_entry;
 }
예제 #7
0
 public Result(ProCacheEntry <Tval> entry, TaskCompletionSource <Tval> completion = null)
 {
     _entry      = entry;
     _completion = completion;
 }