com.github.chengyuxing.sql.plugins.QueryCacheManager Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of rabbit-sql Show documentation
Show all versions of rabbit-sql Show documentation
Light wrapper of JDBC, support ddl, dml, query, plsql/procedure/function, transaction and manage sql
file.
package com.github.chengyuxing.sql.plugins;
import com.github.chengyuxing.common.DataRow;
import com.github.chengyuxing.common.utils.StringUtil;
import org.jetbrains.annotations.NotNull;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Stream;
/**
* Query cache manager.
*/
public interface QueryCacheManager {
/**
* Unique key for the cache.
* It is necessary to ensure uniqueness to avoid cache clutter.
*
* @param sql sql name or sql string
* @param args args
* @return the unique key
*/
default @NotNull String uniqueKey(@NotNull String sql, Map args) {
String argsStr = Objects.nonNull(args) ? "@" + StringUtil.hash(args.toString(), "MD5") : "";
if (sql.startsWith("&")) {
return sql + argsStr;
}
return StringUtil.hash(sql, "MD5") + argsStr;
}
/**
* Get cache.
* Notice: Do not return an empty stream if the cache does not exist.
* To trigger the execution of a database query, null must be returned.
*
* @param uniqueKey unique cache key generated by {@link #uniqueKey(String, Map)}
* @return cache or null
*/
Stream get(String uniqueKey);
/**
* Put cache.
*
* @param uniqueKey unique cache key generated by {@link #uniqueKey(String, Map)}
* @param value query result
*/
void put(@NotNull String uniqueKey, List value);
/**
* Check the sql matches the conditions or not, if matches,
* cache operations {@link #put(String, List) put} and {@link #get(String) get} will be enabling.
*
* @param sql sql name or sql string
* @param args args
* @return true or false
*/
boolean isAvailable(@NotNull String sql, Map args);
}