示例#1
0
        private async Task <bool> SafeAddOrReplaceWithMerging(TKey key, IAsyncCacheData <TKey, TValue> data,
                                                              CancellationToken token)
        {
            var dbdata = await _keyBasedFetcher.GetAsync(key, token).ConfigureAwait(false);

            if (dbdata == null || dbdata.Count == 0)
            {
                return(false);
            }
            TValue value;

            if (dbdata.Count == 1)
            {
                value = dbdata[0].Value;
                data.AddOrReplace(key, value);
            }
            else
            {
                value = dbdata[0].Value;
                for (var i = 1; i < dbdata.Count; i++)
                {
                    TValue outVal;
                    if (dbdata[i].MergeCurrentDataForKeyConflict(value, out outVal))
                    {
                        value = outVal;
                    }
                }
                data.AddOrReplace(key, value);
            }
            return(true);
        }
示例#2
0
        public override Task <bool> AddOrReplaceForAsync(TKey key, IAsyncCacheData <TKey, TValue> data,
                                                         CancellationToken token)
        {
            //We do NOT do key look up in FILE data.
            //if there is a fallback, we ask it
            var fallback = Fallback;

            Thread.MemoryBarrier();
            return(fallback != null?fallback.AddOrReplaceForAsync(key, data, token) : Task.FromResult(false));
        }
示例#3
0
        private async Task <bool> SafeAddOrReplaceWithoutMerging(TKey key, IAsyncCacheData <TKey, TValue> data,
                                                                 CancellationToken token)
        {
            var hasRow = await _keyBasedFetcher.HasRowAsync(key, token).ConfigureAwait(false);

            if (!hasRow)
            {
                return(false);
            }
            data.AddOrReplace(key, default(TValue));
            return(true);
        }
 internal override void Merge(IAsyncCacheData <TKey, TValue> target, CancellationToken token)
 {
     for (var i = 0; i < SyncRoots.Length; i++)
     {
         var syncRoot = SyncRoots[i];
         lock (syncRoot)
         {
             token.ThrowIfCancellationRequested();
             target.AddOrReplace(_data[i].AsEnumerable());
         }
     }
 }
示例#5
0
 public override Task <bool> AddOrReplaceForAsync(TKey key, IAsyncCacheData <TKey, TValue> data,
                                                  CancellationToken token)
 {
     try
     {
         _localCts.Token.ThrowIfCancellationRequested();
         return(SafeAddOrReplaceFor(key, data, token));
     }
     catch (OperationCanceledException)
     {
         throw;
     }
     catch (Exception e)
     {
         throw new AsyncCacheException(AsyncCacheErrorCode.InvalidCache, "Unknown error", e);
     }
 }
示例#6
0
 private Task <bool> SafeAddOrReplaceFor(TKey key, IAsyncCacheData <TKey, TValue> data, CancellationToken token)
 {
     return(data.ValueTypeCanBeMerged
         ? SafeAddOrReplaceWithMerging(key, data, token)
         : SafeAddOrReplaceWithoutMerging(key, data, token));
 }
示例#7
0
 internal override void Merge(IAsyncCacheData <TKey, TValue> target, CancellationToken token)
 {
 }
示例#8
0
 public abstract Task <bool> AddOrReplaceForAsync(TKey key, IAsyncCacheData <TKey, TValue> data, CancellationToken token);
示例#9
0
 internal abstract void Merge(IAsyncCacheData <TKey, TValue> target, CancellationToken token);