Redis學習筆記二、基本使用二 集合使用

1、省略了類
package com.haoxiansheng.middleware.springMessage;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.haoxiansheng.middleware.MainApplicationTest;
import com.haoxiansheng.middleware.rabbitmq.entity.Fruit;
import com.haoxiansheng.middleware.rabbitmq.entity.Person;
import com.haoxianshengr.middleware.rabbitmq.entity.PhoneUser;
import com.haoxiansheng.middleware.rabbitmq.entity.Student;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.*;

import java.util.*;

@Slf4j
public class RedisCollectionsTest extends MainApplicationTest {

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private ObjectMapper objectMapper;

    /**
     * 列表操作List
     */
    @Test
    public void testRedisList() {

        List<Person> persons = new ArrayList<>();
        persons.add(new Person(1, "張飛", "1255679"));
        persons.add(new Person(2, "劉備", "1253679"));
        persons.add(new Person(3, "關羽", "1275679"));
        persons.add(new Person(4, "趙雲", "1258679"));
        log.info("RedisList==> 構造好已經排好序的對象列表,{}", persons);

        final String key = "redis:test:list:4";
        ListOperations listOperations = redisTemplate.opsForList();

        persons.forEach(x -> listOperations.leftPush(key, x)); // 往列表中添加元素 ==> 從隊尾中添加

        // 獲取Redis 中list 的數據-  從隊頭中遍歷獲取, 直到沒有元素爲止
        log.info("==獲取Redis 的數據--從隊頭中獲取==");
        Object personReceive = listOperations.rightPop(key);
        Person resP;
        while (Objects.nonNull(personReceive)) {
            resP = (Person) personReceive;
            log.info("當前解析數據:{}", resP);
            personReceive = listOperations.rightPop(key);
        }
    }

    /**
     * Set 與list 操作基本類似 區別Set 存放具有相同類型或特性的不重複的數據
     */

    @Test
    public void testRedisSet() {

        Set<String> users = new HashSet<>();
        users.add("張三");
        SetOperations setOperations = redisTemplate.opsForSet();
        final String key = "redis:test:set:1";
        users.forEach(x -> setOperations.add(key, x));

        Object user = setOperations.pop(key);
        while (Objects.nonNull(user)) {
            log.info("從緩衝中獲取的集合- 當前用戶:{}", user);
            user = setOperations.pop(key);
        }
    }

    /**
     * SortedSet
     */

    @Test
    public void testRedisZset() {
        // 構造一組無序的用戶手機充值對象列表
        List<PhoneUser> phoneUsers = new ArrayList<>();
        phoneUsers.add(new PhoneUser("101", 12.0));
        phoneUsers.add(new PhoneUser("109", 99.0));
        phoneUsers.add(new PhoneUser("187", 112.0));
        phoneUsers.add(new PhoneUser("156", 95.0));
        phoneUsers.add(new PhoneUser("154", 17.0));
        phoneUsers.add(new PhoneUser("199", 94.0));

        log.info(" 構造一組無序的用戶手機充值對象列表 =={}", phoneUsers);

        final String key = "redis:zset:6";
        // 因爲zSet 在add 元素進入緩衝後, 下次就不能進行更新了, 因而爲了測試方便 進行操作之前先清空該緩存(實際生產環境中不建議這麼使用)
        redisTemplate.delete(key);

        // 獲取有序集合SortedSet 操作組件ZSetOperations
        ZSetOperations zSetOperations = redisTemplate.opsForZSet();
        phoneUsers.forEach(x -> zSetOperations.add(key, x, x.getFare()));

        //獲取訪問充值排名考前的用戶列表
        Long size = zSetOperations.size(key);

        Set<PhoneUser> minSet = zSetOperations.range(key, 0L, size);  //從小到大排序

        Set<PhoneUser> maxSet = zSetOperations.reverseRange(key, 0L, size);  // 從大到小排名

        minSet.forEach(x -> log.info("從緩衝中讀取手機充值記錄排序列表, 當前記錄=={}", x));

        maxSet.forEach(x -> log.info("從緩衝中讀取手機充值記錄排序列表, 當前記錄=={}", x));

    }


    /**
     * hash 哈希存儲
     */
    @Test
    public void testRedisHash() {

        List<Student> students = new ArrayList<>();
        List<Fruit> fruits = new ArrayList<>();

        students.add(new Student("101", "java", "齊天大聖"));
        students.add(new Student("102", "php", "秦王"));
        students.add(new Student("103", "guava", "老子"));

        fruits.add(new Fruit("apple", "紅色"));
        fruits.add(new Fruit("orange", "橙色"));
        fruits.add(new Fruit("banana", "黃色"));

        final String skey = "redis:hash:student:3";
        final String fkey = "redis:hash:fruit:3";

        //遍歷添加 採用hash 哈希存儲至緩衝中
        HashOperations hashOperations = redisTemplate.opsForHash(); // 獲取hash 存儲操作組件
        students.forEach(x -> hashOperations.put(skey, x.getId(), x));
        fruits.forEach(x -> hashOperations.put(fkey, x.getName(), x));

        // 獲取學生與水果列表
        Map<String, Student> studentMap = hashOperations.entries(skey);
        log.info("獲取學生列表對象=={}", studentMap);
        Map<String, Fruit> fruitMap = hashOperations.entries(fkey);
        log.info("獲取s水果列表對象=={}", fruitMap);

        // 獲取指定的學生對象
        String sField = "103";
        Student student = (Student) hashOperations.get(skey, sField);
        log.info("獲取指定學生對象=={}===> {}", sField, student);
        // 獲取指定的水果對象
        String fField = "orange";
        Fruit fruit = (Fruit) hashOperations.get(fkey, fField);
        log.info("獲取指定水果對象=={}===> {}", fField, fruit);
    }
}

 

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