Skip to content

putIfAbsent returns wrong value #26

@GenghisChen

Description

@GenghisChen

Using the cache in its ConcurrentMap form (.asMap()), when time-limited entries expire the .get() method returns null as is should, but the .putIfAbsent() method return the old-expired value.

Consider the following example:

    Cache<Integer, String> cache1 = Caffeine.newBuilder()
            .expireAfterWrite(1, TimeUnit.SECONDS)
            .build();
    ConcurrentMap<Integer, String> cache2 = cache1.asMap();

    cache2.put(1, "A");
    Thread.sleep(2000);
    System.out.println(cache2.get(1));  // Expect to see null (and see null)
    System.out.println(cache2.putIfAbsent(1, "B")); // Expect to see null (and see "A")

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions