RedisTemplate
and StringRedisTemplate
cooperate with Redisson
,use it easily,provide the more friendly and perfect API for you!
The biggest advantage is the omission of the redisson.yaml
config file
This project is devolped based on xsxgit/redis-spring-boot-starter(Standing on the shoulders of giants)
<dependency>
<groupId>com.naah69</groupId>
<artifactId>spring-boot-starter-redisson</artifactId>
<version>1.0.0</version>
</dependency>
The code of this demo is in spring-boot-starter-redisson-demo
- Maven Dependency:
<dependency>
<groupId>com.naah69</groupId>
<artifactId>spring-boot-starter-redisson</artifactId>
<version>1.0.0</version>
</dependency>
- Redis Configuration:
yml:
server:
port: 6969
spring:
profiles:
active: local
---
spring:
profiles: local
redis:
redisson:
enable: true
singleServerConfig:
idleConnectionTimeout: 10000
pingTimeout: 1000
connectTimeout: 10000
timeout: 3000
retryAttempts: 3
retryInterval: 1500
failedSlaveReconnectionInterval: 3000
failedSlaveCheckInterval: 3
# password: null
subscriptionsPerConnection: 5
clientName: "redisson-demo"
address: "redis://127.0.0.1:6379"
subscriptionConnectionMinimumIdleSize: 1
subscriptionConnectionPoolSize: 50
connectionMinimumIdleSize: 32
connectionPoolSize: 64
database: 0
threads: 0
nettyThreads: 0
codec:
class: "org.redisson.codec.JsonJacksonCodec"
transportMode: "NIO"
---
spring:
profiles: cluster
redis:
redisson:
enable: true
clusterServersConfig:
idleConnectionTimeout: 10000
pingTimeout: 1000
connectTimeout: 10000
timeout: 3000
retryAttempts: 3
retryInterval: 1500
failedSlaveReconnectionInterval: 3000
failedSlaveCheckInterval: 3
password: 'password'
subscriptionsPerConnection: 5
clientName: "redisson-demo"
loadBalancer:
class: "org.redisson.connection.balancer.RoundRobinLoadBalancer"
slaveSubscriptionConnectionMinimumIdleSize: 1
slaveSubscriptionConnectionPoolSize: 50
slaveConnectionMinimumIdleSize: 32
slaveConnectionPoolSize: 64
masterConnectionMinimumIdleSize: 32
masterConnectionPoolSize: 64
readMode: "SLAVE"
nodeAddresses:
- "redis://127.0.0.1:7110"
- "redis://127.0.0.1:7111"
- "redis://127.0.0.1:7112"
- "redis://127.0.0.1:7113"
scanInterval: 1000
threads: 0
nettyThreads: 0
codec:
class: "org.redisson.codec.JsonJacksonCodec"
transportMode: "NIO"
The following order is the order that is recommended
- RedisTemplate
@Autowired
private StringRedisTemplate redisTemplate;
@Test
public void test() {
//get client
ValueOperations<String, String> redisClient = redisTemplate.opsForValue();
//key
String key = "RedisTemplateTest";
//delete
redisTemplate.delete(key);
//value
long current = System.currentTimeMillis();
String value = current + "";
//set
redisClient.set(key, value);
//verify
Assert.assertEquals(redisClient.get(key), value);
}
- RedisUtils
@Test
public void test() {
//get client
StringHandler redisClient = RedisUtil.getStringHandler();
//key
String key = "RedisUtilsTest";
//delete
redisClient.remove(key);
//value
long current = System.currentTimeMillis();
String value = current + "";
//set
redisClient.set(key, value);
//verify
Assert.assertEquals(redisClient.get(key), value);
}
- RedissonClient
@Autowired
private RedissonClient redissonClient;
@Test
public void test() {
//key
String key = "RedissonClientTest";
//get client
RBucket<String> redisClient = redissonClient.getBucket(key);
//delete
redisClient.delete();
//value
long current = System.currentTimeMillis();
String value = current + "";
//set
redisClient.set(value);
//verify
Assert.assertEquals(redisClient.get(), value);
}
get database instance:
// get default database instance(DB)
DBHandler dbHandler = RedisUtil.getDBHandler();
...
// get the database instance that is second index(DB)
DBHandler dbHandler = RedisUtil.getDBHandler(2);
...
Instance | Data Type | Get Way |
---|---|---|
NumberHandler | Number | RedisUtil.getNumberHandler() RedisUtil.getNumberHandler(dbIndex) |
StringHandler | String | RedisUtil.getStringHandler() RedisUtil.getStringHandler(dbIndex) |
HashHandler | Hash | RedisUtil.getHashHandler() RedisUtil.getHashHandler(dbIndex) |
SetHandler | Set | RedisUtil.getSetHandler() RedisUtil.getSetHandler(dbIndex) |
ZsetHandler | Zset | RedisUtil.getZsetHandler() RedisUtil.getZsetHandler(dbIndex) |
HyperLogLogHandler | HyperLogLog | RedisUtil.getHyperLogLogHandler() RedisUtil.getHyperLogLogHandler(dbIndex) |
BitmapHandler | Bitmap | RedisUtil.getBitmapHandler() RedisUtil.getBitmapHandler(dbIndex) |
GeoHandler | Geo | RedisUtil.getGeoHandler() RedisUtil.getGeoHandler(dbIndex) |
KeyHandler | Key | RedisUtil.getKeyHandler() RedisUtil.getKeyHandler(dbIndex) |
ScriptHandler | Lua Script | RedisUtil.getScriptHandler() RedisUtil.getScriptHandler(dbIndex) |
PubSubHandler | Pubsub | RedisUtil.getPubSubHandler() RedisUtil.getPubSubHandler(dbIndex) |
StreamHandler | Stream | RedisUtil.getStreamHandler() RedisUtil.getStreamHandler(dbIndex) RedisUtil.getStreamHandler(dbIndex, mapper) |
DBHandler | DB | RedisUtil.getDBHandler() RedisUtil.getDBHandler(dbIndex) |
SentinelHandler | Sentinel | RedisUtil.getSentinelHandler() RedisUtil.getSentinelHandler(dbIndex) |
ClusterHandler | Cluster | RedisUtil.getClusterHandler() |
CustomCommandHandler | CustomCommand | RedisUtil.getCustomCommandHandler() RedisUtil.getCustomCommandHandler(dbIndex) |
RedisLockHandler | Lock | RedisUtil.getRedisLockHandler() RedisUtil.getRedisLockHandler(dbIndex) |
TransactionHandler | Transaction | RedisUtil.getTransactionHandler() RedisUtil.getTransactionHandler(dbIndex) |
List execute = RedisUtil.getTransactionHandler(2).execute(handler -> {
// open monitoring
handler.watch("xx", "test");
// open transaction
handler.beginTransaction();
// get the string handler
StringHandler stringHandler = handler.getStringHandler();
// operate
stringHandler.set("xx", "hello");
stringHandler.append("xx", "world");
stringHandler.append("xx", "!");
// get the number handler
NumberHandler numberHandler = handler.getNumberHandler();
numberHandler.addLong("test", 100);
numberHandler.incrementLong("test");
numberHandler.incrementLong("test");
numberHandler.incrementLong("test");
// commit
return handler.commit();
});