package com.matrix.component.redis; import com.google.gson.Gson; import com.google.gson.GsonBuilder; import com.matrix.core.tools.LogUtil; import com.matrix.core.tools.StringUtils; import net.sf.json.JSONArray; import net.sf.json.JSONNull; import net.sf.json.JSONObject; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.CommandLineRunner; import redis.clients.jedis.Jedis; import redis.clients.jedis.JedisPool; import redis.clients.jedis.JedisPoolConfig; import java.util.*; import static org.springframework.util.Assert.notNull; /** * redis连接 * * @author jiangyouyao * @email 512061637@qq.com * @date 2019年2月25日 */ public class RedisClient implements CommandLineRunner { /** * redis服务器地址 */ @Value("${redis.hostname}") private String hostname; /** * redis 访问端口 */ @Value("${redis.port}") private int port; /** * 默认失效时间 */ @Value("${redis.timeout}") private int timeOut; @Value("${redis.password}") private String password; /** * 非切片连接池 */ private JedisPool jedisPool; /** * 数据库标号 */ @Value("${redis.database}") private int database; private RedisClient() { } /** * 保存一个对象,待超时时间 * * @author JIANGYOUYAO * @email 935090232@qq.com * @date 2018年1月19日 * @param key * @param bean * @param timeOut */ public void saveValue(String key, String value, int timeOut) { if (StringUtils.isNotBlank(value)) { Jedis jedis = getJedis(); jedis.setex(key, timeOut, value); jedis.close(); } } /** * 保存一个string,无过期时间 * * @author zhangh * @date 2018年7月19日 * @param key * @param value * @param timeOut */ public void saveValueForever(String key, String value) { if (StringUtils.isNotBlank(value)) { Jedis jedis = getJedis(); jedis.set(key, value); jedis.close(); } } /** * 删除一个对象 * * @author JIANGYOUYAO * @email 935090232@qq.com * @date 2018年1月19日 * @param key */ public void removeObject(String key) { Jedis jedis = getJedis(); jedis.del(key); jedis.close(); } /** * 根据key前缀批量删除缓存 * * @author wanglizhu * @email 935090232@qq.com * @date 2019年8月6日 * @param key */ public void batchDel(String key) { Jedis jedis = getJedis(); Set set = jedis.keys(key +"*"); Iterator it = set.iterator(); while(it.hasNext()){ String keyStr = it.next(); System.out.println(keyStr); jedis.del(keyStr); } jedis.close(); } /** * 保存一个对象使用默认超时时间 * * @author JIANGYOUYAO * @email 935090232@qq.com * @date 2018年1月19日 * @param key * @param jsonObject */ public void saveValue(String key, Object jsonObject) { if (jsonObject != null) { Jedis jedis = getJedis(); Gson g = new Gson(); jedis.setex(key, timeOut, g.toJson(jsonObject)); jedis.close(); } } /** * 保存一个对象,不设置过期时间 * * @author zhangh * @date 2018年7月19日 * @param key * @param jsonObject */ public void saveValueForever(String key, Object jsonObject) { if (jsonObject != null) { Jedis jedis = getJedis(); Gson g = new Gson(); jedis.set(key, g.toJson(jsonObject)); jedis.close(); } } public void saveMapValue(String key, Map jsonObject) { if (jsonObject != null) { Jedis jedis = getJedis(); Gson gson = new GsonBuilder().enableComplexMapKeySerialization().create(); jedis.setex(key, timeOut, gson.toJson(jsonObject)); jedis.close(); } } /** * * 此方法将ArrayList集合直接存储为一个字符串 * * @param key * 存储的名字 * @param list * 要存储的集合对象 */ public Boolean setArrayList(String key, List list) { if (list != null && key != null && key != "") { Jedis jedis = getJedis(); jedis.set(key, JSONArray.fromObject(list).toString()); jedis.close(); return true; } return false; } /** * 此方法将会把存在redis中的数据取出来,并封装成相应的Arraylist集合 * * @param key * 存储的名字 * @param beanClass * 要封装成为的javaBean * @return List */ public List getArraylist(String key, Class beanClass) { List list = new ArrayList(); if (StringUtils.isNotBlank(key) && beanClass != null) { Jedis jedis = getJedis(); JSONArray jsonArray = JSONArray.fromObject(jedis.get(key)); if(!JSONNull.getInstance().equals(jsonArray.get(0))){ String o = jsonArray.get(0).toString(); JSONArray myJsonArray = JSONArray.fromObject(o); for(int i = 0; i < myJsonArray.size(); i++){ Object object = JSONObject.toBean( (JSONObject) myJsonArray.get(i), beanClass); list.add(object); } } jedis.close(); } return list; } /** * 获取list * @param key * return :list */ public List getCachedValueList(String key) { List value = new ArrayList<>(); Jedis jedis = null; try { jedis = getJedis(); value = Collections.singletonList(jedis.lrange(key, 0, jedis.llen(key))); } catch (RuntimeException e) { LogUtil.error("获取redis 值 RuntimeException", e, ""); if (jedis != null) { jedis.close(); } } finally { if (jedis != null) { LogUtil.debug("释放jdedis 链接", jedis); jedis.close(); } } return value; } // public List saveList public String getCachedValue(String key) { String value = ""; Jedis jedis = null; try { jedis = getJedis(); value = jedis.get(key); } catch (RuntimeException e) { LogUtil.error("获取redis 值 RuntimeException", e, ""); if (jedis != null) { jedis.close(); } } finally { if (jedis != null) { LogUtil.debug("释放jdedis 链接", jedis); jedis.close(); } } return value; } public void resetExpire(String key) { Jedis jedis = getJedis(); jedis.expire(key, timeOut); jedis.close(); } /** * bean创建后调用 */ // @Override public void afterPropertiesSet() throws Exception { // 校验参数 notNull(hostname, "Property 'hostname' is required"); notNull(port, "Property 'port' is required"); JedisPoolConfig config = new JedisPoolConfig(); config.setMaxIdle(100); config.setMaxTotal(1000); config.setMaxWaitMillis(30000L); config.setTestOnBorrow(false); // 每次释放链接的数目 config.setNumTestsPerEvictionRun(20); jedisPool = new JedisPool(config, hostname, port, 30000, password, database); } private Jedis getJedis() { Jedis jedis = jedisPool.getResource(); return jedis; } @Override public void run(String... args) throws Exception { // 校验参数 notNull(hostname, "Property 'hostname' is required"); notNull(port, "Property 'port' is required"); JedisPoolConfig config = new JedisPoolConfig(); //控制一个pool最多有多少个状态为idle(空闲的)的jedis实例。 config.setMaxIdle(1000); //设置最大实例总数 config.setMaxTotal(1); config.setMaxWaitMillis(30000L); config.setTestOnBorrow(false); // 每次释放链接的数目 config.setNumTestsPerEvictionRun(20); config.setSoftMinEvictableIdleTimeMillis(1800000); jedisPool = new JedisPool(config, hostname, port, 30000, password, database); } }