0
点赞
收藏
分享

微信扫一扫

redis支持的5种数据类型的存储代码测试

千白莫 2022-03-10 阅读 57

redis一共支持存储5种数据类型:String、list、set、zset、hash。

public class RedisTest {

	@Autowired
	private ValueOperations valueOperations;
	@Autowired
	private ListOperations listOperations;
	@Autowired
	private SetOperations setOperations;
	@Autowired
	private ZSetOperations zSetOperations;
	@Autowired
	private HashOperations hashOperations;

	@Test
	public void testSaveValue(){
		//存储字符串
		valueOperations.set("0","0");
		System.out.println(valueOperations.get("0"));

		//存储list
		//从list右侧增加值
		listOperations.rightPush("key0","0");
		listOperations.rightPush("key0","value1");
		//按照key和对应list的索引对值进行修改
		listOperations.set("key0",0,"value0");
		//从list的左侧插入值
		listOperations.leftPush("key0","value2");
		//通过key和索引获取值
		System.out.println(listOperations.index("key0",0));
		System.out.println(listOperations.index("key0",1));
		//获取最后一个元素并删除
		Object key0 = listOperations.rightPop("key0");
		System.out.println(key0);

		//存储set
		setOperations.add("a","0","1");
		setOperations.add("a","2");
		System.out.println(setOperations.members("a"));


		//存储Zset,score值可以用来排序
		zSetOperations.add("b","0",3);
		zSetOperations.add("b","1",2);
		Set range = zSetOperations.range("b", 0, zSetOperations.size("b"));
		System.out.println(range.size());

		//存储hash(对象),hk为对象的属性名,hv为对象的属性值
		hashOperations.put("s","name","李雷");
		hashOperations.put("s","age",20);
		hashOperations.put("s","score",100);
		System.out.println(hashOperations.get("s","name"));
		System.out.println(hashOperations.get("s","age"));
		List values = hashOperations.values("s");
		System.out.println(values);
		Map entries = hashOperations.entries("s");
		System.out.println(entries.get("age"));
	}

}

举报

相关推荐

0 条评论