Redis

文章目录


一、Redis是什么?

Redis

二、Redis的常用基本配置

Redis

三、Redis通用命令

Redis

四、Redis数据类型

Redis

1.String 字符串类型

Redis
Redis

2.Hash键值类型

Redis
Redis

3.List列表类型

Redis
Redis

4.Set与Zset集合

Redis

五、Jedis

Redis

1.maven导入依赖

<dependencies>
        <dependency>
            <groupId>redis.clients</groupId>
            <artifactId>jedis</artifactId>
            <version>2.9.0</version>
        </dependency>
    </dependencies>

2.使用

public class JedisTestor {
    public static void main(String[] args) {
        Jedis jedis = new Jedis("192.168.86.128",6379);
        try{
            jedis.auth("123456");
            jedis.select(2);
            System.out.println("success");
            jedis.mset(new String[]{"title","奶粉","num","20"});
            List<String> mget = jedis.mget(new String[]{"sn", "title", "num"});
            System.out.println(mget);

            Long num = jedis.incr("num");
            System.out.println(num);

            jedis.hset("student:3312","name","小明");
            String name = jedis.hget("student:3312", "name");
            System.out.println(name);

            Map<String,String> stu = new HashMap();
            stu.put("name","lili");
            stu.put("age","18");
            stu.put("id","3313");
            jedis.hmset("student:3313",stu);
            Map<String, String> smap = jedis.hgetAll("student:3313");
            System.out.println(smap);

            jedis.del("letter");
            jedis.rpush("letter",new String[]{"d","e","f"});
            jedis.lpush("letter",new String[]{"c","b","a"});
            List<String> letter = jedis.lrange("letter", 0, -1);
            jedis.lpop("letter");
            jedis.rpop("letter");
            letter = jedis.lrange("letter", 0, -1);

            System.out.println(letter);
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            jedis.close();
        }
    }
}

3.Jedis缓存数据

public class CacheSample {
    public CacheSample(){
        Jedis jedis = new Jedis("192.168.86.128",6379);
        try {
            List<Goods> goodsList = new ArrayList<>();
            goodsList.add(new Goods(8818,"pg","",3.5f));
            goodsList.add(new Goods(8819,"xj","",5f));
            goodsList.add(new Goods(8820,"cz","",25f));
            jedis.auth("123456");
            jedis.select(3);
            for(Goods goods:goodsList){
                String json = JSON.toJSONString(goods);
                System.out.println(json);
                String key = "goods"+goods.getGoodsId();
                jedis.set(key,json);
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            jedis.close();
        }
    }

    public static void main(String[] args) {
        new CacheSample();
        System.out.println("输入要查询的商品编号");
        String goodsId = new Scanner(System.in).next();
        Jedis jedis = new Jedis("192.168.86.128",6379);
        try{
            jedis.auth("123456");
            jedis.select(3);
            String key = "goods"+goodsId;
            if(jedis.exists(key)){
                String s = jedis.get(key);
                System.out.println(s);
                Goods g = JSON.parseObject(s, Goods.class);
                System.out.println(g.getGoodsName());
            }else{
                System.out.println("编号错误");
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            jedis.close();
        }
    }
}
上一篇:redis 数据库基础


下一篇:Redis从0到精通--Jedis