关闭

redis示例

344人阅读 评论(0) 收藏 举报
分类:

List操作

import org.apache.commons.lang.SerializationUtils;
import org.junit.Before;
import org.junit.Test;
import redis.clients.jedis.Jedis;

import java.io.Serializable;
import java.util.List;

/**
 * Created by IntelliJ IDEA
 * Date: 15/11/20
 * Time: 下午10:09
 */
public class redisListTest {

    private Jedis redis;

    static class Model implements Serializable {
        private Integer id;
        private String name;
        private String address;

        public Integer getId() {
            return id;
        }

        public void setId(Integer id) {
            this.id = id;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public String getAddress() {
            return address;
        }

        public void setAddress(String address) {
            this.address = address;
        }

        @Override
        public String toString() {
            return "Model{" +
                    "id=" + id +
                    ", name='" + name + '\'' +
                    ", address='" + address + '\'' +
                    '}';
        }
    }

    @Test
    public void testAdd() {
        Model model1 = new Model();
        model1.setId(1);
        model1.setName("zhangsan");
        model1.setAddress("beijing");
        redis.lpush(SerializationUtils.serialize("orderCategory"), SerializationUtils.serialize(model1));

        Model model2 = new Model();
        model2.setId(2);
        model2.setName("lishi");
        model2.setAddress("beijing");
        redis.lpush(SerializationUtils.serialize("orderCategory"), SerializationUtils.serialize(model2));

        System.out.println(redis.llen(SerializationUtils.serialize("orderCategory")));

        List<byte[]> result = redis.lrange(SerializationUtils.serialize("orderCategory"), 0, 1);
        for (int i = 0; i < result.size(); i++) {
            Model temp = (Model) SerializationUtils.deserialize(result.get(i));
            System.out.println(temp);
        }

    }

    @Test
    public void testDelete() {
        redis.expire(SerializationUtils.serialize("orderCategory"),0);
    }

    @Test
    public void testUpdate(){
        Model model2 = new Model();
        model2.setId(2);
        model2.setName("888");
        model2.setAddress("beijing");
        redis.lpush(SerializationUtils.serialize("orderCategory"), SerializationUtils.serialize(model2));
        List<byte[]> result = redis.lrange(SerializationUtils.serialize("orderCategory"), 0, 1);
        for (int i = 0; i < result.size(); i++) {
            Model temp = (Model) SerializationUtils.deserialize(result.get(i));
            System.out.println(temp);
        }
    }

    @Test
    public void testSelect(){
        System.out.println(redis.llen(SerializationUtils.serialize("orderCategory")));

        List<byte[]> result = redis.lrange(SerializationUtils.serialize("orderCategory"), 0, -1);
        for (int i = 0; i < result.size(); i++) {
            Model temp = (Model) SerializationUtils.deserialize(result.get(i));
            System.out.println(temp);
        }
    }

    @Before
    public void testBefore() {
        redis = new Jedis();
    }
}



hash操作

import org.apache.commons.lang.SerializationUtils;
import org.junit.Before;
import org.junit.Test;
import redis.clients.jedis.Jedis;

import java.io.Serializable;
import java.util.Iterator;
import java.util.Map;

/**
 * Created by IntelliJ IDEA
 * Date: 15/11/20
 * Time: 下午10:09
 */
public class redisHashTest {

    private Jedis redis;

    static class Model implements Serializable {
        private Integer id;
        private String name;
        private String address;

        public Integer getId() {
            return id;
        }

        public void setId(Integer id) {
            this.id = id;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public String getAddress() {
            return address;
        }

        public void setAddress(String address) {
            this.address = address;
        }

        @Override
        public String toString() {
            return "Model{" +
                    "id=" + id +
                    ", name='" + name + '\'' +
                    ", address='" + address + '\'' +
                    '}';
        }
    }

    @Test
    public void testAdd() {
        Model model1 = new Model();
        model1.setId(1);
        model1.setName("zhangsan");
        model1.setAddress("beijing");
        redis.hset(SerializationUtils.serialize("orderCategoryHash"), SerializationUtils.serialize(model1.getId()), SerializationUtils.serialize(model1));

        Model model2 = new Model();
        model2.setId(2);
        model2.setName("lishi");
        model2.setAddress("beijing");
        redis.hset(SerializationUtils.serialize("orderCategoryHash"), SerializationUtils.serialize(model2.getId()), SerializationUtils.serialize(model2));

        System.out.println(redis.hlen(SerializationUtils.serialize("orderCategoryHash")));

        Map<byte[], byte[]> fields = redis.hgetAll(SerializationUtils.serialize("orderCategoryHash"));
        Iterator<Map.Entry<byte[], byte[]>> iter = fields.entrySet()
                .iterator();
        while (iter.hasNext()) {
            Map.Entry<byte[], byte[]> e = iter.next();
            byte[] value = e.getValue();
            Model temp = (Model) SerializationUtils.deserialize(value);
            System.out.println(temp);
        }

    }

    @Test
    public void testDelete() {
        redis.expire(SerializationUtils.serialize("orderCategoryHash"), 0);
    }

    @Test
    public void testUpdate() {
        Model model2 = new Model();
        model2.setId(2);
        model2.setName("888");
        model2.setAddress("sh");
        redis.hset(SerializationUtils.serialize("orderCategoryHash"), SerializationUtils.serialize(model2.getId()), SerializationUtils.serialize(model2));

        byte[] result = redis.hget(SerializationUtils.serialize("orderCategoryHash"), SerializationUtils.serialize(model2.getId()));
        Model temp = (Model) SerializationUtils.deserialize(result);
        System.out.println(temp);
    }

    @Test
    public void testSelect() {
        Map<byte[], byte[]> fields = redis.hgetAll(SerializationUtils.serialize("orderCategoryHash"));
        Iterator<Map.Entry<byte[], byte[]>> iter = fields.entrySet()
                .iterator();
        while (iter.hasNext()) {
            Map.Entry<byte[], byte[]> e = iter.next();
            byte[] value = e.getValue();
            Model temp = (Model) SerializationUtils.deserialize(value);
            System.out.println(temp);
        }
    }

    @Before
    public void testBefore() {
        redis = new Jedis();
    }
}


0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:1176056次
    • 积分:12201
    • 等级:
    • 排名:第1223名
    • 原创:260篇
    • 转载:418篇
    • 译文:0篇
    • 评论:114条
    最新评论