Skip to content

LoadingCache refresh() appears to not update newValue to cache #467

@dyimo

Description

@dyimo

Hello.
I have found an issue (tested in 2.8.6), which is not necessarily a bug. I'd like to get your thoughts on what to do about it.
When the refresh loop execution interval is less than the execution time of cacheload.After the loop is executed several times. It is found that the data of cache get is no longer changed.

Here is the code:

    @Test
    public void testRefresh() throws InterruptedException {
        AtomicInteger atomicInteger = new AtomicInteger(0);
        AtomicInteger incr = new AtomicInteger(100000);
        LoadingCache<Key, String> dataCache = Caffeine.newBuilder()
                .expireAfterWrite(8, TimeUnit.SECONDS)
                .refreshAfterWrite(1, TimeUnit.SECONDS)
                .build(key -> {
                    Thread.sleep(4_000);
                    int i = atomicInteger.incrementAndGet();
                    String result = "key==:" + key.incr + ",value:" + i + "---------cache";
                    logger.info(result);
                    return result;
                });

        while (true) {
            Key key = new Key(incr.getAndIncrement());
            dataCache.refresh(key);
            logger.info(dataCache.get(key) + "..........get");
            Thread.sleep(1_000);
        }

    }
    static class Key {
        int code = 0;
        int incr;

        public Key(int incr) {
            this.incr = incr;
        }

        @Override
        public boolean equals(Object o) {
            if (this == o) return true;
            if (o == null || getClass() != o.getClass()) return false;
            Key key = (Key) o;
            return code == key.code;
        }

        @Override
        public int hashCode() {
            return Objects.hash(code);
        }
    }

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