Redis 学习笔记(2)—— Redis Map 类型

1 Redis Map 类型测试

1.1 测试1

package mymap;

import redis.clients.jedis.Jedis;

import java.util.List;
import java.util.Map;
import java.util.Set;

public class MapMain {
    public static void main(String[] args) {
        Jedis jedis = new Jedis("127.0.0.1", 6379);
        jedis.del("name:Mike");

        /*
         * 创建一个对象
         * <rediskey,Map<key,value>>
         * */
        jedis.hset("name:Mike", "姓名", "无名氏");
        jedis.hset("name:Mike", "年龄", "25");
        jedis.hset("name:Mike", "技能", "凌波微步");

        /*
         * 打印对象
         * */
        Map<String, String> Mike = jedis.hgetAll("name:Mike");
        System.out.println("hgetAll  基本信息:");
        for (Map.Entry entry : Mike.entrySet()) {
            System.out.println(entry.getKey() + "-------" + entry.getValue());

        }
        System.out.println();

        /*
         * 获取所有字段的信息
         * */
        Set<String> fields = jedis.hkeys("name:Mike");
        System.out.println("hkeys  ");
        for (String field : fields) {
            System.out.println(field + " ");
        }
        System.out.println();

        /*
        * 获取所有值得信息
        * */
        List<String> values=jedis.hvals("name:Mike");
        System.out.println("hvals ");
        for(String value:values){
            System.out.println(value+" ");
        }
        System.out.println();

        /*
        * 只获取年龄
        * */
        String age=jedis.hget("name:Mike","年龄");
        System.out.println("年龄 = " + age);
        //给年龄加10岁
        jedis.hincrBy("name:Mike","年龄",10);
        System.out.println("最终的年龄是:"+jedis.hget("name:Mike","年龄"));
        System.out.println();

        /*
        * 删除姓名
        * */
        jedis.hdel("name:Mike","姓名");
        for(Map.Entry entry: jedis.hgetAll("name:Mike").entrySet()){
            System.out.println(entry.getKey()+" : "+entry.getValue());
        }

    }
}

D:\programs\Java\jdk1.8.0_181\bin\java.exe "-javaagent:D:\programs\JetBrains\IntelliJ IDEA 2018.2.4\lib\idea_rt.jar=51915:D:\programs\JetBrains\IntelliJ IDEA 2018.2.4\bin" -Dfile.encoding=UTF-8 -classpath D:\programs\Java\jdk1.8.0_181\jre\lib\charsets.jar;D:\programs\Java\jdk1.8.0_181\jre\lib\deploy.jar;D:\programs\Java\jdk1.8.0_181\jre\lib\ext\access-bridge-64.jar;D:\programs\Java\jdk1.8.0_181\jre\lib\ext\cldrdata.jar;D:\programs\Java\jdk1.8.0_181\jre\lib\ext\dnsns.jar;D:\programs\Java\jdk1.8.0_181\jre\lib\ext\jaccess.jar;D:\programs\Java\jdk1.8.0_181\jre\lib\ext\jfxrt.jar;D:\programs\Java\jdk1.8.0_181\jre\lib\ext\localedata.jar;D:\programs\Java\jdk1.8.0_181\jre\lib\ext\nashorn.jar;D:\programs\Java\jdk1.8.0_181\jre\lib\ext\sunec.jar;D:\programs\Java\jdk1.8.0_181\jre\lib\ext\sunjce_provider.jar;D:\programs\Java\jdk1.8.0_181\jre\lib\ext\sunmscapi.jar;D:\programs\Java\jdk1.8.0_181\jre\lib\ext\sunpkcs11.jar;D:\programs\Java\jdk1.8.0_181\jre\lib\ext\zipfs.jar;D:\programs\Java\jdk1.8.0_181\jre\lib\javaws.jar;D:\programs\Java\jdk1.8.0_181\jre\lib\jce.jar;D:\programs\Java\jdk1.8.0_181\jre\lib\jfr.jar;D:\programs\Java\jdk1.8.0_181\jre\lib\jfxswt.jar;D:\programs\Java\jdk1.8.0_181\jre\lib\jsse.jar;D:\programs\Java\jdk1.8.0_181\jre\lib\management-agent.jar;D:\programs\Java\jdk1.8.0_181\jre\lib\plugin.jar;D:\programs\Java\jdk1.8.0_181\jre\lib\resources.jar;D:\programs\Java\jdk1.8.0_181\jre\lib\rt.jar;D:\Data\JavaProject\myredis\target\classes;C:\Users\tzb\.m2\repository\redis\clients\jedis\2.8.2\jedis-2.8.2.jar;C:\Users\tzb\.m2\repository\org\apache\commons\commons-pool2\2.4.2\commons-pool2-2.4.2.jar;C:\Users\tzb\.m2\repository\junit\junit\4.12\junit-4.12.jar;C:\Users\tzb\.m2\repository\org\hamcrest\hamcrest-core\1.3\hamcrest-core-1.3.jar;C:\Users\tzb\.m2\repository\com\google\code\gson\gson\2.3.1\gson-2.3.1.jar mymap.MapMain
hgetAll  基本信息:
年龄-------25
姓名-------无名氏
技能-------凌波微步

hkeys  
年龄 
姓名 
技能 

hvals 
无名氏 
25 
凌波微步 

年龄 = 25
最终的年龄是:35

年龄 : 35
技能 : 凌波微步

Process finished with exit code 0

1.2 测试2

package mymap;

import com.google.gson.Gson;
import redis.clients.jedis.Jedis;

import java.math.BigDecimal;
import java.util.*;

/*
 * 购物车
 * */
public class Cart {
    private Jedis jedis;


    public Cart() {
        jedis = new Jedis("127.0.0.1", 6379);
    }

    public Cart(Jedis jedis) {
        this.jedis = jedis;
    }

    /*
     * 修改购物车中的商品
     * */
    public void updateProduct2Cart(String userName, String productId, int num) {
        jedis.hincrBy("shop-cart:" + userName, productId, num);
    }

    /*
     * 获取购物车中的商品信息
     * */
    public List<Product> getProductsByUserName(String userName) {
        List<Product> products = new ArrayList<Product>();
        Map<String, String> productMap = jedis.hgetAll("shop-cart:" + userName);
        if (productMap == null || productMap.size() == 0) {
            return products;
        }
        for (Map.Entry entry : productMap.entrySet()) {
            Product product = new Product();
            product.setId((String) entry.getKey()); //获取用户购物车中的商品编号
            int num = Integer.parseInt((String) entry.getValue());
            product.setNum(num > 0 ? num : 0);
            complementOtherField(product);
            products.add(product);
        }

        return products;
    }

    private void complementOtherField(Product product) {
        String productId = product.getId();
        String productJsonStr = jedis.get("shop-product:" + productId);
        Product productJson = new Gson().fromJson(productJsonStr, Product.class);
        if (productJson != null) {
            product.setName(productJson.getName());
            product.setPrice(productJson.getPrice());
        }
    }

    public static void main(String[] args) {
        initData();
        Cart cart = new Cart();

        String userName = "Mike";

        //向购物车添加商品
        cart.updateProduct2Cart(userName, "3123918948914", 10);
        cart.updateProduct2Cart(userName, "5667261241411", 100);
        cart.updateProduct2Cart(userName, "9565613461134", -10);

        //打印当前用户的购物车信息
        List<Product> products = cart.getProductsByUserName(userName);
        for (Product product : products) {
            System.out.println(product);
        }
    }

    private static void initData() {
        System.out.println("*******  初始化商品信息 *********");
        Jedis jedis = new Jedis("127.0.0.1", 6379);

        Product product1 = new Product("3123918948914", "惠普(HP)24ES 23.8英寸 全高清IPS 纤薄机身 电脑屏幕 液晶显示器", new BigDecimal(166));
        Product product2 = new Product("5667261241411", "三星(SAMSUNG)27英寸 1800R曲面旗舰窄边框 HDMI/DP高清接口 电脑显示器", new BigDecimal(99));
        Product product3 = new Product("9565613461134", "戴尔(DELL) U2417H 23.8英寸四边微边框旋转升降IPS屏", new BigDecimal(500));

        /*
         * 数据写入 redis
         * */
        jedis.set("shop-product:" + product1.getId(), new Gson().toJson(product1));
        jedis.set("shop-product:" + product2.getId(), new Gson().toJson(product2));
        jedis.set("shop-product:" + product3.getId(), new Gson().toJson(product3));

        /*
         * 打印所有产品信息
         * */
        Set<String> allProductKeys = jedis.keys("shop-product:*");
        for (String key : allProductKeys) {
            String json = jedis.get(key);
            Product product = new Gson().fromJson(json, Product.class);
            System.out.println(product);
        }

        System.out.println("===================用户购物车信息如下=================");

    }
}

package mymap;

import java.math.BigDecimal;

public class Product {
    private String id;
    private String name;
    private BigDecimal price;
    private int num;

    public Product() {
    }

    public Product(String id, String name, BigDecimal price) {
        this.id = id;
        this.name = name;
        this.price = price;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public BigDecimal getPrice() {
        return price;
    }

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

    public int getNum() {
        return num;
    }

    public void setNum(int num) {
        this.num = num;
    }

    @Override
    public String toString() {
        return "Product{" +
                "id='" + id + '\'' +
                ", name='" + name + '\'' +
                ", price=" + price +
                ", num=" + num +
                '}';
    }
}

©️2020 CSDN 皮肤主题: 书香水墨 设计师:CSDN官方博客 返回首页