spring-data-redis操作redis
1.在maven pom檔案中引入依賴
2.在yml檔案中配置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