예제 #1
0
    public async Task TestKeyExpire()
    {
        _cache.CacheOptions.Value.PenetrationSetting.Disable = true;

        var cacheKey1 = nameof(TestKeyExpire).ToLower() + ":" + DateTime.Now.Ticks;
        var cacheKey2 = nameof(TestKeyExpire).ToLower() + ":" + DateTime.Now.Ticks;
        var cacheKey3 = nameof(TestKeyExpire).ToLower() + ":" + DateTime.Now.Ticks;
        var cacheKey4 = "cacheKey4";

        await _cache.SetAsync(cacheKey1, nameof(cacheKey1), TimeSpan.FromSeconds(1000));

        await _cache.SetAsync(cacheKey2, nameof(cacheKey2), TimeSpan.FromSeconds(1000));

        await _cache.SetAsync(cacheKey3, nameof(cacheKey3), TimeSpan.FromSeconds(1000));

        var seconds = await _redisProvider.TTLAsync(cacheKey1);

        Assert.True(seconds > 990);

        await _cache.KeyExpireAsync(new string[] { cacheKey1, cacheKey2, cacheKey3, cacheKey4 }, 100);

        var seconds1 = await _redisProvider.TTLAsync(cacheKey1);

        var seconds2 = await _redisProvider.TTLAsync(cacheKey2);

        var seconds3 = await _redisProvider.TTLAsync(cacheKey3);

        Assert.True(seconds1 <= 100 && seconds1 > 90);
        Assert.True(seconds2 <= 100 && seconds1 > 90);
        Assert.True(seconds3 <= 100 && seconds1 > 90);

        var exists = await _cache.ExistsAsync(cacheKey4);

        Assert.False(exists);

        var value = await _cache.GetAsync <string>(cacheKey3);

        Assert.Equal("cacheKey3", value.Value);
    }