Jedis基础

Jedis是Java语言开发的Redis客户端的工具包
Jedis只是对Redis命令的封装,掌握Redis命令就OK了
需要配置一下Redis
vim redis-conf
修改
是否开启保护模式
protected- mode no
可访问IP
bind 0.0.0.0 (开发时这样用)
修改防火墙将6379端口号开放(永久)
firewall-cmd --zone=public --add- port=6379 --permanent
重新加载防火墙
firewall-cmd --reload

创建一个maven管理的项目 在pop.xml文件中添加

<dependencies>
        <dependency>
            <groupId>redis.clients</groupId>
            <artifactId>jedis</artifactId>
            <version>2.9.0</version>
            <type>jar</type>
            <scope>compile</scope>
        </dependency>
    </dependencies>
public class JedisTestor {
    public static void main(String[] args) {
        Jedis jedis = null;
        try {
        	// IP   端口号
            jedis = new Jedis("IP地址", 6379);
            jedis.auth("密码");
            jedis.select(2);
            System.out.println("Redis连接成功");
            //字符串
            jedis.set("sn", "7781-9938");
            String sn = jedis.get("sn");
            System.out.println("sn:" + sn);
            //一次设置多个
            jedis.mset("title", "婴幼儿奶粉", "num", "20");
            List<String> goods = jedis.mget("sn", "title", "num");
            System.out.println(goods);
            //自增
            Long num = jedis.incr("num");
            System.out.println(num);

            // Hash
            jedis.hset("student:3312", "name", "张晓明");
            String name = jedis.hget("student:3312", "name");
            System.out.println(name);
            Map<String, String> studentMap = new HashMap();
            studentMap.put("name", "李兰");
            studentMap.put("age", "18");
            studentMap.put("id", "3313");
            jedis.hmset("student:3313", studentMap);
            Map<String, String> smap = jedis.hgetAll("student:3313");
            System.out.println(smap);
            // List
            jedis.del("letter");//先删除
            jedis.rpush("letter", "d", "e", "f");//从后增加
            jedis.lpush("letter", "c", "b", "a");// 从前增加
            List<String> letter = jedis.lrange("letter", 0, -1);
            // 查询
            System.out.println(letter);
            jedis.lpop("letter");//第一个弹出
            jedis.rpop("letter");//最后一个弹出
            letter = jedis.lrange("letter", 0, -1);
            System.out.println(letter);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            jedis.close();
        }
    }
}

利用Jedis缓存数据
先创建一个实体类

public class Goods {
    private Integer goodsId;
    private String goodsName;
    private String description;
    private Float price;

    public Goods() {

    }

    public Goods(Integer goodsId, String goodsName, String description, Float price) {
        this.goodsId = goodsId;
        this.goodsName = goodsName;
        this.description = description;
        this.price = price;
    }

    public Integer getGoodsId() {
        return goodsId;
    }

    public void setGoodsId(Integer goodsId) {
        this.goodsId = goodsId;
    }

    public String getGoodsName() {
        return goodsName;
    }

    public void setGoodsName(String goodsName) {
        this.goodsName = goodsName;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public Float getPrice() {
        return price;
    }

    public void setPrice(Float price) {
        this.price = price;
    }


    @Override
    public String toString() {
        return "Goods{" +
                "goodsId=" + goodsId +
                ", goodsName='" + goodsName + '\'' +
                ", description='" + description + '\'' +
                ", price=" + price +
                '}';
    }
}
public class CacheSample {
    public CacheSample() {
        Jedis jedis = new Jedis("IP地址", 6379);
        try {
            List<Goods> goodsList = new ArrayList<>();
            goodsList.add(new Goods(8818, "红富士苹果", "", 3.5f));
            goodsList.add(new Goods(8819, "进口脐橙", "", 4.5f));
            goodsList.add(new Goods(8820, "进口香蕉", "", 25.5f));
            jedis.auth("密码");
            jedis.select(3);
            for (Goods goods : goodsList) {
	            //使用JSON进行序列化
                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("IP地址", 6379);
        try {
            jedis.auth("密码");
            jedis.select(3);
            String key = "goods:" + goodsId;
            // 判断key是否存在
            if (jedis.exists(key)) {
                String json = jedis.get(key);
                System.out.println(json);
                // 将JSON数据解析为Goods
                Goods g = JSON.parseObject(json, Goods.class);
                System.out.println(g);
            } else {
                System.out.println("输入的商品编号不存在,请重新输入!");
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值