Explain:
(1) Demonstrate how to store objects in a redis database
MySQL database, which stores data on the hard disk by default; Due to the slow read and write speed of the hard disk, MySQL processing speed is slower and delays as the data volume increases and the concurrency number increases.
Redis uses memory to store data, which is fast to read and write. Therefore, in projects, such as [commodity data], [personnel information], [equipment information], [asset status] and other data that changes less frequently, Redis is usually used to store them;
This blog is a demonstration of how to store objects in a redis database to simulate the use of redis in real development.
(2) In this blog, the way of object serialization is JSON serialization;
(3) The examples in this blog are somewhat toy-like; How redis are used in practice needs to be understood in the actual project.
Goods class:
package com.imooc.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; } }
Description:
(1) There is nothing to say about Goods class, it is a general commodity information class;
............................................................
CacheSample class:
package com.imooc.jedis; import com.alibaba.fastjson.JSON; import redis.clients.jedis.Jedis; import java.util.ArrayList; import java.util.List; public class CacheSample { public CacheSample() { List<Goods> goodsList = new ArrayList<Goods>(); goodsList.add(new Goods(001,"Commodity 1","Description 1",101f)); goodsList.add(new Goods(002,"Commodity 2","Description 2",102f)); goodsList.add(new Goods(003,"Commodity 3","Description 3",103f)); //In practical projects, these data should all be queried from the database; Jedis jedis = new Jedis("1**.***.***.**4", 6380); try { jedis.auth("12345");//Enter a password for authorization jedis.select(4); for (Goods goods : goodsList) { String goodsJson = JSON.toJSONString(goods);//Serialize objects using Json String key = "goods:" + goods.getGoodsId(); jedis.set(key,goodsJson);//Store the serialized goods object [JSON string] in the Redis database; } } catch (Exception e) { e.printStackTrace(); }finally { jedis.close(); } } public static void main(String[] args) { new CacheSample(); } }
Explain:
(1) For the content of object serialization, you can refer to
Java serialization: [ Java Input/Output Stream 9: Object serialization (requires ObjectOutputStream [Object Output Stream] and ObjectInputStream [Object Input Stream] in byte streams)]
* JSON serialization: [ JSON6: Java: Object collection serialized into JSON (array); JSON (array) is deserialized into a collection of objects;]
● []
Here, we use JSON as the serialization method;
(2) FashJson is required for JSON serialization in Java; You can refer to FastJson [ JSON 4: Java: Introduction to handling JSON; FastJson download and install;],[JSON5: Java: Objects are converted to JSON strings; JSON string converted to object;],[JSON6: Java: Object collection serialized into JSON (array); JSON (array) is deserialized into a collection of objects;]
Here we introduce FastJson by Maven:
(3) Then, in the redis database, you can see the data you just saved;