-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
CacheCrawlQueue.php
114 lines (95 loc) · 2.75 KB
/
CacheCrawlQueue.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
<?php
namespace Spekulatius\SpatieCrawlerToolkit\Queues;
use Spatie\Crawler\CrawlUrl;
use Spatie\Crawler\CrawlQueues\ArrayCrawlQueue;
use Spatie\Crawler\CrawlQueues\CrawlQueue;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Log;
class CacheCrawlQueue extends ArrayCrawlQueue implements CrawlQueue
{
/**
* Key to identify the cache section for instance.
*
* @var string
*/
protected $base_cache_key = null;
/**
* Define expiry of cached URLs.
*
* @var int
*/
protected $ttl = null;
/**
* Defines an instance of the CacheQueue
*
* @param string $identifier
* @param ?int $ttl
*/
public function __construct(string $identifier, ?int $ttl = null)
{
// Store the information for later usage.
$this->base_cache_key = md5($identifier);
$this->ttl = $ttl ?? config('crawler-toolkit.cache_ttl', 3600);
// Load the previous data, if any.
$this->urls = Cache::get($this->base_cache_key . '_urls', []);
$this->pendingUrls = Cache::get($this->base_cache_key . '_pendingUrls', []);
}
/**
* Updates the cached data.
*
* @return void
*/
protected function writeToCache(): void
{
Cache::put(
$this->base_cache_key . '_urls',
$this->urls,
now()->addSeconds($this->ttl)
);
Cache::put(
$this->base_cache_key . '_pendingUrls',
$this->pendingUrls,
now()->addSeconds($this->ttl)
);
}
/**
* Adds a new URL to the queue (and cache).
*
* @param CrawlUrl $crawlUrl
* @return CrawlQueue
*/
public function add(CrawlUrl $crawlUrl): CrawlQueue
{
$urlString = (string) $crawlUrl->url;
if (! isset($this->urls[$urlString])) {
$crawlUrl->setId($urlString);
$this->urls[$urlString] = $crawlUrl;
$this->pendingUrls[$urlString] = $crawlUrl;
$this->writeToCache();
}
return $this;
}
/**
* Marks the given URL as processed
*
* @param CrawlUrl $crawlUrl
* @return void
*/
public function markAsProcessed(CrawlUrl $crawlUrl): void
{
$urlString = (string) $crawlUrl->url;
unset($this->pendingUrls[$urlString]);
$this->writeToCache();
}
public function getPendingUrl(): ?CrawlUrl
{
// Any URLs left?
if (count($this->pendingUrls) > 0) {
// Get a random ID, the key for it and return the URLs.
$random_id = random_int(0, count($this->pendingUrls) - 1);
$key = array_keys($this->pendingUrls)[$random_id];
return $this->pendingUrls[$key];
}
return null;
}
}