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

org.zodiac.cache.reactive.config.ReactiveCacheInfo Maven / Gradle / Ivy

The newest version!
package org.zodiac.cache.reactive.config;

import java.util.Objects;

import org.springframework.context.ApplicationContext;
import org.springframework.core.ResolvableType;
import org.springframework.data.redis.core.ReactiveRedisOperations;
import org.zodiac.cache.reactive.ReactiveCache;
import org.zodiac.cache.reactive.ReactiveCacheManager;
import org.zodiac.cache.reactive.supports.RedisLocalReactiveCacheManager;
import org.zodiac.cache.reactive.supports.UnsupportedReactiveCache;
import org.zodiac.sdk.toolkit.util.ClassUtil;
import org.zodiac.sdk.toolkit.util.lang.StrUtil;

public class ReactiveCacheInfo {

    private boolean enabled = false;

    private Type type = Type.none;

    private final GuavaCacheInfo guava = new GuavaCacheInfo();

    private final CaffeineCacheInfo caffeine = new CaffeineCacheInfo();

    private final RedisCacheInfo redis = new RedisCacheInfo();

    public ReactiveCacheInfo() {
        super();
    }

    public boolean isEnabled() {
        return enabled;
    }

    public void setEnabled(boolean enabled) {
        this.enabled = enabled;
    }

    public Type getType() {
        return type;
    }

    public void setType(Type type) {
        this.type = type;
    }

    public GuavaCacheInfo getGuava() {
        return guava;
    }

    public CaffeineCacheInfo getCaffeine() {
        return caffeine;
    }

    public RedisCacheInfo getRedis() {
        return redis;
    }

    public boolean anyProviderPresent() {
        return ClassUtil.isPresent("com.google.common.cache.Cache", this.getClass().getClassLoader())
                || ClassUtil.isPresent("com.github.benmanes.caffeine.cache.Cache", this.getClass().getClassLoader())
                || ClassUtil.isPresent("org.springframework.data.redis.core.ReactiveRedisOperations", this.getClass().getClassLoader());
    }

    @SuppressWarnings("all")
    public ReactiveCacheManager createCacheManager(ApplicationContext context) {
        if (!anyProviderPresent()) {
            return new ReactiveCacheManager() {
                @Override
                public  ReactiveCache getCache(String name) {
                    return UnsupportedReactiveCache.getInstance();
                }
            };
        }

        if (type == Type.redis) {
            ReactiveRedisOperations operations;
            if (StrUtil.isNotBlank(redis.getBeanName())) {
                operations = context.getBean(redis.getBeanName(), ReactiveRedisOperations.class);
            } else {
                operations = (ReactiveRedisOperations) context.getBeanProvider(ResolvableType.forClassWithGenerics(ReactiveRedisOperations.class, Object.class, Object.class)).getIfAvailable();
            }
            return new RedisLocalReactiveCacheManager(operations, this.createCacheManager(redis.getLocalCacheType()));
        }

        return createCacheManager(type);
    }

    @Override
    public int hashCode() {
        return Objects.hash(caffeine, enabled, guava, redis, type);
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        ReactiveCacheInfo other = (ReactiveCacheInfo)obj;
        return Objects.equals(caffeine, other.caffeine) && enabled == other.enabled
            && Objects.equals(guava, other.guava) && Objects.equals(redis, other.redis) && type == other.type;
    }

    @Override
    public String toString() {
        return "[enabled=" + enabled + ", type=" + type + ", guava=" + guava + ", caffeine="
            + caffeine + ", redis=" + redis + "]";
    }

    private ReactiveCacheManager createUnsupported() {
        return new ReactiveCacheManager() {
            @Override
            public  ReactiveCache getCache(String name) {
                return UnsupportedReactiveCache.getInstance();
            }
        };
    }

    private ReactiveCacheManager createCacheManager(Type type) {
        switch (type) {
            case guava:
                return getGuava().createCacheManager();
            case caffeine:
                return getCaffeine().createCacheManager();
        }
        return createUnsupported();
    }

    enum Strength {
        WEAK, SOFT,;
    }

    public enum Type {
        redis, caffeine, guava, none,;
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy