redis.hostName=127.0.0.1 redis.port=6379 redis.database=3 redis.timeout=15000 redis.usePool=true redis.maxWaitMillis=3000 redis.maxIdle=1000 redis.minEvictableIdleTimeMillis=300000 redis.numTestsPerEvictionRun=3 redis.timeBetweenEvictionRunsMillis=60000 redis.maxTotal=10
import org.slf4j.Logger; import org.slf4j.LoggerFactory; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import redis.clients.jedis.Jedis; import redis.clients.jedis.JedisPool; import redis.clients.jedis.JedisPoolConfig; public class RedisCacheUtil { private static Logger logger = LoggerFactory.getLogger(RedisCacheUtil.class); private final JedisPoolConfig cfg; private final JedisPool jedis_pool; private RedisCacheUtil() { cfg = getConfig(); jedis_pool = getJedisPool(); } private JedisPool getJedisPool() { String host = PropertiesUtil.getInstance().findPropVal("redis.hostName"); int port = (int) PropertiesUtil.getInstance().findPropVal("redis.port", Integer.class); int timeout = (int) PropertiesUtil.getInstance().findPropVal("redis.timeout", Integer.class); int db=(int) PropertiesUtil.getInstance().findPropVal("redis.database", Integer.class); JedisPool pool = new JedisPool(cfg, host, port, timeout, null, db); return pool; } // private JedisConnectionFactory getJedisConnectionFactory() { // JedisConnectionFactory fac = new JedisConnectionFactory(cfg); // return fac; // } // // public ValueOperations<String, String> getOptionTool() { // JedisConnectionFactory fac = getJedisConnectionFactory(); // RedisTemplate<String, String> tmp = new RedisTemplate<>(); // tmp.setConnectionFactory(fac); // tmp.setKeySerializer(new StringRedisSerializer()); // tmp.setValueSerializer(new StringRedisSerializer()); // return tmp.opsForValue(); // } // // public ValueOperations<String, Object> getOptionToolBinary() { // JedisConnectionFactory fac = getJedisConnectionFactory(); // RedisTemplate<String, Object> tmp = new RedisTemplate<>(); // tmp.setConnectionFactory(fac); // tmp.setKeySerializer(new StringRedisSerializer()); // tmp.setValueSerializer(new GenericJackson2JsonRedisSerializer()); // return tmp.opsForValue(); // } private JedisPoolConfig getConfig() { JedisPoolConfig cfg = new JedisPoolConfig(); int maxIdle = (int) PropertiesUtil.getInstance().findPropVal("redis.maxIdle", Integer.class); int minEvictableIdleTimeMillis = (int) PropertiesUtil.getInstance() .findPropVal("redis.minEvictableIdleTimeMillis", Integer.class); int numTestsPerEvictionRun = (int) PropertiesUtil.getInstance().findPropVal("redis.numTestsPerEvictionRun", Integer.class); int timeBetweenEvictionRunsMillis = (int) PropertiesUtil.getInstance() .findPropVal("redis.timeBetweenEvictionRunsMillis", Integer.class); long maxWaitMillis= (long) PropertiesUtil.getInstance() .findPropVal("redis.maxWaitMillis", Long.class); int maxTotal= (int) PropertiesUtil.getInstance() .findPropVal("redis.maxTotal", Integer.class); cfg.setMaxIdle(maxIdle); cfg.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis); cfg.setNumTestsPerEvictionRun(numTestsPerEvictionRun); cfg.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis); cfg.setMaxWaitMillis(maxWaitMillis); cfg.setMaxTotal(maxTotal); return cfg; } private static final RedisCacheUtil instance = new RedisCacheUtil(); public static final RedisCacheUtil instance() { return instance; } /** * 将数据写入内存 * * @param key * @param val */ public void add(String key, String val) { Jedis jedis = jedis_pool.getResource(); jedis.set(key, val); jedis.close(); } /** * 获取数据 * * @param key * @param val */ public String get(String key) { Jedis jedis = jedis_pool.getResource(); String tmp = jedis.get(key); jedis.close(); if (tmp == null) { tmp = ""; } return tmp; } /** * 将内存数据写入磁盘,恢复时或者重启服务自动加载 */ public void writeToDB() { Jedis jedis = jedis_pool.getResource(); jedis.save(); jedis.close(); } /** * 追加内容 * * @param key * @param val */ public void append(String key, String val) { Jedis jedis = jedis_pool.getResource(); jedis.append(key, val); jedis.close(); } /** * 删除key,删除内存中的数据 * * @param key */ public void del(String key) { Jedis jedis = jedis_pool.getResource(); jedis.del(key); jedis.close(); } /** * 取除内存数据,将其转换为jsonArray * * @param key * @return */ public JSONArray getJSONArray(String key) { Jedis jedis = jedis_pool.getResource(); String val = jedis.get(key); jedis.close(); if (val == null || val.trim().length() == 0) { return new JSONArray(); } else { try { return JSONObject.parseObject(val).getJSONArray("data"); } catch (Exception e) { logger.error(e.getMessage()); return JSONArray.parseArray(val); } } } }