Redis(RedisTemplate)使用list链表

RedisTemplate配置:https://www.cnblogs.com/weibanggang/p/10188682.html

package com.wbg.springRedis.test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.data.redis.connection.RedisListCommands;
import org.springframework.data.redis.core.RedisTemplate;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

public class TestList {
    static RedisTemplate redisTemplate = null;

    public static void main(String[] args) throws UnsupportedEncodingException {
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring-redis.xml");
        redisTemplate = applicationContext.getBean(RedisTemplate.class);
        //删除链表
        if (redisTemplate.hasKey("list"))
            redisTemplate.delete("list");
        //那node3插入到list链表
        redisTemplate.opsForList().leftPush("list", "node1");
        print();
        List<String> list = new ArrayList<>();
        for (int i = 2; i < 5; i++) {
            list.add("node" + i);
        }
        //相当lpush把多个值从左插入到链表
        redisTemplate.opsForList().leftPushAll("list", list);
        print();
        //右边插入一个节点
        redisTemplate.opsForList().rightPushAll("list", "node6");
        //获取下标为0的节点
        System.out.println(redisTemplate.opsForList().index("list", 0));
        //获取链表的长度
        System.out.println(redisTemplate.opsForList().size("list"));
        //弹出(删除)左边一个节点
        System.out.println(redisTemplate.opsForList().leftPop("list"));
        //弹出(删除)右边一个节点
        System.out.println(redisTemplate.opsForList().rightPop("list"));
        print();

        //需要使用更为底层的命令才能操作linsert命令
        //在node2前插入before_node节点  RedisListCommands.Position.BEFORE,
        redisTemplate.getConnectionFactory().getConnection().lInsert(
                "list".getBytes("utf-8"),
                RedisListCommands.Position.BEFORE,
                "node2".getBytes("utf-8"),
                "before_node".getBytes("utf-8")
        );
        print();
        //在node2后插入after_node节点  RedisListCommands.Position.AFTER,
        redisTemplate.getConnectionFactory().getConnection().lInsert(
                "list".getBytes("utf-8"),
                RedisListCommands.Position.AFTER,
                "node2".getBytes("utf-8"),
                "after_node".getBytes("utf-8")
        );
        print();
        //如果list存在  左边插入
        redisTemplate.opsForList().leftPushIfPresent("list", "leftEx");
        //如果list存在  右边插入
        redisTemplate.opsForList().rightPushIfPresent("list", "rightEx");
        //左到右  下获取标从0-10节点元素
        list = redisTemplate.opsForList().range("list", 0, 5);
        System.out.println(list);
        //左到右删除多个node节点
        redisTemplate.opsForList().remove("list", 3, "node");
        //给链表下标设置新值
        redisTemplate.opsForList().set("list", 0, "new_value");
        print();


        /*-------------------Spring对Redis阻塞命令的操作--------*/
        //清空数据
        redisTemplate.delete("list");
        redisTemplate.delete("list1");
        List<String> nodeList = new ArrayList<>();
        for (int i = 0; i < 5; i++) {
            nodeList.add("node"+i);
        }

        redisTemplate.opsForList().leftPushAll("list1",nodeList);
        //相当与blpop命令  可以设置时间参数
        redisTemplate.opsForList().leftPop("list1",1, TimeUnit.SECONDS);
        redisTemplate.opsForList().leftPop("list1",1, TimeUnit.SECONDS);
        nodeList.clear();
        for (int i = 0; i < 2; i++) {
            nodeList.add("data"+i);
        }
        redisTemplate.opsForList().leftPushAll("list",nodeList);
        redisTemplate.opsForList().rightPopAndLeftPush("list","list1");
        redisTemplate.opsForList().rightPopAndLeftPush("list","list1",1,TimeUnit.SECONDS);

    }

    public static void print() {
        System.out.println(redisTemplate.opsForList().range("list", 0, redisTemplate.opsForList().size("list")));
    }
}

猜你喜欢

转载自www.cnblogs.com/weibanggang/p/10189331.html
今日推荐