In Spring Boot 3, operating Redis usually uses the tool classes provided by Spring Data Redis, such as RedisTemplate and StringRedisTemplate. The following is a detailed implementation of the Redis operation tool class, covering common functions.
Complete Redis tool class
The following tool classes can implement basic Redis operations such as strings, hashes, lists, collections, and ordered collections.
import ; import .*; import ; import .*; import ; @Component public class RedisUtils { @Autowired private RedisTemplate<String, Object> redisTemplate; /** * Set value */ public void set(String key, Object value, long timeout, TimeUnit timeUnit) { ().set(key, value, timeout, timeUnit); } /** * Get the value */ public Object get(String key) { return ().get(key); } /** * Delete key */ public void delete(String key) { (key); } /** * Batch deletion */ public void deleteKeys(String pattern) { Set<String> keys = (pattern); if (keys != null && !()) { (keys); } } /** * Set the expiration time of the key */ public boolean expire(String key, long timeout, TimeUnit unit) { return ((key, timeout, unit)); } /** * Get the remaining expiration time */ public long getExpire(String key) { return ((key)).orElse(0L); } /** * Check if the key exists */ public boolean hasKey(String key) { return ((key)); } /** * Add value */ public long increment(String key, long delta) { return (().increment(key, delta)).orElse(0L); } /** * Hash operation: Set value */ public void hSet(String key, String hashKey, Object value) { ().put(key, hashKey, value); } /** * Hash operation: get value */ public Object hGet(String key, String hashKey) { return ().get(key, hashKey); } /** * List operation: push left */ public void lPush(String key, Object value) { ().leftPush(key, value); } /** * List operation: pop up right */ public Object rPop(String key) { return ().rightPop(key); } /** * Collection operation: add elements */ public void sAdd(String key, Object... values) { ().add(key, values); } /** * Collection operation: Get all elements */ public Set<Object> sMembers(String key) { return ().members(key); } /** * Ordered collection operation: add elements */ public void zAdd(String key, Object value, double score) { ().add(key, value, score); } /** * Ordered collection operation: get elements in range */ public Set<Object> zRange(String key, long start, long end) { return ().range(key, start, end); } }
Use of tool classes
Set and get values
@Autowired private RedisUtils redisUtils; public void testSetValue() { ("key", "value", 10, ); Object value = ("key"); ("Value: " + value); }
Delete key
("key");
Hash operation
("hashKey", "field", "value"); Object value = ("hashKey", "field"); ("Hash Value: " + value);
List operation
("listKey", "value1"); Object value = ("listKey"); ("List Value: " + value);
Collection operations
("setKey", "value1", "value2"); Set<Object> members = ("setKey"); ("Set Members: " + members);
Ordered collection operations
("zSetKey", "value1", 1.0); Set<Object> range = ("zSetKey", 0, -1); ("ZSet Range: " + range);
Things to note
Using the appropriate serialization method By default, RedisTemplate uses JdkSerializationRedisSerializer, which can cause data to be stored in binary form in Redis. You can configure custom serializers such as StringRedisSerializer or Jackson2JsonRedisSerializer.
Sample configuration:
@Configuration public class RedisConfig { @Bean public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory connectionFactory) { RedisTemplate<String, Object> template = new RedisTemplate<>(); (connectionFactory); // Set the serializer for Key and Value (new StringRedisSerializer()); (new Jackson2JsonRedisSerializer<>()); // Set the serializer for Hash Key and Value (new StringRedisSerializer()); (new Jackson2JsonRedisSerializer<>()); (); return template; } }
Connection pooling and performance optimization
Use Redis connection pooling to improve performance, such as configuring connection pool size and maximum number of connections.
Avoid frequent use of keys commands, and SCAN is recommended for production environments.
Expiration strategy
Set a reasonable expiration time for temporary data to avoid excessive memory consumption.
Summarize
The tool class encapsulates common operations of Redis, which are easy to call and manage.
Choose the appropriate Redis data structure (String, Hash, List, Set, ZSet) according to your needs.
Configure the appropriate serialization method and connection pool to ensure performance and data readability.
This is the end of this article about the detailed explanation of springboot3 redis common operation tools. For more related content of springboot3 redis tool, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!