Redis数据操作
Posted andrew520
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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(); } } }
以上是关于Redis数据操作的主要内容,如果未能解决你的问题,请参考以下文章