概念了解
对于java连接redis后使用的API接口
可以选用原生的如redisTemplate.opsForValue() redisTemplate.opsForHash() 等
也可以选择经一层包装的boundValueOps() boundHashOps()
boundListOps()、boundSetOps()、boundZSetOps()
哪个好用 用哪个就行 实际上都没有关系
如果 两者是实现的效果是相同的
建议还是使用以redisTemplate.opsForValue() 等为主
因为更加贴近redis的原生语言 方便在 redis-cli 更好的回忆起相关的命令
实战了解
@Slf4j
@SpringBootTest
@RunWith(SpringRunner.class)
public class RedisBoundJunit {
@Autowired
StringRedisTemplate redisTemplate;
/**
* 类似于 redisTemplate.opsForValue()
*/
@Test
public void boundValueOpsTest() {
//针对于某个key
BoundValueOperations<String, String> boundValueOperations = redisTemplate.boundValueOps("String");
boundValueOperations.set("string");
System.out.println(boundValueOperations.get());
}
/**
* 类似于 redisTemplate.opsForHash()
*/
@Test
public void boundHashOperationsTest() {
//针对于某个key
BoundHashOperations<String, String, Object> boundHashOperations = redisTemplate.boundHashOps("hash");
boundHashOperations.put("hashKey1","hashValue1");
boundHashOperations.put("hashKey2","hashValue2");
boundHashOperations.put("hashKey3","hashValue3");
System.out.println(boundHashOperations.get("hashKey1"));
System.out.println(boundHashOperations.get("hashKey2"));
System.out.println(boundHashOperations.get("hashKey3"));
System.out.println("keys"+boundHashOperations.keys());
System.out.println("values"+boundHashOperations.values());
System.out.println("entries"+boundHashOperations.entries());
}
/**
* redisTemplate的删除操作非常方便
*/
@Test
public void redisTemplate(){
redisTemplate.delete("hash");
}
}
项目连接
请配合项目代码食用效果更佳:
项目地址:
https://github.com/hesuijin/hesuijin-study-project
Git下载地址:
https://github.com.cnpmjs.org/hesuijin/hesuijin-study-project.git
redis-module项目模块下 单元测试
网友评论