redis示例

原创 2015年11月20日 23:19:14

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的MVC示例

  • 2016年10月29日 19:41
  • 45.01MB
  • 下载

redis cluster 集群配置示例: 创建, 添加节点, 重新分片, 删除节点

1 redis集群搭建 Redis 从3.0.0 正式版开始官方支持集群,  下面开始做一个集群配置的示例.   官网地址: http://redis.io/download 当前最新版下载地址: h...
  • reyleon
  • reyleon
  • 2016年05月19日 15:50
  • 5676

Python操作Redis的小示例

  • 2013年10月06日 19:15
  • 1KB
  • 下载

maven版spring-data-redis简单示例

  • 2013年03月06日 17:32
  • 17KB
  • 下载

redis 学习笔记(2)-client端示例代码

转自:http://www.cnblogs.com/yjmyzz/p/4105731.html redis提供了几乎所有主流语言的client,java中主要使用二种:Jedis与Redis...
  • lohocc
  • lohocc
  • 2015年03月05日 13:17
  • 289

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

  • 2016年08月16日 11:12
  • 6.66MB
  • 下载

redis demo 示例

  • 2014年04月08日 23:31
  • 43.1MB
  • 下载

Redis示例配置文件

# Redis示例配置文件 # 注意单位问题:当需要设置内存大小的时候,可以使用类似1k、5GB、4M这样的常见格式: # # 1k => 1000 bytes # 1kb => 1024 byt...
  • pcyph
  • pcyph
  • 2015年08月26日 22:26
  • 504
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:redis示例
举报原因:
原因补充:

(最多只允许输入30个字)