Skip to main content

Data Writes

Execute Redis write commands through dbVisitor. SET and DEL return update counts, while commands such as INCR return query results; choose the API by the reply form. See Query Operations for reading stored data.

Writing and Deleting Keys

JdbcTemplate jdbc = new JdbcTemplate(dataSource);
jdbc.executeUpdate("SET ? ?", new Object[] { "user:1001:name", "mali" });
jdbc.executeUpdate("DEL ?", "user:1001:name");

SET replaces the existing value; DEL removes the key. Both commands return update counts through executeUpdate. Use HSET to write a Hash field:

jdbc.executeUpdate("HSET ? ? ?", new Object[] { "user:1001", "name", "mali" });

See Query Operations for reading and mapping HGETALL results.

Writing Through Mappers

@SimpleMapper
public interface NameWriteMapper {
@Insert("SET #{key} #{name}")
int save(@Param("key") String key, @Param("name") String name);

@Delete("DEL #{key}")
int remove(@Param("key") String key);
}
Session session = new Configuration().newSession(dataSource);
NameWriteMapper mapper = session.createMapper(NameWriteMapper.class);
mapper.save("user:1001:name", "mali");

File-Based Writes with BaseMapper

BaseMapper's statement execution methods can call the save and remove commands above. It does not generate Redis write commands from an entity; configure those commands explicitly in the Mapper file.

Reading Values Returned by Writes

INCR changes a value but returns the new number. Use a query method to retrieve it:

Long value = jdbc.queryForLong("INCR ?", "order:sequence");

In a Mapper, this command uses @Query, not @Update. Choose the API by the adapter's return form, not whether the command changes data. The same applies to ZADD and LPOP; see Command Syntax.

Sharing Data Across Sessions and APIs

Method annotations, Mapper files, and session.jdbc() can access the same Redis keys. Sessions connected to the same Redis database can also read each other's writes:

Configuration configuration = new Configuration();
try (Session writer = configuration.newSession(dataSource);
Session reader = configuration.newSession(dataSource)) {
writer.createMapper(NameWriteMapper.class).save("user:1001:name", "mali");
String name = reader.jdbc().queryForString("GET ?", "user:1001:name");
}

These calls still execute explicit Redis commands. session.lambda() and entity-generated BaseMapper CRUD are unavailable; BaseMapper's Mapper-file command methods remain usable.

Overwrite Behavior and Errors

Invalid commands report errors, but SET overwrites an existing key rather than reporting a duplicate-key error. Reading a missing key is not an error either; see Query Errors.

Multi-Key Writes and Transactions

Use MSET for multi-key writes. It is one Redis command, not a JDBC batch. Separate SET calls do not automatically form a transaction; see Transaction Support.