Jump to content

Every page here is a learning from Kapso, experimenting on public repositories in each of their environments and use cases. Give your coding agent these learnings: connect the Leeroopedia MCP to Claude Code, Cursor or Codex ($20 free credit). Powered by Leeroo · founders@leeroo.com

Implementation:APIJSON APIJSON Demo DemoSQLExecutor

From Leeroopedia
Knowledge Sources
Domains Database, Caching, Performance
Last Updated 2026-10-14 00:00 GMT

Overview

Concrete tool for executing SQL statements with Redis-based caching and multi-datasource connection pooling provided by the APIJSON framework.

Description

The DemoSQLExecutor class extends APIJSONSQLExecutor<Long> and handles:

  • Redis caching: Overrides getCache(), putCache(), and removeCache() to use a static RedisTemplate for query result caching with differentiated TTLs.
  • Multi-datasource connection routing: Overrides getConnection() to route to the correct Druid datasource bean based on the datasource field in the SQLConfig.
  • Non-relational execution: Overrides execute() to handle Milvus, Cassandra, and InfluxDB databases that don't use standard JDBC.

The Redis cache uses:

  • 60-second TTL for normal tables
  • 10-minute TTL for User/Privacy tables
  • 10-minute TTL for EXPLAIN/HEAD queries
  • Config tables are not cached

The connection pool supports three Druid datasources:

  • DRUID — primary database
  • DRUID-TEST — test database
  • DRUID-ONLINE — online/production database

Usage

  • Standard execution: Called by the APIJSON framework via parser.executeSQL() — no explicit invocation needed.
  • Cache configuration: Modify the Redis connection in the static block (host, port).
  • Datasource routing: Clients use the @datasource keyword in JSON requests to select a datasource.

Code Reference

Source Location

  • Repository: APIJSON-Demo
  • File: APIJSON-Java-Server/APIJSONBoot-MultiDataSource/src/main/java/apijson/demo/DemoSQLExecutor.java

Signature

public class DemoSQLExecutor extends APIJSONSQLExecutor<Long> {
    public static final RedisTemplate<String, String> REDIS_TEMPLATE;

    static {
        REDIS_TEMPLATE = new RedisTemplate<>();
        REDIS_TEMPLATE.setConnectionFactory(
            new JedisConnectionFactory(new RedisStandaloneConfiguration("127.0.0.1", 6379))
        );
        REDIS_TEMPLATE.setKeySerializer(new StringRedisSerializer());
        REDIS_TEMPLATE.setValueSerializer(new GenericToStringSerializer<>(Serializable.class));
        REDIS_TEMPLATE.afterPropertiesSet();
    }

    @Override
    public List<JSONObject> getCache(String sql, SQLConfig<Long, JSONObject, JSONArray> config);

    @Override
    public synchronized void putCache(String sql, List<JSONObject> list,
        SQLConfig<Long, JSONObject, JSONArray> config);

    @Override
    public synchronized void removeCache(String sql, SQLConfig<Long, JSONObject, JSONArray> config);

    @Override
    public Connection getConnection(SQLConfig<Long, JSONObject, JSONArray> config) throws Exception;

    @Override
    public JSONObject execute(@NotNull SQLConfig<Long, JSONObject, JSONArray> config,
        boolean unknownType) throws Exception;
}

Import

import apijson.demo.DemoSQLExecutor;

I/O Contract

Inputs

Name Type Required Description
config SQLConfig Yes SQL configuration with table, method, conditions, datasource
unknownType boolean No Whether result type is unknown
sql String Yes (cache) SQL string used as cache key

Outputs

Name Type Description
execute return JSONObject Query results as JSON object
getCache return List<JSONObject> Cached query results or null on miss
getConnection return Connection JDBC connection from appropriate pool

Usage Examples

Redis Cache TTL Logic

// In putCache:
String table = config != null && config.isMain() ? config.getTable() : null;
if (table != null && !DemoSQLConfig.CONFIG_TABLE_LIST.contains(table)) {
    if (config.isExplain() || RequestMethod.isHeadMethod(config.getMethod(), true)) {
        // EXPLAIN/HEAD: 10-minute TTL
        REDIS_TEMPLATE.opsForValue().set(sql, JSON.toJSONString(list), 10 * 60, TimeUnit.SECONDS);
    } else {
        // User/Privacy: 10-minute TTL; others: 60-second TTL
        REDIS_TEMPLATE.opsForValue().set(sql, JSON.toJSONString(list),
            USER_.equals(table) || PRIVACY_.equals(table) ? 10 * 60 : 60, TimeUnit.SECONDS);
    }
}

Datasource Routing

// In getConnection:
String datasource = config.getDatasource();  // from @datasource keyword
String key = datasource + "-" + config.getDatabase();

// Resolve to Spring bean:
// "DRUID"       → druidDataSource bean
// "DRUID-TEST"  → druidTestDataSource bean
// "DRUID-ONLINE"→ druidOnlineDataSource bean

Cache Invalidation on Delete

// In removeCache:
if (config.getMethod() == RequestMethod.DELETE) {
    REDIS_TEMPLATE.expire(sql, 60, TimeUnit.SECONDS);  // Delayed expiry prevents stampede
} else {
    REDIS_TEMPLATE.delete(sql);  // Immediate deletion for POST/PUT
}

Related Pages

Implements Principle

Requires Environment

Uses Heuristic

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment