欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

spring-data-redis操作redis

程序员文章站 2022-06-02 19:54:02
...

1.在maven pom文件中引入依赖

spring-data-redis操作redis

2.在yml文件中配置redis的链接信息

spring-data-redis操作redis

3.开始编码

  3.1   引用RedisTemplate<K, V>模板或者StringRedisTemplate

        

	@Autowired
	private StringRedisTemplate stringRedisTemplate;//
	@Autowired
	private RedisTemplate<String, String> redisTemplate;//

3.2     设置String类型的值(再设置一次这个key的值就是修改)

/**
	 * 设置String类型的值
	 * @return
	 */
	@RequestMapping("/set")
	@ResponseBody
	public String set(){
		BoundValueOperations<String, String> boundValueOperations = redisTemplate.boundValueOps("abcd");
		
		String value = "我是超人";
		boundValueOperations.set(value , 300, TimeUnit.SECONDS);//值/存活时间/时间单位
		boundValueOperations.set(value+",你是吗");//值
		//boundValueOperations.set("你老爸", 3);//值/偏移量 很不好用,不如那种来用String的方法做替换
		return boundValueOperations.get();
	}

3.3    获取String类型的值

/**
	 * 获取String类型的值
	 * @return
	 */
	@RequestMapping("/get")
	@ResponseBody
	public String get(){
		BoundValueOperations<String, String> boundValueOperations = redisTemplate.boundValueOps("abcd");
		return boundValueOperations.get();
	}

3.4     设置hash类型的值

/**
	 * 设置hash类型的值
	 * @return
	 */
	@RequestMapping("/hset")
	@ResponseBody
	public String hset() {
		Customer customer = customerService.getCustomer(1l);
		//获取hash类型操作接口
		BoundHashOperations<String,String,Object> boundHashOperations =stringRedisTemplate.boundHashOps("customer:id:"+customer.getId());//设置hash对象的key
		boundHashOperations.put("id", customer.getId());
		boundHashOperations.put("address", customer.getAddress());
		boundHashOperations.put("borthDay", customer.getBorthDay().getTime());
		boundHashOperations.put("name", customer.getName());
		boundHashOperations.put("customer:id:"+customer.getId(), customer);
		return boundHashOperations.getKey();
	}

3.5    获取hash类型的值

/**
	 * 获取hash类型的值
	 * @param key
	 * @return
	 */
	@RequestMapping("/getHset/{key}")
	@ResponseBody
	public Map<Object, Object> getHset(@PathVariable String key) {
		BoundHashOperations<String,Object,Object> boundHashOperations =stringRedisTemplate.boundHashOps(key);
		Map<Object, Object> map = boundHashOperations.entries();
		return map;
	}

3.6    获取hash类型的指定键的值

/**
	 * 获取hash类型的指定键的值
	 * @param key
	 * @return
	 */
	@RequestMapping("/getHsetKey/{key}")
	@ResponseBody
	public String getHsetKey(@PathVariable String key) {
		BoundHashOperations<String,Object,Object> boundHashOperations =stringRedisTemplate.boundHashOps(key);
		return (String)boundHashOperations.get("name");
	}

3.7    删除hash类型的指定键值对

/**
	 * 删除hash类型的指定键值对
	 * @param key
	 * @return
	 */
	@RequestMapping("/delHsetKey/{key}")
	@ResponseBody
	public Long delHsetKey(@PathVariable String key) {
		BoundHashOperations<String,Object,Object> boundHashOperations =stringRedisTemplate.boundHashOps(key);
		return boundHashOperations.delete("borthDay");
	}

3.8    设置有序集合的值

/**
	 * 设置有序集合的值
	 * @return
	 */
	@RequestMapping("/setZset")
	@ResponseBody
	public Set<String> setZset() {
		BoundZSetOperations<String, String> boundZSetOperations = redisTemplate.boundZSetOps("dragon_list");
		boundZSetOperations.add("小龙", 1);//元素值/排序分数
		boundZSetOperations.add("龙龙", 1);
		boundZSetOperations.add("魔龙", 2);
		boundZSetOperations.add("神龙", 3);
		return redisTemplate.boundZSetOps("dragon_zset").range(0, -1);//获取有序集合的值
	}

3.9    获取有序集合的值

/**
	 * 获取有序集合的值
	 * @return
	 */
	@RequestMapping("/getZset")
	@ResponseBody
	public Set<TypedTuple<String>> getZset() {
		Set<TypedTuple<String>> set = redisTemplate.boundZSetOps("dragon_zset").rangeWithScores(0, -1);
		return set;
	}

3.10    删除有序集合的值

	/**
	 * 删除有序集合的指定值
	 * @return
	 */
	@RequestMapping("/delZset")
	@ResponseBody
	public Long delZset() {
		Long count = redisTemplate.boundZSetOps("dragon_zset").remove("魔龙");
		return count;
	}

3.11    设置集合的值

/**
	 * 设置集合的值
	 * @return
	 */
	@RequestMapping("/setSet")
	@ResponseBody
	public Set<String> setSet() {
		BoundSetOperations<String, String> boundSetOperations = redisTemplate.boundSetOps("dragon_set");
		boundSetOperations.add("小龙","龙龙","魔龙","神龙");
		boundSetOperations.expire(5000, TimeUnit.SECONDS);
		return boundSetOperations.members();
	}

3.12    获取集合的值

/**
	 * 获取集合的值
	 * @return
	 */
	@RequestMapping("/getSet")
	@ResponseBody
	public Set<String> getSet() {
		BoundSetOperations<String, String> boundSetOperations = redisTemplate.boundSetOps("dragon_set");
		return boundSetOperations.members();
	}

3.13    删除集合的指定值

/**
	 * 删除集合的指定值
	 * @return
	 */
	@RequestMapping("/delSet")
	@ResponseBody
	public Set<String> delSet() {
		BoundSetOperations<String, String> boundSetOperations = redisTemplate.boundSetOps("dragon_set");
		boundSetOperations.remove("魔龙");
		return boundSetOperations.members();
	}

3.14    设置列表的值

/**
	 * 设置列表的值
	 * @return
	 */
	@RequestMapping("/setList")
	@ResponseBody
	public List<String> setList() {
		BoundListOperations<String, String> boundListOperations = redisTemplate.boundListOps("dragon_list");
		boundListOperations.leftPushAll("小龙","龙龙","魔龙","神龙");//从列表头部开始添加(直观排列:神龙-->小龙),小龙最先进,不停被挤下去
		boundListOperations.rightPushAll("神龙","魔龙","龙龙","小龙");//从列表尾部开始添加(直观排列:神龙-->小龙),神龙最先进,后面不停添加
		System.err.println(boundListOperations.index(0));//列表中索引为0的值
		return boundListOperations.range(0, -1);
	}

3.15    获取列表的值

/**
	 * 获取列表的值
	 * @return
	 */
	@RequestMapping("/getList")
	@ResponseBody
	public List<String> getList() {
		BoundListOperations<String, String> boundListOperations = redisTemplate.boundListOps("dragon_list");
		return boundListOperations.range(0, -1);
	}

3.16    删除列表的值

/**
	 * 删除列表的指定值
	 * @return
	 */
	@RequestMapping("/delList")
	@ResponseBody
	public List<String> delList() {
		BoundListOperations<String, String> boundListOperations = redisTemplate.boundListOps("dragon_list");
		/**
		 * count > 0 : 从表头开始向表尾搜索,移除与 VALUE 相等的元素,数量为 COUNT 。
		 * count < 0 : 从表尾开始向表头搜索,移除与 VALUE 相等的元素,数量为 COUNT 的绝对值。
		 * count = 0 : 移除表中所有与 VALUE 相等的值。
		 */
		int count = 1;
		boundListOperations.remove(count, "魔龙");//count/value
		return boundListOperations.range(0, -1);
	}

3.17    删除键值对

/**
	 * 删除(删除键值对,hash,set,zset,list被删除后就没了,不同于它们各自删除内部指定值得删除)
	 * @param key
	 * @return
	 */
	@RequestMapping("/del/{key}")
	@ResponseBody
	public boolean del(@PathVariable String key){
		return redisTemplate.delete(key);
	}

3.18    选择序列化方式(所有数据保存到redis上面都通过了序列化)

	stringRedisTemplate.setEnableDefaultSerializer(false);
		stringRedisTemplate.setKeySerializer(new JdkSerializationRedisSerializer());
		stringRedisTemplate.setValueSerializer(new OxmSerializer());
		stringRedisTemplate.setHashValueSerializer(new Jackson2JsonRedisSerializer<Object>(Object.class));
		stringRedisTemplate.setStringSerializer(new StringRedisSerializer());
		stringRedisTemplate.setDefaultSerializer(new StringRedisSerializer(Charset.forName("UTF-8")));
stringRedisTemplate.setKeySerializer(new Jackson2JsonRedisSerializer<>(String.class));//设置非hash类型键的序列化方式
stringRedisTemplate.setValueSerializer(new Jackson2JsonRedisSerializer<>(String.class));//设置非hash类型值的序列化方式
stringRedisTemplate.setHashKeySerializer(new Jackson2JsonRedisSerializer<>(String.class));//设置hash键的序列化方式

stringRedisTemplate.setHashValueSerializer(new Jackson2JsonRedisSerializer<>(String.class));//设置hash值的序列化方式

3.19自动注入redisTemplate的时候不要给redisTemplate的泛型指定类型,如果指定了需要用@Resource来注入,用@Autowired注入的话只能设置成redisTemplate<String,String>,因为StringRedisTemplate实现redisTemplate的时候传入的是这个类型,所以可以找到这个bean,其它的就没办法找到,需要用到@Resource根据redisTemplate名称找到bean,但如果不指定具体的泛型类型,那么它所注入的bean也没有指定到具体类型,在应用可以可以*填写

@Autowired
	private RedisTemplate redisTemplate;//@Resource根据redisTemplate名称查找注入
	@RequestMapping("/set")
	@ResponseBody
	public Customer set(){
		Customer customer = customerService.getCustomer(1l);
		BoundValueOperations<String, Customer> boundValueOperations = redisTemplate.boundValueOps("abcd");
		redisTemplate.setEnableDefaultSerializer(true);
		redisTemplate.setDefaultSerializer(new Jackson2JsonRedisSerializer<>(Customer.class));
		redisTemplate.setKeySerializer(new StringRedisSerializer());
		redisTemplate.setValueSerializer(null);
		redisTemplate.afterPropertiesSet();
//		String value = "我是超人";
//		boundValueOperations.set(value , 300, TimeUnit.SECONDS);//值/存活时间/时间单位
//		boundValueOperations.set(value+",你是吗");//值
		//boundValueOperations.set("你老爸", 3);//值/偏移量 很不好用,不如那种来用String的方法做替换
		boundValueOperations.set(customer);
		return  boundValueOperations.get();
	}

4.总结

spring data redis所封装的实现是jredis和lettuce,它们对redis的操作都是基于redis命令的,spring-data-redis提供了这两个工具对redis的连接操作,如果有什么不懂可以通过redis命令来学习spring-data-redis

相关标签: redis java