jedis存储对象(序列化)开发

首先引入jar包
 
<!-- https://mvnrepository.com/artifact/redis.clients/jedis -->
<dependency>
    <groupId>redis.clients</groupId>
    <artifactId>jedis</artifactId>
    <version>2.8.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/commons-pool/commons-pool -->
<dependency>
    <groupId>commons-pool</groupId>
    <artifactId>commons-pool</artifactId>
    <version>1.5.6</version>
</dependency>

//main方法
public static void main(String[] args) {
//        Jedis jedis = RedisUtil.getJedis();
        ;
        //权限认证
//        jedis.auth("admin");
        List<String> list=new ArrayList<String>();
        list.add("jgc");
        list.add("jgc1");
        JedisPool.set("list", list);//key-->name中放入了value-->xinxin
         System.out.print(JedisPool.get("list"));

    }
public class JedisPool {
    public interface JedisAction {
        Object action(Jedis jedis);
    }

    public interface JedisActionVoid {
        void action(Jedis jedis) throws IOException;
    }

    public static Object execute(JedisAction jedisAction) {
        Jedis jedis = RedisUtil.getJedis();
        Object o = null;
        try {
            o = jedisAction.action(jedis);
        } catch (Exception e) {

        } finally {
            jedis.close();
        }
        return o;
    }

    public static void execute(JedisActionVoid jedisAction) {
        Jedis jedis = RedisUtil.getJedis();

        try {
            jedisAction.action(jedis);
        } catch (Exception e) {

        } finally {
            jedis.close();
        }

    }



    public static byte[] getStream(Object o) {
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        ObjectOutputStream oout = null;
        try {
            oout = new ObjectOutputStream(outputStream);
            oout.writeObject(o);
            return outputStream.toByteArray();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (oout != null)
                try {
                    oout.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
        }
        return null;
    }

    public static Object getObject(byte[] bytes) throws IOException {
        ByteArrayInputStream bais;
        try {
            bais = new ByteArrayInputStream(bytes);
            ObjectInputStream ois = new ObjectInputStream(bais);
            return ois.readObject();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
    public static Object get(final String key) {
        return execute(new JedisAction() {
            public Object action(Jedis jedis) {
                Object o = null;
                byte[] vs = jedis.get(key.getBytes());
                if (null != vs) {
                    try {
                        o = getObject(vs);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }

                return o;
            }
        });
    }
    public static void set(final String key, final Object value) {

        execute(new JedisActionVoid() {
            public void action(Jedis jedis) throws IOException {
                if (key == null) {
                    return;
                }
                jedis.set(key.getBytes(), getStream(value));
            }
        });
    }

    public static void setExpired(final String key, final int time) {
        execute(new JedisActionVoid() {
            public void action(Jedis jedis) {
                jedis.expire(key, time);
            }
        });
    }
}
package com.netease.test.controller;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

/**
 * Created by hzjiaoguangcai on 2017/6/21.
 */
public class RedisUtil {
    //Redis服务器IP
    private static String ADDR = "localhost";

    //Redis的端口号
    private static int PORT = 6379;

    //访问密码
    private static String AUTH = "ac123";

    //可用连接实例的最大数目,默认值为8    //如果赋值为-1,则表示不限制;如果pool已经分配了maxActivejedis实例,则此时pool的状态为exhausted(耗尽)    private static int MAX_ACTIVE = 1024;

    //控制一个pool最多有多少个状态为idle(空闲的)jedis实例,默认值也是8    private static int MAX_IDLE = 200;

    //等待可用连接的最大时间,单位毫秒,默认值为-1,表示永不超时。如果超过等待时间,则直接抛出JedisConnectionException    private static int MAX_WAIT = 10000;

    private static int TIMEOUT = 10000;

    //borrow一个jedis实例时,是否提前进行validate操作;如果为true,则得到的jedis实例均是可用的;
    private static boolean TEST_ON_BORROW = true;

    private static JedisPool jedisPool = null;

    /**
     * 初始化Redis连接池
     */
    static {
        try {
            JedisPoolConfig config = new JedisPoolConfig();
            config.setMaxTotal(MAX_ACTIVE);
            config.setMaxIdle(MAX_IDLE);
            config.setTestOnBorrow(TEST_ON_BORROW);
            jedisPool = new JedisPool(config, ADDR, PORT, TIMEOUT);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 获取Jedis实例
     * @return
     */
    public synchronized static Jedis getJedis() {
        try {
            if (jedisPool != null) {
                Jedis resource = jedisPool.getResource();
                return resource;
            } else {
                return null;
            }
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 释放jedis资源
     * @param jedis
     */
    public static void returnResource(final Jedis jedis) {
        jedis.close();
    }
}

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值