StringRedisTemplate的相關用法

1.Redis String 操作字符串

        //新增元素
    	stringRedisTemplate.opsForValue().set("key", "value");
    	//獲取元素
    	stringRedisTemplate.opsForValue().get("key");
    	//刪除元素
    	stringRedisTemplate.delete("key");
    	

2.Redis List 操作List

        //將數據添加到key對應的現有數據的左邊
    	stringRedisTemplate.opsForList().leftPush("key", "value");
    	//將數據添加到key對應的現有數據的右邊
    	stringRedisTemplate.opsForList().rightPush("key", "value");
    	//從左往右遍歷
    	stringRedisTemplate.opsForList().leftPop("key");
    	//從右往左遍歷
    	stringRedisTemplate.opsForList().rightPop("key");
    	//查詢全部元素
    	stringRedisTemplate.opsForList().range("key", 0, -1);
    	//查詢前三個元素
    	stringRedisTemplate.opsForList().range("key", 0, 3);
    	//從左往右刪除list中元素A  (1:從左往右 -1:從右往左 0:刪除全部)
    	stringRedisTemplate.opsForList().remove("key", 1, "A");
    	

3.Redis Hash 操作Hash

        //判斷key對應的map中是否存在key1
    	stringRedisTemplate.opsForHash().hasKey("key", "key1");
    	//往key對應的map中新增(key1,value1)
    	stringRedisTemplate.opsForHash().put("key", "key1", "value1");
    	//獲取key對應的map中key1的值
    	stringRedisTemplate.opsForHash().get("key", "key1");
    	//刪除key對應的map中多個子key(可變參數) 
    	stringRedisTemplate.opsForHash().delete("key", "key1" ,"key2" ,"key3");
    	//獲取key對應的map
    	stringRedisTemplate.opsForHash().entries("key");
    	//獲取key對應的map中全部子key集合
    	stringRedisTemplate.opsForHash().keys("key");
    	//獲取key對應的map中全部value集合
    	stringRedisTemplate.opsForHash().values("key");

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章