Redis代碼示例


RedisTemplate

如果想要在java中使用Redis相關的數據結構,要先注入RedisTemplate。

    @Autowired
    private RedisTemplate<K,V>  redisTemplate;

其中K,V類型,可以使用具體的類型,比如String或者其他具體類。

    @Autowired
    private RedisTemplate<String,Integer>  redisTemplate;

StringRedisTemplate

如果key和value都是String類型的,直接用StringRedisTemplate 。

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

List(隊列)

Redis隊列通過redisTemplate.opsForList()來操作。
常用api如下:

redisTemplate.opsForList().rightPush(K  key ,V  value);  //表示從隊列的右側放入新的值 ,其中key為隊列名,value為入列的值

redisTemplate.opsForList().leftPop(K  key);            //取出隊列最右側的值

redisTemplate.opsForList().range(K key, long start, long end);         //遍歷隊列

示例如下:
從隊列的最右側放入新的數據。從隊列的最左側取出已有的數據。

@Autowired
private StringRedisTemplate stringRedisTemplate;

redisTemplate.opsForList().rightPush("list","python");

redisTemplate.opsForList().leftPop("list");

Hash(散列)

Redis的散列可以讓將多個鍵值對存儲到一個Redis鍵里面。
常用api:

void put(H key, HK hashKey, HV value);      //設置散列hashKey的值
HV get(H key, Object hashKey);      //從鍵中的哈希獲取給定hashKey的值。返回類型HV表示HashValue
Set<HK> keys(H key);            //獲取key所對應的散列表的key
Set<K> keys(K pattern) ;        //按照給定的pattern查找key。*表示所有的key,關鍵字加*表示模糊查詢,比如user*表示所有帶user的key。
Long increment(H key, HK hashKey, long delta);     //通過給定的delta增加散列hashKey的值(整型)
Boolean hasKey(H key, Object hashKey);     //確定哈希hashKey是否存在
Long size(H key);      //獲取key所對應的散列表的大小個數
Cursor<Map.Entry<HK, HV>> scan(H key, ScanOptions options);       //使用Cursor在key的hash中迭代,相當於迭代器。

示例如下:
1.通過Hash存儲某個用戶數據。

redisTemplate.opsForHash().put("user2","name","tom");
redisTemplate.opsForHash().put("user2","age",26);
redisTemplate.opsForHash().put("user2","sex","male");

2.獲取用戶數據的所有字段。

System.out.println(redisTemplate.opsForHash().keys("user2"));
//redisHash1所對應的散列表為{age=26, name=jack, sex=male}
結果:[name, sex, age]

3.獲取某個用戶的年齡:

String age=redisTemplate.opsForHash().get("user2","age")

4.獲取所有所有key包含user的數據。
通過keys(pattern)方式查找key,以下的pattern為user加上*表示所有包含user的key。

Set<String> setKeys = stringRedisTemplate.keys("user"+"*");
List<Object> list = new ArrayList<>();
for(String key:setKeys) {
	//取出所有數據
	List<Object> hashList = stringRedisTemplate.opsForHash().values(key);
	for(int i=0;i<hashList.size();i++) {
		Object object=hashList.get(i);
	}
}

5.使用Cursor遍歷:

使用:Cursor<Map.Entry<Object, Object>> curosr = template.opsForHash().scan("user2", ScanOptions.ScanOptions.NONE);
        while(curosr.hasNext()){
            Map.Entry<Object, Object> entry = curosr.next();
            System.out.println(entry.getKey()+":"+entry.getValue());
        }
結果:
age:26
name:tom
male:sex

Set(無序集合)

Redis的Set是string類型的無序集合。集合成員是唯一的,這就意味着集合中不能出現重復的數據。
Redis 中 集合是通過哈希表實現的,所以添加,刪除,查找的復雜度都是O(1)。
常用api如下:

Long add(K key, V... values);
Long remove(K key, Object... values);
Cursor<V> scan(K key, ScanOptions options);            //遍歷set

ZSET(有序集合)

ZSet是有序的集合,且不允許重復的成員。

Long rank(K key, Object o);   //返回有序集中指定成員的排名,其中有序集成員按分數值遞增(從小到大)順序排列

更詳細的講解請見:
https://www.jianshu.com/p/7bf5dc61ca06


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM