功能介绍
该模块基于org.springframework.boot:spring-boot-starter-data-redis
进行扩展,对Redis常见的五种数据类型(string,list,set,zset,hash
)和key
(主体为key的所有方法,如:expire、setNx和delete等)的方法进行了二次封装,为应用提供操作Redis的能力。
优势
- 简化配置。代码层无须重复对
Redis
进行初始化配置,简单引用依赖后,使用注解@resource
即可获得各类数据类型操作能力。 - 命名统一。对
Redis
中的key
和hashKey
单独实现序列化和反序列化方式,通过配置spring.redis.prefix.key
后,即可对应用中的key
实现统一化前缀,便于后期归类、搜索和批量删除。 - 泛型支持。操作方法中所有
value
均使用泛型定义,通过在使用时初始化数据类型,避免在使用过程中设置/取出不同值,进而造成序列化失败。 - 多数据源支持。
模块引用说明
maven配置
<dependency>
<groupId>com.kingengine.kems.sdk</groupId>
<artifactId>kems-sdk-redis</artifactId>
</dependency>
yml配置
redis:
selected: d0
sources:
- id: d0
host: 10.202.61.11
port: 6379
password: Kingdom_123
database: 0
prefix:
enable: true
key: kems-test
配置说明参阅
kems-sdk-redis/src/main/resources/META-INF/spring-configuration-metadata.json
简单示例
Redis配置
redis:
selected: d0
sources:
#默认库
- id: d0
host: 10.202.61.11
port: 6379
password: Kingdom_123
database: 0
prefix:
enable: true
key: kems-test
#共享库
- id: d2
host: 10.202.61.11
port: 6379
password: Kingdom_123
database: 2
prefix:
enable: true
key: kems-common
代码演示
多数据源使用
在需要切换到特定数据源的类class
或方法method
上添加注解@RedisSource()
。如果未配置,或者配置的数据源非法,则会使用默认数据源完成后续操作!
@Resource
private RedisStringService<String> userRedisStringService;
/**
* 使用数据源d0
*/
@RedisSource("d0")
public String getD0() {
return userRedisStringService.get("hello");
}
/**
* 使用数据源d2
*/
@RedisSource("d2")
public void setD2(String s) {
userRedisStringService.set("hello", s);
}
锁操作
@Resource
private RedisKeyService redisKeyService;
@Test
void lock() {
String uuid = UUID.randomUUID().toString();
if (!redisKeyService.lock(LOCK_KEY, uuid, 10)) {
return;
}
try {
//业务逻辑
} finally {
redisKeyService.releaseLock(LOCK_KEY, uuid);
}
}
key相关
@Resource
private RedisKeyService redisKeyService;
@Test
public void expire() {
stringService.set("s-hello", "world");
Assert.assertEquals(true, redisKeyService.expire("s-hello", 10, TimeUnit.SECONDS));
redisKeyService.delete("s-hello");
}
@Test
public void getExpire() {
stringService.set("s-hello", "world");
redisKeyService.expire("s-hello", 3, TimeUnit.SECONDS);
Assert.assertNotNull(stringService.get("s-hello"));
try {
Thread.sleep(4 * 1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
Assert.assertNull(stringService.get("s-hello"));
redisKeyService.delete("s-hello");
}
@Test
public void countKeyPattern() {
stringService.set("s-hello-3-1", "tom");
stringService.set("s-hello-3-2", "jam");
Assert.assertEquals(2, redisKeyService.countKeyPattern("s-hello-3-*"));
redisKeyService.delete("s-hello-3-1");
redisKeyService.delete("s-hello-3-2");
}
@Test
public void lockWithoutExpire() {
Assert.assertTrue(redisKeyService.lock("lock-test", "lock"));
Assert.assertFalse(redisKeyService.lock("lock-test", "lock"));
Assert.assertTrue(redisKeyService.releaseLock("lock-test"));
}
@Test
public void lock() {
Assert.assertTrue(redisKeyService.lock("lock-test", "lock", 10));
Assert.assertFalse(redisKeyService.lock("lock-test", "lock", 10));
Assert.assertFalse(redisKeyService.releaseLock("lock-test", "lock1"));
Assert.assertTrue(redisKeyService.releaseLock("lock-test", "lock"));
}
@Test
public void delete() {
stringService.set("s-hello-4", "tom");
Assert.assertEquals("tom", stringService.get("s-hello-4"));
redisKeyService.delete("s-hello-4");
Assert.assertNull(stringService.get("s-hello-4"));
}
@Test
public void scan() {
stringService.set("s-hello-5-1", "tom");
stringService.set("s-hello-5-2", "jam");
Set<String> set = redisKeyService.scan("s-hello-5*", 1000L);
Assert.assertEquals(2, set.size());
redisKeyService.delete("s-hello-5-1");
redisKeyService.delete("s-hello-5-2");
}
string
相关
@Resource
private RedisStringService<String> redisStringService;
@Test
public void set() {
User user = new User();
user.setAge(12);
user.setName("tom");
userRedisStringService.set("s-1", user);
Assert.assertNotNull(userRedisStringService.get("s-1"));
Assert.assertEquals(12, (long) userRedisStringService.get("s-1").getAge());
redisKeyService.delete("s-1");
}
@Test
public void get() {
User user = new User();
user.setAge(12);
user.setName("tom");
userRedisStringService.set("s-1", user);
Assert.assertNotNull(userRedisStringService.get("s-1"));
Assert.assertEquals(12, (long) userRedisStringService.get("s-1").getAge());
redisKeyService.delete("s-1");
}
@Test
public void increment() {
loginCntRedisService.increment("login-cnt");
Assert.assertEquals(1, (long) loginCntRedisService.get("login-cnt"));
loginCntRedisService.increment("login-cnt", 100);
Assert.assertEquals(101, (long) loginCntRedisService.get("login-cnt"));
redisKeyService.delete("login-cnt");
}
@Test
public void decrement() {
loginCntRedisService.increment("login-cnt-1", 100);
loginCntRedisService.decrement("login-cnt-1");
Assert.assertEquals(99, (long) loginCntRedisService.get("login-cnt-1"));
redisKeyService.delete("login-cnt-1");
}
list
相关
@Resource
private RedisListService<User> redisListService;
@Test
public void rpush() {
User user = new User();
user.setAge(12);
user.setName("hello");
userRedisListService.rPush("list-user", user);
user = userRedisListService.rPop("list-user");
Assert.assertNotNull(user);
Assert.assertEquals("hello", user.getName());
redisKeyService.delete("list-user");
}
@Test
public void range() {
User user = new User();
user.setAge(12);
user.setName("hello");
userRedisListService.rPush("list-user", user);
user = new User();
user.setAge(13);
user.setName("hello1");
userRedisListService.rPush("list-user", user);
List<User> list = userRedisListService.range("list-user", 0, -1);
Assert.assertNotNull(list);
Assert.assertEquals(2, list.size());
Assert.assertEquals("hello", list.get(0).getName());
redisKeyService.delete("list-user");
}
set
相关
@Resource
private RedisSetService<User> redisSetService;
@Test
public void len() {
Bill bill = new Bill();
bill.setId(1L);
bill.setOrderTime(new Date());
redisSetService.add("bills", bill);
bill = new Bill();
bill.setId(2L);
bill.setOrderTime(new Date(System.currentTimeMillis() - 1000 * 60 * 60));
redisSetService.add("bills", bill);
Assert.assertEquals(2, redisSetService.len("bills"));
redisKeyService.delete("bills");
}
@Test
public void add() {
Bill bill = new Bill();
bill.setId(3L);
bill.setOrderTime(new Date());
Assert.assertEquals(1, redisSetService.add("bills", bill));
redisKeyService.delete("bills");
}
@Test
public void members() {
Bill bill = new Bill();
bill.setId(3L);
bill.setOrderTime(new Date());
redisSetService.add("bills", bill);
Set<Bill> bills = new HashSet<>();
bills.add(bill);
Assert.assertNotNull(bills);
Assert.assertTrue(bills.contains(bill));
redisKeyService.delete("bills");
}
zset
相关
@Resource
private RedisZSetService<User> redisZSetService;
@Test
public void len() {
Bill bill = new Bill();
bill.setId(1L);
bill.setOrderTime(new Date(System.currentTimeMillis()));
redisZSetService.add("bills", System.currentTimeMillis(), bill);
bill = new Bill();
bill.setId(2L);
bill.setOrderTime(new Date(System.currentTimeMillis() - 1000 * 60 * 60));
redisZSetService.add("bills", System.currentTimeMillis() - 1000 * 60 * 60, bill);
Assert.assertEquals(2, redisZSetService.len("bills"));
redisKeyService.delete("bills");
}
@Test
public void add() {
Bill bill = new Bill();
bill.setId(1L);
bill.setOrderTime(new Date());
redisZSetService.add("bills", System.currentTimeMillis(), bill);
Assert.assertEquals(1, redisZSetService.len("bills"));
redisKeyService.delete("bills");
}
hash
相关
@Resource
private RedisHashService<User> redisHashService;
@Test
public void setUser() {
User user = new User();
user.setAge(1);
user.setName("hello");
userRedisHashService.put("user", "hello", user);
User user1 = userRedisHashService.get("user", "hello");
Assert.assertNotNull(user1);
Assert.assertEquals(user1.getAge(), user.getAge());
}
@Test
public void get() {
redisHashService.put("routes", "user", "/user");
Object o = redisHashService.get("routes", "user");
Assert.assertEquals("/user", o.toString());
redisKeyService.delete("routes");
}
@Test
public void put() {
redisHashService.put("routes", "user", "/user");
Object o = redisHashService.get("routes", "user");
Assert.assertEquals("/user", o.toString());
redisKeyService.delete("routes");
}
@Test
public void hasKey() {
redisHashService.put("routes", "hashKey", "/user");
Assert.assertTrue(redisHashService.hasKey("routes", "hashKey"));
redisKeyService.delete("routes");
}
@Test
public void entries() {
redisKeyService.delete("routes");
redisHashService.put("routes", "user", "/user");
redisHashService.put("routes", "hashKey", "/user");
Map<Object, Object> map = redisHashService.entries("routes");
Assert.assertNotNull(map);
Assert.assertEquals(2, map.size());
redisKeyService.delete("routes");
}
@Test
public void delete() {
redisHashService.put("routes", "delete", "/user");
Assert.assertTrue(redisHashService.hasKey("routes", "delete"));
redisHashService.delete("routes", "delete");
Assert.assertFalse(redisHashService.hasKey("routes", "delete"));
}
@Test
public void increment() {
redisKeyService.delete("routes");
redisHashService.increment("routes", "cnt", 1);
Object o = redisHashService.get("routes", "cnt");
Assert.assertEquals(1, o);
redisKeyService.delete("routes");
}