com.clickhouse.data.ClickHouseCache Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of clickhouse-data Show documentation
Show all versions of clickhouse-data Show documentation
Data processing utilities for ClickHouse
package com.clickhouse.data;
import java.util.function.Function;
import com.clickhouse.data.cache.CaffeineCache;
import com.clickhouse.data.cache.JdkLruCache;
/**
* Wrapper interface depicts essential methods required by a client-side cache.
*/
public interface ClickHouseCache {
/**
* Default cache size.
*/
static final int DEFAULT_CACHE_SIZE = 50;
/**
* Creates a cache with specific capacity and load function.
*
* @param type of key
* @param type of value
* @param capacity capacity of the cache, zero or negative number will be
* treated as {@link #DEFAULT_CACHE_SIZE}
* @param expireSeconds seconds to expire after access
* @param loadFunc non-null load function
* @return cache
*/
static ClickHouseCache create(int capacity, long expireSeconds, Function loadFunc) {
ClickHouseCache cache;
try {
cache = CaffeineCache.create(capacity, expireSeconds, loadFunc);
} catch (Throwable e) {
// ignore
cache = JdkLruCache.create(capacity, loadFunc);
}
return cache;
}
/**
* Gets value from cache if it exists.
*
* @param key key, in genernal should NOT be null
* @return non-null value in general
*/
V get(K key);
/**
* Gets inner cache object to gain more access.
*
* @param type of the cache
* @param clazz non-null class of the cache
* @return inner cache object
* @throws NullPointerException when {@code clazz} is null
*/
T unwrap(Class clazz);
}