首先引入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已经分配了maxActive个jedis实例,则此时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(); } }