SpringBoot学习4.2-Redis常用数据类型的操作

目录

1.操作接口

1.1.七种数据类型的操作接口

1.2.绑定键的操作类

2.常见数据类型的操作

2.1.字符串

2.2.散列

2.3链表


1.操作接口

1.1.七种数据类型的操作接口

public void operateType() {
	// 1.地理位置
	GeoOperations<Object, Object> opsForGeo = redisTemplate.opsForGeo();
	// 2.散列
	HashOperations<Object, Object, Object> opsForHash = redisTemplate.opsForHash();
	// 3.基数
	HyperLogLogOperations<Object, Object> opsForHyperLogLog = redisTemplate.opsForHyperLogLog();
	// 4.列表
	ListOperations<Object, Object> opsForList = redisTemplate.opsForList();
	// 5.集合
	SetOperations<Object, Object> opsForSet = redisTemplate.opsForSet();
	// 6.字符串
	ValueOperations<Object, Object> opsForValue = redisTemplate.opsForValue();
	// 7.有序集合
	ZSetOperations<Object, Object> opsForZSet = redisTemplate.opsForZSet();
}

1.2.绑定键的操作类

/**绑定键的操作类,传入key可以获取到对应的数据操作对象*/
public void boundType() {
	// 1.地理位置
	BoundGeoOperations<Object, Object> boundGeoOps = redisTemplate.boundGeoOps("key");
	// 2.散列
	BoundHashOperations<Object, Object, Object> boundHashOps = redisTemplate.boundHashOps("key");
	// 3.列表
	BoundListOperations<Object, Object> boundListOps = redisTemplate.boundListOps("key");
	// 4.集合
	BoundSetOperations<Object, Object> boundSetOps = redisTemplate.boundSetOps("key");
	// 5.字符串
	BoundValueOperations<Object, Object> boundValueOps = redisTemplate.boundValueOps("key");
	// 6.有序集合
	BoundZSetOperations<Object, Object> boundZSetOps= redisTemplate.boundZSetOps("key");
}

2.常见数据类型的操作

2.1.字符串

public void operateString() {
	ValueOperations<Object, Object> opsForValue = redisTemplate.opsForValue();
	opsForValue.set("str1", "增加一个字符串");
	opsForValue.set("int1", 1);
	BoundValueOperations<Object, Object> boundValueOps = redisTemplate.boundValueOps("int1");
	System.out.println((int) boundValueOps.get() + 2);
}

2.2.散列

public void operateHash() {
	// 散列操作接口
	HashOperations<Object, Object, Object> opsForHash = redisTemplate.opsForHash();
	Map<Object, Object> map = new HashMap<>();
	map.put("name", "zhangsan");
	map.put("age", 18);
	// 将map放入到HashOperations中
	opsForHash.putAll("hash2", map);
	// 获取指定map的指定key的数据:opsForHash.get(代表map的key, map中的key);
	Object age = opsForHash.get("hash2", "age");
	System.out.println("age = " + age);
	//给指定map中增加或修改某key对应的数据
	opsForHash.put("hash2", "sex", "female");
	Object sex  = opsForHash.get("hash2", "sex");
	System.out.println("sex = " + sex);
	// 将map绑定到散列绑定键操作接口
	BoundHashOperations<Object, Object, Object> hash2 = redisTemplate.boundHashOps("hash2");
	hash2.put("age", 25);
	System.out.println(hash2.get("age"));
}

结果:

age = 18
sex = female
age = 25

2.3.链表

2.3.1数据顺序

public void operateList1() {
	ListOperations<Object, Object> opsForList = redisTemplate.opsForList();
	// 从左边存入数据,存入后顺序为:v5,v4,v3,v2,v1
	opsForList.leftPushAll("list1", "v1","v2","v3","v4","v5");
	BoundListOperations<Object, Object> boundListOps = redisTemplate.boundListOps("list1");
	System.out.println("list1中元素个数 = " + boundListOps.size());
	
	Long listSize = boundListOps.size();
	for(int i = 0; i < listSize;i++) {
		//弹出左边的数据(移除)
		Object left = boundListOps.leftPop();
		System.out.println("弹出左边的数据:" + left);
	}
}

 结果:

list1中元素个数 = 5
弹出左边的数据:v5
弹出左边的数据:v4
弹出左边的数据:v3
弹出左边的数据:v2
弹出左边的数据:v1

 可以看出从左边存入后,链表中的数据顺序是:v5,v4,v3,v2,v1。

2.3.2移除指定数据,且指定移除个数

/**链表操作2*/
public void operateList2() {
	ListOperations<Object, Object> opsForList = redisTemplate.opsForList();
	opsForList.leftPushAll("list1", "v1","v2","v3","v4","v5","v2");
	BoundListOperations<Object, Object> boundListOps = redisTemplate.boundListOps("list1");
	
	// boundListOps.remove(移除个数, 移除的值),返回书记移除的个数
	Long lon = boundListOps.remove(3, "v2");// 移除3个值为“v2”的数据
	System.out.println("移除v2的个数="+lon);
	
	Long listSize = boundListOps.size();
	for(int i = 0; i < listSize;i++) {
		//弹出右边的数据
		boundListOps.leftPop();
	}
}

结果: 移除v2的个数=2

链表参考:RedisTemplate常用集合使用说明-boundListOps(八)

 

github:https://github.com/zhangyangfei/SpringBootLearn.git 中的springRedis工程。

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值