Redis数据操作
http://www.importnew.com/19321.html
https://blog.csdn.net/u013256816/article/details/51125842
另外纠正原文部分错误:
import redis.clients.jedis.Jedis; import redis.clients.jedis.JedisPool; import redis.clients.jedis.JedisPoolConfig; import java.util.HashMap; import java.util.Map; /** * @Author: feng * @Date: 2019/5/26 10:26 * @Description: */ public class RedisUtil { private RedisUtil(){} private static class RedisUtilHolder{ private static final RedisUtil instance = new RedisUtil(); } public static RedisUtil getInstance(){ return RedisUtilHolder.instance; } private static Map<String,JedisPool> maps = new HashMap<String,JedisPool>(); private static JedisPool getPool(String ip, int port){ String key = ip+":"+port; JedisPool pool = null; if(!maps.containsKey(key)) { JedisPoolConfig config = new JedisPoolConfig(); config.setMaxTotal(RedisConfig.MAX_ACTIVE); config.setMaxIdle(RedisConfig.MAX_IDLE); config.setMaxWaitMillis(RedisConfig.MAX_WAIT); config.setTestOnBorrow(true); config.setTestOnReturn(true); pool = new JedisPool(config,ip,port,RedisConfig.TIMEOUT); maps.put(key, pool); } else { pool = maps.get(key); } return pool; } public Jedis getJedis(String ip, int port) { Jedis jedis = null; int count = 0; while(jedis == null && count<RedisConfig.RETRY_NUM){ try { jedis = getPool(ip,port).getResource(); } catch (Exception e) { e.printStackTrace(); //加上计数 count++; } } return jedis; } public void closeJedis(Jedis jedis, String ip, int port){ if(jedis != null) { getPool(ip,port).getResource().close(); } } }