Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

分布式redis爬虫缓存清理支持 #893

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -153,4 +153,19 @@ public int getTotalRequestsCount(Task task) {
pool.returnResource(jedis);
}
}

/**
* clean all current task redis queue
* @param task current task
*/
public void flush(Task task) {
Jedis jedis = pool.getResource();
try {
jedis.del(getSetKey(task));
jedis.del(getQueueKey(task));
jedis.del(getItemKey(task));
} finally {
pool.returnResource(jedis);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,13 @@
public class RedisSchedulerTest {

private RedisScheduler redisScheduler;
private Task task;

@Before
public void setUp() {
redisScheduler = new RedisScheduler("localhost");
}

@Ignore("environment depended")
@Test
public void test() {
Task task = new Task() {
task = new Task() {
@Override
public String getUUID() {
return "1";
Expand All @@ -35,11 +32,32 @@ public Site getSite() {
return null;
}
};
}

@Ignore("environment depended")
@Test
public void test() {
Request request = new Request("http://www.ibm.com/developerworks/cn/java/j-javadev2-22/");
request.putExtra("1","2");
redisScheduler.push(request, task);
Request poll = redisScheduler.poll(task);
assertThat(poll).isEqualTo(request);

}

@Ignore("environment depended")
@Test
public void testFlush() {
Request request = new Request("http://www.baidu.com");
request.putExtra("1","2");
redisScheduler.push(request, task);
int totalRequestsCount = redisScheduler.getTotalRequestsCount(task);
assertThat(totalRequestsCount).isGreaterThan(0);
redisScheduler.flush(task);
totalRequestsCount = redisScheduler.getTotalRequestsCount(task);
assertThat(totalRequestsCount).isEqualTo(0);
int leftRequestsCount = redisScheduler.getLeftRequestsCount(task);
assertThat(leftRequestsCount).isEqualTo(0);

}
}