package com.lanyuanxiaoyao.service.scheduler;

import com.github.benmanes.caffeine.cache.CacheLoader;
import com.github.benmanes.caffeine.cache.Caffeine;
import com.github.benmanes.caffeine.cache.LoadingCache;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.checker.nullness.qual.Nullable;

/**
 * @author lanyuanxiaoyao
 */
public class TestCache {
    public static void main(String[] args) {
        CacheLoaderImpl loader = new CacheLoaderImpl();
        LoadingCache<String, Boolean> cache = Caffeine.newBuilder()
                .build(loader);
        loader.setBindCache(cache);
        System.out.println(cache.asMap());
        System.out.println(cache.get("one"));
        System.out.println(cache.asMap());
    }

    public static class CacheLoaderImpl implements CacheLoader<String, Boolean> {
        private LoadingCache<String, Boolean> bindCache;

        public void setBindCache(LoadingCache<String, Boolean> bindCache) {
            this.bindCache = bindCache;
        }

        @Override
        public @Nullable Boolean load(@NonNull String key) throws Exception {
            bindCache.put("two", false);
            return true;
        }
    }
}
