Spring RedisTemplate操作-List操作

package com.panku.web.redis;
import java.util.Collection;

import javax.annotation.Resource;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.dao.DataAccessException;
import org.springframework.data.redis.connection.DataType;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.core.ListOperations;
import org.springframework.data.redis.core.RedisCallback;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
/**
 * Spring RedisTemplate操作-List操作
 * @author ccx
 *
 */
@RunWith(SpringJUnit4ClassRunner.class)  
@ContextConfiguration(locations = "classpath:applicationContext.xml")  
public class RedisTemplateList {
    @Resource(name="redisTemplate")
    private RedisTemplate rt;
    
    public void flushdb(){
        rt.execute(new RedisCallback() {
            public String doInRedis(RedisConnection connection) throws DataAccessException {
                connection.flushDb();
                return "ok";
            }
        });
    }
    @Test
    public void set(){
        flushdb();
        ListOperations vo = rt.opsForList();
        vo.leftPush("book", "wzg");
        vo.leftPushAll("book", "cff","cl","mc");
        vo.leftPush("book", "cl", "||");
        vo.rightPush("book", "yw");
        vo.rightPushAll("book", "sj","jy");
        vo.rightPush( "book", "sj", "||");
        vo.leftPop("book");
        vo.rightPop("book");
        vo.rightPopAndLeftPush("book", "bag");
        vo.set("book", 0, "gzf");
        vo.leftPush("bag", "||");
        vo.remove("bag", 3, "||");
    }
    @Test
    public void get(){
        ListOperations vo = rt.opsForList();
        out(vo.range("book", 0, -1));
        vo.trim("book", 2, 3);
        out(vo.range("book", 0, -1));
        out(vo.size("book"));
        out(vo.range("bag", 0, -1));
        out(vo.index("book", 1));
        
    }
    
    public void out(String str){
        System.out.println(str);
    }
    public void out(Collection list){
        System.out.println(list);
    }
    public void out(long num){
        System.out.println(num);
    }
    public void out(boolean num){
        System.out.println(num);
    }
    public void out(DataType num){
        System.out.println(num);
    }
}

你可能感兴趣的:(Spring RedisTemplate操作-List操作)