package RedisObject;
import java.io.Serializable;
public class Person implements Serializable {
private static final long serialVersionUID = 1L;
private int id;
private String name;
public Person() {
}
public Person(int id, String name) {
super();
this.id = id;
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
package RedisObject;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
public class SerializeUtil {
/ **
* Serialization
*
* @param object
* @return
* /
public static byte [] serialize (Object object) {
ObjectOutputStream oos = null;
ByteArrayOutputStream baos = null;
try {
baos = new ByteArrayOutputStream ();
oos = new ObjectOutputStream (baos);
oos.writeObject (object);
byte [] bytes = baos.toByteArray ();
return bytes;
} catch (Exception e) {
}
return null;
}
/ **
* Deserialization
*
* @param bytes
* @return
* /
public static Object unserialize (byte [] bytes) {
ByteArrayInputStream bais = null;
try {
bais = new ByteArrayInputStream (bytes);
ObjectInputStream ois = new ObjectInputStream (bais);
return ois.readObject ();
} catch (Exception e) {
}
return null;
}
}
package RedisObject;
import redis.clients.jedis.Jedis;
public class PersonRedisTest {
private static Jedis jedis = null;
/ **
* Initialize Jedis object
*
* @throws Exception
* /
public PersonRedisTest () {
jedis = new Jedis ("127.0.0.1", 6379);
}
/ **
* Serialize write object, write Person object into Redis
*
* /
public void setObject () {
jedis.set ("person: 100" .getBytes (),
SerializeUtil.serialize (new Person (100, "zhangsan")));
jedis.set ("person: 101" .getBytes (),
SerializeUtil.serialize (new Person (101, "bruce")));
}
/ **
* Deserialize objects, get objects with Jedis
*
* /
public void getObject () {
byte [] data100 = jedis.get (("person: 100"). getBytes ());
Person person100 = (Person) SerializeUtil.unserialize (data100);
System.out.println (String.format ("person: 100-> id =% s, name =% s",
person100.getId (), person100.getName ()));
byte [] data101 = jedis.get (("person: 101"). getBytes ());
Person person101 = (Person) SerializeUtil.unserialize (data101);
System.out.println (String.format ("person: 101-> id =% s, name =% s",
person101.getId (), person101.getName ()));
}
public static void main (String [] args) {
PersonRedisTest rt = new PersonRedisTest ();
rt.setObject ();
rt.getObject ();
}
}
Executes the main function result:
To view data for Redis storage:
Three, Redis storage pictures
In the P22 (page 22nd) of the Redis Starter guide, there is a passage:
"The string type is the most important data type in Redis, and it can store strings in whatever form, including binary data. You can use it to store a user's mailbox, a JSON object, or even a picture. ”
Redis is a data structure type of server. is not simply a Key-value store. The keys in Redis are binary security (binary security means that data is secured during transmission, including encryption, etc.), so the contents of the keys should not contain spaces or newline characters. For example, "Hello World" and "Hello world\n" are wrong.
So how do you store images in Redis? Plainly. Image is a set of binary data, direct storage of binary data is certainly not possible, to achieve Redis storage binary data, then you have to implement the conversion beforehand.
The process of Base64 codec and Redis storage objects has been written in one or two. Then we can do this. Store images in Redis:
- Convert a picture into a string
(1) We can encode or decode base64 in the Redis storage image. In kv format, K is the normal string, and V is the Base64 encoding of the picture. Get (K) after the Base64 decoding can be;
(2) We can also store the network URL of the image in Redis or the local path path, the detailed implementation can make the picture itself to disk, and then in Redis with the network URL of the picture or the local path path to the value (V) storage.
-
Convert images to object objects
Store picture objects directly in Redis. Use the Java serialization/deserialization mechanism for processing implementations.
Redis storage Image [Base64/url/path]vs[object]