• java使用Redis2--保存对象


    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));
        }
    }
  • 相关阅读:
    常见26个jquery使用技巧详解(比如禁止右键点击、隐藏文本框文字等)
    禁用页面及页面所有frame内的右键菜单
    JS模态窗口返回值兼容问题解决方案
    winform登录时,在密码框按下回车,直接登陆
    C#中实现邮件发送功能
    CTFHub-彩蛋(持续更新~)
    CTFHub技能树-目录遍历
    免密钥方式登陆配置
    Ansible_Day1
    Python_Day2_共享你的代码
  • 原文地址:https://www.cnblogs.com/yangmengdx3/p/4708249.html
Copyright © 2020-2023  润新知