redis示例

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();
    }
}


阅读更多
个人分类: Redis
想对作者说点什么? 我来说一句

Python操作Redis的小示例

2013年10月06日 1KB 下载

C# Redis示例Demo

2013年11月26日 14.35MB 下载

redis windows 安装包含.net项目示例

2016年08月16日 6.66MB 下载

没有更多推荐了,返回首页

不良信息举报

redis示例

最多只允许输入30个字

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭