Using RedisTemplate to Manage String and Hash Data in Spring

This article demonstrates how to encapsulate common RedisTemplate operations—such as deleting single or multiple keys, setting expiration, checking existence, and performing String and Hash CRUD actions—by providing concrete Java code examples and step‑by‑step method implementations.

Coder Trainee
Coder Trainee
Coder Trainee
Using RedisTemplate to Manage String and Hash Data in Spring

Encapsulating Custom Operations

Delete a single key: redisTemplate.delete(key) Delete multiple keys: redisTemplate.delete(keys) where keys is a var‑args parameter, e.g., public void deleteByKey(String ...keys) Set key expiration: redisTemplate.expire(key, time, TimeUnit.MINUTES) (parameters: key, duration, unit)

Get remaining TTL of a key: redisTemplate.getExpire(key) Check if a key exists:

redisTemplate.hasKey(key)

String Type Operations

Add cache entries

// Simple set
redisTemplate.boundValueOps("StringKey").set("StringValue");
// Set with expiration (1 minute)
redisTemplate.boundValueOps("StringKey").set("StringValue", 1, TimeUnit.MINUTES);

// Using BoundValueOperations
BoundValueOperations stringKey = redisTemplate.boundValueOps("StringKey");
stringKey.set("StringValue");
stringKey.set("StringValue", 1, TimeUnit.SECOND);

// Using ValueOperations
ValueOperations ops = redisTemplate.opsForValue();
ops.set("StringKey", "StringValue");
ops.set("StringValue", "StringValue", 1, TimeUnit.SECOND);

Delete a cache key

Boolean result = redisTemplate.delete(key);

Increment a numeric value

redisTemplate.boundValueOps("key").increment(4L); // increase by 4
redisTemplate.boundValueOps("key").increment(-4L); // decrease by 4

Hash Type Data Operations

Add a hash entry

redisTemplate.boundHashOps("HashKey").put("SmallKey", "HashValue");

BoundHashOperations hashKey = redisTemplate.boundHashOps("HashKey");
hashKey.put("SmallKey", "HashValue");

HashOperations hashOps = redisTemplate.opsForHash();
hashOps.put("HashKey", "SmallKey", "HashValue");

Set expiration for a hash key

redisTemplate.boundValueOps("HashKey").expire(1, TimeUnit.SECOND);
redisTemplate.expire("HashKey", 1, TimeUnit.SECOND);

Insert a Map into a hash

HashMap<string, string> hashMap = new HashMap<>();
redisTemplate.boundHashOps("HashKey").putAll(hashMap);

Retrieve all field keys from a hash

Set keys1 = redisTemplate.boundHashOps("HashKey").keys();

BoundHashOperations hashKey = redisTemplate.boundHashOps("HashKey");
Set keys2 = hashKey.keys();

HashOperations hashOps = redisTemplate.opsForHash();
Set keys3 = hashOps.keys("HashKey");
Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

JavaCacheRedisSpringStringHashRedisTemplate
Coder Trainee
Written by

Coder Trainee

Experienced in Java and Python, we share and learn together. For submissions or collaborations, DM us.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.