Commit c267bf23 by chenweitao

Merge branch 'mlbWork' into 'master'

redis连接设置

See merge request !55
parents cc442545 3995d426
......@@ -41,6 +41,8 @@ import org.springframework.beans.factory.annotation.Autowired;
public class WeiboHotSearchCrawler {
private static HttpBoot httpBoot = new HttpBoot.Builder().retryTimes(3).build();
private static RedisDao redisDao = new RedisDao();
/**
* @Title: weiboHotSearchTest
* @author hero
......@@ -112,7 +114,6 @@ public class WeiboHotSearchCrawler {
* @return void 返回类型
*/
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";
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");
......@@ -168,7 +169,6 @@ public class WeiboHotSearchCrawler {
continue;
}
// }
redisDao.closeRedis();
return result;
} catch (Exception e) {
log.error("解析微博时热搜时出现解析错误,数据不是json结构", e);
......@@ -177,7 +177,6 @@ public class WeiboHotSearchCrawler {
log.info("解析微博时热搜时出现解析错误,页面结构有问题");
}
}
redisDao.closeRedis();
return Collections.emptyList();
}
......
......@@ -6,6 +6,7 @@ import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
import java.util.HashSet;
import java.util.Set;
/**
......@@ -14,16 +15,15 @@ import java.util.Set;
@Log4j2
public class RedisDao {
private Jedis jedis;
private JedisPool jedisPool;
public RedisDao(){
JedisPoolConfig poolConfig = new JedisPoolConfig();
poolConfig.setMaxIdle(RedisConfig.redisMaxIdle);
poolConfig.setMaxTotal(RedisConfig.redisMaxTotal);
poolConfig.setMinIdle(RedisConfig.redisMinIdle);
JedisPool jedisPool = new JedisPool(poolConfig,RedisConfig.redisHost,RedisConfig.redisPort,RedisConfig.redisTimeout);
jedis = jedisPool.getResource();
jedis.select(RedisConfig.redisDataBase);
poolConfig.setMaxWaitMillis(RedisConfig.redisTimeout);
jedisPool = new JedisPool(poolConfig,RedisConfig.redisHost,RedisConfig.redisPort);
}
/**
......@@ -32,7 +32,17 @@ public class RedisDao {
* @param 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 {
* @return
*/
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 {
* @param 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 {
* @return
*/
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 {
* @param key
*/
public void removeRedis(String key){
jedis.del(key);
}
/**
* 关闭redis连接
*/
public void closeRedis(){
jedis.close();
Jedis jedis = jedisPool.getResource();
jedis.select(RedisConfig.redisDataBase);
try {
jedis.del(key);
}catch (Exception e){
e.printStackTrace();
}finally {
if(jedis != null){
jedis.close();
}
}
}
}
......@@ -29,6 +29,8 @@ import java.util.*;
public class GatherTimer {
private Logger logger = LoggerFactory.getLogger(GatherTimer.class);
private RedisDao redisDao = new RedisDao();
/** 知乎数码子分类 */
private String DIGITAL = "digital";
/** 知乎国际子分类 */
......@@ -57,7 +59,6 @@ public class GatherTimer {
@Scheduled(cron = "45 0/10 * * * ? ")
public void updateWeiBo(){
logger.info("微博热搜导语更新...");
RedisDao redisDao = new RedisDao();
HotSearchCacheDAO hotSearchCacheDAO = new HotSearchCacheDAO();
Set<String> hotSearchIdSet = redisDao.getRedisSetData(RedisConfig.WEIBO_HOTSEARCHIDS);
redisDao.removeRedis(RedisConfig.WEIBO_HOTSEARCHIDS);
......@@ -73,7 +74,6 @@ public class GatherTimer {
ZhiWeiTools.sleep(3000L);
}
}
redisDao.closeRedis();
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