All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.quarkus.cache.runtime.devui.CacheJsonRPCService Maven / Gradle / Ivy

package io.quarkus.cache.runtime.devui;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Comparator;
import java.util.List;
import java.util.Optional;

import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;

import org.jboss.logging.Logger;

import io.quarkus.cache.Cache;
import io.quarkus.cache.CacheManager;
import io.quarkus.cache.CaffeineCache;
import io.quarkus.cache.runtime.caffeine.CaffeineCacheImpl;
import io.smallrye.common.annotation.NonBlocking;
import io.smallrye.mutiny.Uni;
import io.vertx.core.json.JsonArray;
import io.vertx.core.json.JsonObject;

@ApplicationScoped
public class CacheJsonRPCService {

    @Inject
    CacheManager manager;

    @Inject
    Logger logger;

    @NonBlocking
    public JsonArray getAll() {
        Collection names = manager.getCacheNames();
        List allCaches = new ArrayList<>(names.size());
        for (String name : names) {
            Optional cache = manager.getCache(name);
            if (cache.isPresent() && cache.get() instanceof CaffeineCache) {
                allCaches.add((CaffeineCache) cache.get());
            }
        }
        allCaches.sort(Comparator.comparing(CaffeineCache::getName));

        var array = new JsonArray();
        for (CaffeineCache cc : allCaches) {
            array.add(getJsonRepresentationForCache(cc));
        }
        return array;
    }

    private JsonObject getJsonRepresentationForCache(Cache cc) {
        return new JsonObject().put("name", cc.getName()).put("size", ((CaffeineCacheImpl) cc).getSize());
    }

    public Uni clear(String name) {
        Optional cache = manager.getCache(name);
        if (cache.isPresent()) {
            return cache.get().invalidateAll().map((t) -> getJsonRepresentationForCache(cache.get()));
        } else {
            return Uni.createFrom().item(new JsonObject().put("name", name).put("size", -1));
        }
    }

    @NonBlocking
    public JsonObject refresh(String name) {
        Optional cache = manager.getCache(name);
        if (cache.isPresent()) {
            return getJsonRepresentationForCache(cache.get());
        } else {
            return new JsonObject().put("name", name).put("size", -1);
        }
    }

    public JsonArray getKeys(String name) {
        Optional cache = manager.getCache(name);
        if (cache.isPresent()) {
            CaffeineCache caffeineCache = (CaffeineCache) cache.get();
            JsonArray keys = new JsonArray();
            for (Object key : caffeineCache.keySet()) {
                keys.add(key.toString());
            }
            return keys;
        } else {
            return JsonArray.of();
        }
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy