Skip to content

InMemoryCaching: Difference between Set() and Add() #551

Description

@ricsiLT

Description

Why is there a difference between these two methods? What are the uses for them? From testing I see that one of them increments the _cacheSize and the other one doesn't. Why is that?

Related code

        var maxSize = 50_000;
        var cacheOptions = new InMemoryCachingOptions
        {
            SizeLimit = maxSize,
            ExpirationScanFrequency = 1
        };
        var cache = new InMemoryCaching("default", cacheOptions);
        foreach (var i in Enumerable.Range(1, maxSize))
        {
            cache.Add(i.ToString(), i);
        }

        foreach (var i in Enumerable.Range(maxSize, 10))
        {
            cache.Set(i.ToString(), i);
        }

        cache.Set("50011", 11);
        await Task.Delay(6000); // Just in case expiration cleanup is in progress??

        Assert.NotNull(cache.Get("50011"));
        Assert.Null(cache.Get("1")); // I'd expect this key to be missing, but it's not??

Expected behavior: [What you expected to happen]

Regardless of .Add() or .Set(), SizeLimit should be honored.

Actual behavior: [What actually happened]

When using .Set(), size limit is honored.
When using .Add(), size limit is ignored.

Specifications

    <PackageReference Include="EasyCaching.InMemory" Version="1.9.2" />

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions