org.babyfish.jimmer.sql.cache.UsedCacheImpl Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of jimmer-sql Show documentation
Show all versions of jimmer-sql Show documentation
A revolutionary ORM framework for both java and kotlin
package org.babyfish.jimmer.sql.cache;
import org.babyfish.jimmer.Draft;
import org.babyfish.jimmer.meta.ImmutableProp;
import org.babyfish.jimmer.meta.ImmutableType;
import org.babyfish.jimmer.meta.TargetLevel;
import org.babyfish.jimmer.runtime.ImmutableSpi;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.*;
import java.util.function.Supplier;
class UsedCacheImpl implements UsedCache {
private static final ThreadLocal>> LOADING_CACHES_LOCAL =
new ThreadLocal<>();
protected final Cache raw;
private final CacheOperator operator;
UsedCacheImpl(Cache raw, CacheOperator operator) {
this.raw = Objects.requireNonNull(raw, "raw cannot be null");
this.operator = operator;
}
static UsedCache wrap(
Cache cache,
CacheOperator operator
) {
if (cache == null) {
return null;
}
if (cache instanceof UsedCache, ?>) {
UsedCacheImpl wrapper = (UsedCacheImpl) cache;
if (wrapper.operator == operator) {
return wrapper;
}
cache = wrapper.raw;
}
if (cache instanceof Cache.Parameterized, ?>) {
return new ParameterizedUsedCacheImpl<>(
(Cache.Parameterized)cache,
operator
);
}
return new UsedCacheImpl<>(cache, operator);
}
public static UsedCache export(UsedCache cache) {
if (cache != null) {
Set> disabledCaches = LOADING_CACHES_LOCAL.get();
if (disabledCaches != null && disabledCaches.contains(cache)) {
return null;
}
}
return cache;
}
public static Cache unwrap(Cache cache) {
if (cache instanceof UsedCache, ?>) {
UsedCacheImpl wrapper = (UsedCacheImpl) cache;
return wrapper.raw;
}
return cache;
}
@Override
public @NotNull ImmutableType type() {
return raw.type();
}
@Override
public @Nullable ImmutableProp prop() {
return raw.prop();
}
@NotNull
@Override
public Map getAll(@NotNull Collection keys, @NotNull CacheEnvironment env) {
return loading(() -> {
Map valueMap = raw.getAll(keys, env);
for (V value : valueMap.values()) {
validateResult(value);
}
return valueMap;
});
}
@SuppressWarnings("unchecked")
@Override
public void delete(@NotNull K key) {
if (operator == null || CacheOperator.isSuspending()) {
raw.delete(key);
} else {
operator.delete((UsedCache