コード例 #1
0
    public async Task TestBloomFilter()
    {
        var cacheKey = nameof(TestBloomFilter).ToLower();

        await _redisProvider.KeyDelAsync(cacheKey);

        await _redisProvider.BfReserveAsync(cacheKey, 0.001, 10000000);

        var initValues = new List <string>();

        for (int index = 0; index < 100000; index++)
        {
            initValues.Add($"adnc{index}");
        }
        await _redisProvider.BfAddAsync(cacheKey, initValues);

        var trueResult = await _redisProvider.BfExistsAsync(cacheKey, "adnc100");

        Assert.True(trueResult);

        var falseResult = await _redisProvider.BfExistsAsync(cacheKey, "adnc");

        Assert.False(falseResult);

        var values = new List <string>()
        {
            "adnc88888", "adnc78888", "adnc68888", "adnc58888"
        };
        var results = await _redisProvider.BfExistsAsync(cacheKey, values);

        var trueLength = results.Where(x => x == true).Count();

        Assert.Equal(values.Count, trueLength);

        values = new List <string>()
        {
            "danc888889", "danc888888", "danc8888888", "danc8888889"
        };
        results = await _redisProvider.BfExistsAsync(cacheKey, values);

        var falseLength = results.Where(x => x == false).Count();

        Assert.Equal(values.Count, falseLength);
    }