Redis中并没有提供set(String key, Object obj)的方法,但提供了set(final byte[] key, final byte[] value) 的方法,可以通过把对象转化成字节数组的方式进行储存。
对象类
package redis; import java.io.Serializable; public class Goods implements Serializable { /** * */ private static final long serialVersionUID = 3870927632803751641L; private String name; private int num; private int price; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getNum() { return num; } public void setNum(int num) { this.num = num; } public int getPrice() { return price; } public void setPrice(int price) { this.price = price; } }
序列化工具类
package redis; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; /** * @author Administrator * */ public class SerializeUtil { 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; } 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; } }
redis保存对象扩展类
package redis; import redis.clients.jedis.Jedis; /** * * @author ajun * */ public class RedisClient { private static final String ip = "192.168.77.135"; private static final int port = 6379; protected static RedisClient redis = new RedisClient(); protected static Jedis jedis = new Jedis(ip, port);; static { } protected RedisClient() { System.out.println(" init Redis "); } public static RedisClient getInstance() { return redis; } /** set Object */ public String set(Object object, String key) { return jedis.set(key.getBytes(), SerializeUtil.serialize(object)); } /** get Object */ public Object get(String key) { byte[] value = jedis.get(key.getBytes()); return SerializeUtil.unserialize(value); } /** delete a key **/ public boolean del(String key) { return jedis.del(key.getBytes()) > 0; } }
测试类
package redis; import redis.clients.jedis.Jedis; public class SaveObjectTest { /** * Administrator * * @param args */ public static void main(String[] args) { // 操作单独的文本串 Jedis redis = new Jedis("192.168.77.135", 6379); redis.set("key", "value"); System.out.println(redis.get("key")); System.out.println(redis.del("key")); // 操作实体类对象 Goods good = new Goods(); // 这个Goods实体我就不写了啊 good.setName("洗衣机"); good.setNum(400); good.setPrice(19); redis.set("good".getBytes(), SerializeUtil.serialize(good)); byte[] value = redis.get("good".getBytes()); Object object = SerializeUtil.unserialize(value); if (object != null) { Goods goods = (Goods) object; System.out.println(goods.getName()); System.out.println(goods.getNum()); System.out.println(goods.getPrice()); } System.out.println(redis.del("good".getBytes())); // 操作实体类对象2(实际上和上面是一样的) String key = "goods-key"; Goods g = new Goods(); g.setName("电风扇--d"); g.setNum(200); String temp = RedisClient.getInstance().set(g, key); System.out.println(temp); Object o = RedisClient.getInstance().get(key); if (o != null) { Goods g1 = (Goods) o; System.out.println(g1.getName()); System.out.println(g1.getNum()); } System.out.println(RedisClient.getInstance().del(key)); } }