Springboot中使用StringRedisTemplate访问Redis

阅读更多

Springboot2默认情况下使用lettuce框架访问Redis,只需要在pom.xml文件添加以下依赖即可:


	org.springframework.boot
	spring-boot-starter-data-redis


	org.apache.commons
	commons-pool2

 

在需要访问Redis的类中注入StringRedisTemplate实例:

@Autowired
StringRedisTemplate stringRedisTemplate;

 

String(字符串):

private void testValue(){
	ValueOperations value = stringRedisTemplate.opsForValue();
	value.getOperations().delete("aaa"); //删除
	value.getOperations().delete("bbb");
	
	value.set("aaa", "123"); //set
	System.out.println(value.setIfAbsent("aaa", "123")); //没有才set
	
	System.out.println(value.size("aaa")); //长度
	System.out.println(value.get("aaa")); //获取值
	
	value.append("aaa", "456"); //追加
	System.out.println(value.get("aaa"));
	
	value.increment("bbb", 1); //数值自增
	System.out.println(value.get("bbb"));
	
	value.multiGet(Arrays.asList("aaa", "bbb", "ccc"))
		.stream().forEach(System.out::println);
}

 

Hash(散列):

private void testHash(){
	HashOperations hash = stringRedisTemplate.opsForHash();
	hash.getOperations().delete("hash1");
	
	hash.put("hash1", "aaa", "111"); //put
	hash.put("hash1", "bbb", "222");
	hash.put("hash1", "ccc", "333");
	
	System.out.println(hash.size("hash1")); //长度
	
	hash.entries("hash1").forEach((k,v) -> { //显示所有的key和value
		System.out.println(k + "=" + v);
	});
	
	hash.keys("hash1").stream().forEach(System.out::println); //显示所有的key
	hash.values("hash1").stream().forEach(System.out::println); //显示所有的value
	
	System.out.println(hash.putIfAbsent("hash1", "aaa", "aaa")); //没有key才put
	
	hash.increment("hash1", "count", 1); //数值自增
	System.out.println(hash.get("hash1", "count"));
	
	hash.increment("hash1", "count", 1);
	System.out.println(hash.get("hash1", "count"));
	
	hash.increment("hash1", "count", -1);
	System.out.println(hash.get("hash1", "count"));
	
	System.out.println(hash.hasKey("hash1", "amount")); //判断key是否存在
	System.out.println(hash.get("hash1", "count"));
	hash.delete("hash1", "count"); //删除
}

 

Set(集合):

private void testSet(){
	SetOperations set = stringRedisTemplate.opsForSet();
	set.getOperations().delete("set1");
	set.getOperations().delete("set2");
	
	set.add("set1", "111", "222", "333", "111");
	set.add("set2", "222", "333", "444");
	
	System.out.println(set.size("set1")); //长度
	
	System.out.println(set.members("set1")); //获取所有值
	System.out.println(set.members("set2"));
	
	System.out.println(set.difference("set1", "set2")); //从前者取得与后者不一样的元素
	System.out.println(set.intersect("set1", "set2")); //交集
	System.out.println(set.union("set1", "set2")); //并集
	set.remove("set1", "111"); //删除一个元素
	System.out.println(set.pop("set1")); //弹出一个元素
	System.out.println(set.randomMember("set2")); //随机取一个元素
	System.out.println(set.isMember("set2", "444")); //给定元素是否是成员
	set.move("set2", "444", "set1"); //移动
	System.out.println(set.members("set1"));
}

 

List(列表):

private void testList() {
	ListOperations list = stringRedisTemplate.opsForList();
	list.getOperations().delete("list"); //清空
	
	list.leftPush("list", "111"); //push 放到list中
	list.leftPushIfPresent("list", "222");
	list.rightPush("list", "333");
	list.rightPushIfPresent("list", "444");
	
	System.out.println(list.index("list", 0));
	System.out.println(list.range("list", 0, -1));
	list.trim("list", 0, 2); //截取
	System.out.println(list.range("list", 0, -1));
	
	System.out.println(list.leftPop("list")); //pop 取出消费掉
	System.out.println(list.rightPop("list"));
}

 

Zset(有序集合):

private void testZSet(){
	ZSetOperations zset = stringRedisTemplate.opsForZSet();
	zset.getOperations().delete("zset1");
	zset.getOperations().delete("zset2");
	
	zset.add("zset1", "aaa", 1);
	zset.add("zset1", "bbb", 1);
	zset.add("zset1", "ccc", 1);
	
	zset.add("zset2", "bbb", 1);
	zset.add("zset2", "ccc", 1);
	zset.add("zset2", "ddd", 1);
	
	System.out.println(zset.size("zset1")); //长度
	System.out.println(zset.range("zset1", 0, -1)); //取所有元素
	
	zset.incrementScore("zset1", "bbb", 1); //增加分数score
	zset.incrementScore("zset1", "aaa", 2);
	
	System.out.println(zset.range("zset1", 0, -1)); //分数递增排序
	zset.rangeWithScores("zset1", 0, -1).stream().forEach(t -> {
		System.out.println(t.getValue() + "-" + t.getScore());
	});
	
	System.out.println(zset.reverseRange("zset1", 0, -1)); //分数递减排序
	zset.reverseRangeWithScores("zset1", 0, -1).stream().forEach(t -> {
		System.out.println(t.getValue() + "-" + t.getScore());
	});
	
	System.out.println(zset.rank("zset1", "ccc")); //分数递增排序,取元素的索引
	System.out.println(zset.reverseRank("zset1", "ccc")); //分数递减排序,取元素的索引
	
	System.out.println(zset.score("zset1", "bbb")); //取元素的分数
	
	zset.remove("zset2", "bbb"); //删除元素
	System.out.println(zset.range("zset2", 0, -1));
}

      

你可能感兴趣的:(Springboot中使用StringRedisTemplate访问Redis)