Exemple #1
0
        public async Task TestGetOrAddStrongFingerprintAsyncExisting()
        {
            var strongFp = StrongFingerprint.Random();
            var hashList = new ContentHashListWithDeterminism(ContentHashList.Random(), CacheDeterminism.None);

            var initialData = new Dictionary <RedisKey, RedisValue>
            {
                { _redisSerializer.ToRedisKey(strongFp).Prepend(RedisNameSpace), _redisSerializer.ToRedisValue(hashList) },
            };

            using (var mockDb = new MockRedisDatabase(SystemClock.Instance, initialData))
            {
                await RunTest(
                    mockDb,
                    async (context, metadataCache, redisDb) =>
                {
                    var getContentHashListResult = await metadataCache.GetOrAddContentHashListAsync(
                        context,
                        strongFp,
                        fp =>
                    {
                        throw new InvalidOperationException(
                            "GetFunc not expected to be called since data is already present in the cache");
                    });

                    // Check result
                    Assert.True(getContentHashListResult.Succeeded);
                    Assert.Equal(hashList, getContentHashListResult.ContentHashListWithDeterminism);
                });
            }
        }
Exemple #2
0
        public void ContentHashListWithDeterminismToRedisValue()
        {
            var chl        = ContentHashList.Random();
            var d          = CacheDeterminism.ViaCache(CacheDeterminism.NewCacheGuid(), DateTime.UtcNow);
            var chlwd      = new ContentHashListWithDeterminism(chl, d);
            var redisValue = _serializer.ToRedisValue(chlwd);
            var expected   =
                HexUtilities.BytesToHex(d.Serialize()) +
                RedisSerializer.RedisValueSeparator +
                RedisSerializer.RedisValueExistsSemaphore +
                RedisSerializer.RedisValueSeparator +
                chl.Serialize() +
                RedisSerializer.RedisValueSeparator +
                RedisSerializer.RedisValueSeparator;

            Assert.Equal(expected, redisValue);
        }
Exemple #3
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));
            }
        }