利用redis缓存数据

package com.wei.jedis;

import com.alibaba.fastjson.JSON;
import jdk.nashorn.internal.parser.JSONParser;
import redis.clients.jedis.Jedis;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class CacheSample {
    public CacheSample(){
        Jedis jedis = new Jedis("192.168.177.139",6379);
        try{
            List<Goods> goodsList = new ArrayList<Goods>();
            goodsList.add(new Goods(8818, "红富士", "好吃", 23f));
            goodsList.add(new Goods(8819, "进口香蕉", "不错", 22f));
            goodsList.add(new Goods(8820,"进口苹果", "很脆", 23f));

            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.177.139", 6379);
        try{
            jedis.auth("123456");
            jedis.select(3);
            String key="goods:"+goodsId;
            if (jedis.exists(key)) {
                String json = jedis.get(key);
                System.out.println(json);
                Goods goods = JSON.parseObject(json, Goods.class);
                System.out.println(goods.getGoodsName());
                System.out.println(goods.getPrice());
            }else{

                System.out.println("您输入的商品编号不存在,请重新输入!");
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        finally {
            jedis.close();
        }
    }
}

 

package com.wei.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;
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

阳光不锈@

如果有帮助的话,打赏一下吧

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值