Commit 3995d426 by 马黎滨

redis连接设置

parent 1caeb3fb
...@@ -41,6 +41,8 @@ import org.springframework.beans.factory.annotation.Autowired; ...@@ -41,6 +41,8 @@ import org.springframework.beans.factory.annotation.Autowired;
public class WeiboHotSearchCrawler { public class WeiboHotSearchCrawler {
private static HttpBoot httpBoot = new HttpBoot.Builder().retryTimes(3).build(); private static HttpBoot httpBoot = new HttpBoot.Builder().retryTimes(3).build();
private static RedisDao redisDao = new RedisDao();
/** /**
* @Title: weiboHotSearchTest * @Title: weiboHotSearchTest
* @author hero * @author hero
...@@ -112,7 +114,6 @@ public class WeiboHotSearchCrawler { ...@@ -112,7 +114,6 @@ public class WeiboHotSearchCrawler {
* @return void 返回类型 * @return void 返回类型
*/ */
public static List<HotSearchList> weiboHotSearchByPhone(Date date){ public static List<HotSearchList> weiboHotSearchByPhone(Date date){
RedisDao redisDao = new RedisDao();
String url = "https://m.weibo.cn/api/container/getIndex?containerid=106003type%3D25%26t%3D3%26disable_hot%3D1%26filter_type%3Drealtimehot&title=%E5%BE%AE%E5%8D%9A%E7%83%AD%E6%90%9C&extparam=pos%3D0_0%26mi_cid%3D100103%26cate%3D10103%26filter_type%3Drealtimehot%26c_type%3D30&luicode=10000011&lfid=231583"; String url = "https://m.weibo.cn/api/container/getIndex?containerid=106003type%3D25%26t%3D3%26disable_hot%3D1%26filter_type%3Drealtimehot&title=%E5%BE%AE%E5%8D%9A%E7%83%AD%E6%90%9C&extparam=pos%3D0_0%26mi_cid%3D100103%26cate%3D10103%26filter_type%3Drealtimehot%26c_type%3D30&luicode=10000011&lfid=231583";
Map<String,String> headerMap = new HashMap<>(); Map<String,String> headerMap = new HashMap<>();
headerMap.put("User-Agent", "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.142 Safari/537.36"); headerMap.put("User-Agent", "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.142 Safari/537.36");
...@@ -168,7 +169,6 @@ public class WeiboHotSearchCrawler { ...@@ -168,7 +169,6 @@ public class WeiboHotSearchCrawler {
continue; continue;
} }
// } // }
redisDao.closeRedis();
return result; return result;
} catch (Exception e) { } catch (Exception e) {
log.error("解析微博时热搜时出现解析错误,数据不是json结构", e); log.error("解析微博时热搜时出现解析错误,数据不是json结构", e);
...@@ -177,7 +177,6 @@ public class WeiboHotSearchCrawler { ...@@ -177,7 +177,6 @@ public class WeiboHotSearchCrawler {
log.info("解析微博时热搜时出现解析错误,页面结构有问题"); log.info("解析微博时热搜时出现解析错误,页面结构有问题");
} }
} }
redisDao.closeRedis();
return Collections.emptyList(); return Collections.emptyList();
} }
......
...@@ -6,6 +6,7 @@ import redis.clients.jedis.Jedis; ...@@ -6,6 +6,7 @@ import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool; import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig; import redis.clients.jedis.JedisPoolConfig;
import java.util.HashSet;
import java.util.Set; import java.util.Set;
/** /**
...@@ -14,16 +15,15 @@ import java.util.Set; ...@@ -14,16 +15,15 @@ import java.util.Set;
@Log4j2 @Log4j2
public class RedisDao { public class RedisDao {
private Jedis jedis; private JedisPool jedisPool;
public RedisDao(){ public RedisDao(){
JedisPoolConfig poolConfig = new JedisPoolConfig(); JedisPoolConfig poolConfig = new JedisPoolConfig();
poolConfig.setMaxIdle(RedisConfig.redisMaxIdle); poolConfig.setMaxIdle(RedisConfig.redisMaxIdle);
poolConfig.setMaxTotal(RedisConfig.redisMaxTotal); poolConfig.setMaxTotal(RedisConfig.redisMaxTotal);
poolConfig.setMinIdle(RedisConfig.redisMinIdle); poolConfig.setMinIdle(RedisConfig.redisMinIdle);
JedisPool jedisPool = new JedisPool(poolConfig,RedisConfig.redisHost,RedisConfig.redisPort,RedisConfig.redisTimeout); poolConfig.setMaxWaitMillis(RedisConfig.redisTimeout);
jedis = jedisPool.getResource(); jedisPool = new JedisPool(poolConfig,RedisConfig.redisHost,RedisConfig.redisPort);
jedis.select(RedisConfig.redisDataBase);
} }
/** /**
...@@ -32,7 +32,17 @@ public class RedisDao { ...@@ -32,7 +32,17 @@ public class RedisDao {
* @param value * @param value
*/ */
public void setRedisData(String key, String value){ public void setRedisData(String key, String value){
jedis.set(key,value); Jedis jedis = jedisPool.getResource();
jedis.select(RedisConfig.redisDataBase);
try {
jedis.set(key, value);
} catch (Exception e){
e.printStackTrace();
}finally {
if(jedis != null){
jedis.close();
}
}
} }
/** /**
...@@ -41,7 +51,19 @@ public class RedisDao { ...@@ -41,7 +51,19 @@ public class RedisDao {
* @return * @return
*/ */
public String getRedisData(String key){ public String getRedisData(String key){
return jedis.get(key); Jedis jedis = jedisPool.getResource();
jedis.select(RedisConfig.redisDataBase);
String s = null;
try {
s = jedis.get(key);
}catch (Exception e){
e.printStackTrace();
}finally {
if(jedis != null){
jedis.close();
}
}
return s;
} }
/** /**
...@@ -50,7 +72,17 @@ public class RedisDao { ...@@ -50,7 +72,17 @@ public class RedisDao {
* @param value * @param value
*/ */
public void addDataToSet(String key, String value){ public void addDataToSet(String key, String value){
jedis.sadd(key,value); Jedis jedis = jedisPool.getResource();
jedis.select(RedisConfig.redisDataBase);
try {
jedis.sadd(key, value);
}catch (Exception e){
e.printStackTrace();
}finally {
if(jedis != null){
jedis.close();
}
}
} }
/** /**
...@@ -59,7 +91,19 @@ public class RedisDao { ...@@ -59,7 +91,19 @@ public class RedisDao {
* @return * @return
*/ */
public Set<String> getRedisSetData(String key){ public Set<String> getRedisSetData(String key){
return jedis.smembers(key); Jedis jedis = jedisPool.getResource();
jedis.select(RedisConfig.redisDataBase);
Set<String> set = new HashSet<>();
try {
set = jedis.smembers(key);
}catch (Exception e){
e.printStackTrace();
}finally {
if(jedis != null){
jedis.close();
}
}
return set;
} }
/** /**
...@@ -67,13 +111,16 @@ public class RedisDao { ...@@ -67,13 +111,16 @@ public class RedisDao {
* @param key * @param key
*/ */
public void removeRedis(String key){ public void removeRedis(String key){
jedis.del(key); Jedis jedis = jedisPool.getResource();
} jedis.select(RedisConfig.redisDataBase);
try {
/** jedis.del(key);
* 关闭redis连接 }catch (Exception e){
*/ e.printStackTrace();
public void closeRedis(){ }finally {
jedis.close(); if(jedis != null){
jedis.close();
}
}
} }
} }
...@@ -29,6 +29,8 @@ import java.util.*; ...@@ -29,6 +29,8 @@ import java.util.*;
public class GatherTimer { public class GatherTimer {
private Logger logger = LoggerFactory.getLogger(GatherTimer.class); private Logger logger = LoggerFactory.getLogger(GatherTimer.class);
private RedisDao redisDao = new RedisDao();
/** 知乎数码子分类 */ /** 知乎数码子分类 */
private String DIGITAL = "digital"; private String DIGITAL = "digital";
/** 知乎国际子分类 */ /** 知乎国际子分类 */
...@@ -57,7 +59,6 @@ public class GatherTimer { ...@@ -57,7 +59,6 @@ public class GatherTimer {
@Scheduled(cron = "45 0/10 * * * ? ") @Scheduled(cron = "45 0/10 * * * ? ")
public void updateWeiBo(){ public void updateWeiBo(){
logger.info("微博热搜导语更新..."); logger.info("微博热搜导语更新...");
RedisDao redisDao = new RedisDao();
HotSearchCacheDAO hotSearchCacheDAO = new HotSearchCacheDAO(); HotSearchCacheDAO hotSearchCacheDAO = new HotSearchCacheDAO();
Set<String> hotSearchIdSet = redisDao.getRedisSetData(RedisConfig.WEIBO_HOTSEARCHIDS); Set<String> hotSearchIdSet = redisDao.getRedisSetData(RedisConfig.WEIBO_HOTSEARCHIDS);
redisDao.removeRedis(RedisConfig.WEIBO_HOTSEARCHIDS); redisDao.removeRedis(RedisConfig.WEIBO_HOTSEARCHIDS);
...@@ -73,7 +74,6 @@ public class GatherTimer { ...@@ -73,7 +74,6 @@ public class GatherTimer {
ZhiWeiTools.sleep(3000L); ZhiWeiTools.sleep(3000L);
} }
} }
redisDao.closeRedis();
logger.info("微博热搜导语更新结束..."); logger.info("微博热搜导语更新结束...");
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment