Ejemplo n.º 1
0
        private void TestContentHashListWithDeterminismRoundtrip(ContentHashListWithDeterminism contentHashListWithDeterminism)
        {
            var redisValue        = _serializer.ToRedisValue(contentHashListWithDeterminism);
            var roundtrippedValue = _serializer.AsContentHashList(redisValue);

            Assert.Equal(contentHashListWithDeterminism, roundtrippedValue);
        }
Ejemplo n.º 2
0
        /// <inheritdoc />
        public async Task <GetContentHashListResult> GetOrAddContentHashListAsync(Context context, StrongFingerprint strongFingerprint, Func <StrongFingerprint, Task <GetContentHashListResult> > getFuncAsync)
        {
            try
            {
                var        cacheKey = _redisSerializer.ToRedisKey(strongFingerprint);
                RedisValue cacheResult;

                Stopwatch stopwatch = Stopwatch.StartNew();
                try
                {
                    _cacheTracer.GetContentHashListStart(context);
                    cacheResult = await StringGetWithExpiryBumpAsync(context, cacheKey);
                }
                finally
                {
                    _cacheTracer.GetContentHashListStop(context, stopwatch.Elapsed);
                }

                if (!cacheResult.HasValue)
                {
                    _cacheTracer.RecordContentHashListFetchedFromBackingStore(context, strongFingerprint);
                    GetContentHashListResult getResult = await getFuncAsync(strongFingerprint);

                    if (getResult.Succeeded)
                    {
                        var cacheValue = _redisSerializer.ToRedisValue(getResult.ContentHashListWithDeterminism);

                        stopwatch = Stopwatch.StartNew();
                        try
                        {
                            _cacheTracer.AddContentHashListStart(context);
                            bool result = await StringSetWithExpiryBumpAsync(context, cacheKey, cacheValue);

                            context.Logger.Debug($"Added redis cache entry for {strongFingerprint}: {getResult}. Result: {result}");
                        }
                        finally
                        {
                            _cacheTracer.AddContentHashListStop(context, stopwatch.Elapsed);
                        }
                    }

                    return(getResult);
                }
                else
                {
                    _cacheTracer.RecordGetContentHashListFetchedDistributed(context, strongFingerprint);
                }

                return(new GetContentHashListResult(_redisSerializer.AsContentHashList(cacheResult)));
            }
            catch (Exception ex)
            {
                return(new GetContentHashListResult(ex));
            }
        }
Ejemplo n.º 3
0
        public Task TestGetOrAddStrongFingerprintAsyncEmptyCache()
        {
            return(RunTest(async(context, metadataCache, redisDb) =>
            {
                var strongFp = StrongFingerprint.Random();
                var hashList = new ContentHashListWithDeterminism(ContentHashList.Random(), CacheDeterminism.None);
                var getContentHashListResult = await metadataCache.GetOrAddContentHashListAsync(context, strongFp, fp => ToContentHashListResult(hashList));

                Assert.True(getContentHashListResult.Succeeded);

                // Check Redis data
                Assert.Equal(1, redisDb.GetDbWithExpiry().Keys.Count);

                var cacheKey = _redisSerializer.ToRedisKey(strongFp).Prepend(RedisNameSpace);
                Assert.True(redisDb.GetDbWithExpiry().ContainsKey(cacheKey));
                Assert.Equal(hashList, _redisSerializer.AsContentHashList(redisDb.GetDbWithExpiry()[cacheKey].Value));
            }));
        }