RedisCacheManager
import java.util.concurrent.ConcurrentHashMap;
import org.springframework.beans.factory.annotation.Value;import org.springframework.stereotype.Service;
import redis.clients.jedis.Jedis;import redis.clients.jedis.JedisPool;import redis.clients.jedis.JedisPoolConfig;
@Service("redisCacheManager")// @Slf4jpublic class RedisCacheManager { @Value("${redisdbtype}") private String redisdbtype; @Value("${redisdbnumber}") private String redisdbnumber; @Value("${host}") private String host; @Value("${port}") private int port; @Value("${timeout}") private int timeout; @Value("${passwords}") private String passwords; @Value("${maxtotal}") private String maxtotal; @Value("${maxidle}") private String maxidle; @Value("${minidle}") private String minidle; @Value("${maxwaitmillis}") private String maxwaitmillis; @Value("${testonborrow}") private String testonborrow; @Value("${testwhileidle}") private String testwhileidle; private static JedisPoolConfig poolConfig = null; // 保存不同的数据库连接 private ConcurrentHashMap redisPoolMap = new ConcurrentHashMap(); public ConcurrentHashMap getRedisPoolMap() { if (redisPoolMap.size() < 1) { initConfig(); initPoolMap(); } return redisPoolMap; } private void initConfig() { poolConfig = new JedisPoolConfig(); poolConfig.setTestOnBorrow(testwhileidle.equals("true") ? true : false); poolConfig.setTestWhileIdle(testonborrow.equals("true") ? true : false); poolConfig.setMaxIdle(Integer.parseInt(maxidle)); poolConfig.setMaxTotal(Integer.parseInt(maxtotal)); poolConfig.setMinIdle(Integer.parseInt(minidle)); poolConfig.setMaxWaitMillis(Integer.parseInt(maxwaitmillis)); } private void initPoolMap() { try { if (null != redisdbtype && null != redisdbnumber) { String[] dbs = redisdbtype.split(","); String[] numbers = redisdbnumber.split(","); for (int i = 0; i < dbs.length; i++) { // 得到redis连接池对象 JedisPool jedisPool = new JedisPool(poolConfig, host, port, timeout, passwords); // 存放不同redis数据库 redisPoolMap.put(dbs[i], new RedisCachePool(Integer.parseInt(numbers[i]), jedisPool)); } } } catch (Exception e) { // log.error("redisCacheManager初始化失败!" + e.getLocalizedMessage()); } } public Jedis getResource(RedisDataBaseType dbtypeName) { Jedis jedisResource = null; RedisCachePool pool = redisPoolMap.get(dbtypeName.toString()); if (pool != null) { jedisResource = pool.getResource(); } return jedisResource; } public void returnResource(RedisDataBaseType dbtypeName, Jedis jedis) { RedisCachePool pool = redisPoolMap.get(dbtypeName.toString()); if (pool != null) pool.returnResource(jedis); }}
JedisUtils
import redis.clients.jedis.Jedis;import redis.clients.jedis.JedisPool;import redis.clients.jedis.JedisPoolConfig;import redis.clients.jedis.exceptions.JedisException;
import java.util.List;import java.util.Map;
public class JedisUtils {
private static JedisPoolConfig config;
private static JedisPool jedisPool;
private static String password = "xxxxxxxxxx";
static { config = new JedisPoolConfig(); config.setMaxIdle(100); config.setMaxIdle(10); jedisPool = new JedisPool(config, "192.168.113.128", 6379); }
public static Jedis getJedis() { Jedis jedis = jedisPool.getResource(); jedis.auth(password); return jedis; }
public static void close(Jedis jedis) { //从源码可以分析得到,如果是使用连接池的形式,这个并非真正的close,而是把连接放回连接池中 if (jedis != null) { jedis.close(); } }
public static String get(String key) { Jedis jedis = null; try { jedis = getJedis(); return jedis.get(key); } catch (Exception e) { e.printStackTrace(); throw new JedisException(e.getMessage(),e); } finally { close(jedis); } }
public static void set(String key, String value) { Jedis jedis = null; try { jedis = getJedis(); jedis.set(key, value); } catch (Exception e) { e.printStackTrace(); throw new JedisException(e.getMessage(),e); } finally { close(jedis); } }
public static void set(String key, String value, long seconds) { Jedis jedis = null; try { jedis = getJedis(); / public static String blpop(int timeout,String key){ Jedis jedis = null; try { jedis = getJedis(); List list = jedis.blpop(timeout, key); return list.get(1); }catch (Exception e){ e.printStackTrace(); throw new JedisException(e.getMessage(),e); }finally { close(jedis); } }
public static String blpop(String key){ Jedis jedis = null; try { jedis = getJedis(); List list = jedis.blpop(0, key); return list.get(1); }catch (Exception e){ e.printStackTrace(); throw new JedisException(e.getMessage(),e); }finally { close(jedis); } }
public static void lpush(String key,String... value){ Jedis jedis = null; try { jedis = getJedis(); jedis.lpush(key,value); }catch (Exception e){ e.printStackTrace(); throw new JedisException(e.getMessage(),e); } }
public static String brpop(int timeout,String key){ Jedis jedis = null; try { jedis = getJedis(); List list = jedis.brpop(timeout, key); return list.get(1); }catch (Exception e){ e.printStackTrace(); throw new JedisException(e.getMessage(),e); }finally { close(jedis); } }
public static String brpop(String key){ Jedis jedis = null; try { jedis = getJedis(); List list = jedis.brpop(0, key); return list.get(1); }catch (Exception e){ e.printStackTrace(); throw new JedisException(e.getMessage(),e); }finally { close(jedis); } }
public static void rpush(String key,String... value){ Jedis jedis = null; try { jedis = getJedis(); jedis.rpush(key,value); }catch (Exception e){ e.printStackTrace(); throw new JedisException(e.getMessage(),e); } }
public static long ttl(String key) { Jedis jedis = null; try { jedis = getJedis(); return jedis.ttl(key); } catch (Exception e) { e.printStackTrace(); throw new JedisException(e.getMessage(),e); } finally { close(jedis); } }}
更多学习资料 请关注微信公众号



