1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace rOpenDev\Google; |
4
|
|
|
|
5
|
|
|
use Exception; |
6
|
|
|
use rOpenDev\Cache\SimpleCacheFile; |
7
|
|
|
use rOpenDev\Cache\SimpleCacheFile as fileCache; |
8
|
|
|
|
9
|
|
|
trait CacheTrait |
10
|
|
|
{ |
11
|
|
|
/** @var mixed Contain the cache folder for SERP results * */ |
12
|
|
|
protected string $cacheFolder = '/tmp'; |
13
|
|
|
|
14
|
|
|
/** @var int Contain in seconds, the time cache is valid. Default 1 Day (86400). * */ |
15
|
|
|
protected int $cacheExpireTime = 86400; |
16
|
|
|
|
17
|
|
|
protected bool $previousRequestUsedCache = false; |
18
|
|
|
|
19
|
|
|
public function setCacheExpireTime($seconds): self |
20
|
|
|
{ |
21
|
|
|
$this->cacheExpireTime = $seconds; |
22
|
|
|
|
23
|
|
|
return $this; |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
public function previousRequestUsedCache(): bool |
27
|
|
|
{ |
28
|
|
|
return $this->previousRequestUsedCache; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @param string $cache |
33
|
|
|
*/ |
34
|
|
|
public function setCacheFolder(?string $cache): self |
35
|
|
|
{ |
36
|
|
|
$this->cacheFolder = null === $cache ? '' : $cache; |
37
|
|
|
|
38
|
|
|
return $this; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Delete cache file for a query ($q). |
43
|
|
|
* |
44
|
|
|
* @throws \Exception Where self::$cache is not set |
45
|
|
|
* |
46
|
|
|
* @return int Number of files deleted |
47
|
|
|
*/ |
48
|
|
|
public function deleteCacheFiles(): int |
49
|
|
|
{ |
50
|
|
|
if (! $this->cacheFolder) { |
51
|
|
|
throw new Exception('Cache Folder is not defined : you can\'t delete cache files'); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
return $this->getCacheManager()->getMaintener()->deleteCacheFilesByPrefix(); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Return cache instance. |
59
|
|
|
* |
60
|
|
|
* @throws \Exception if the cache (folder, self::$cache via self::setCache) is not set |
61
|
|
|
* |
62
|
|
|
* @return \rOpenDev\cache\SimpleCacheFile |
63
|
|
|
*/ |
64
|
|
|
protected function getCacheManager(): ?SimpleCacheFile |
65
|
|
|
{ |
66
|
|
|
if (! $this->cacheFolder) { |
67
|
|
|
return null; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
$cachePrefix = md5($this->q).'_'; |
71
|
|
|
|
72
|
|
|
return fileCache::instance($this->cacheFolder, $cachePrefix); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Return a cache key | A vérifier avec chrome. |
77
|
|
|
*/ |
78
|
|
|
protected function getCacheKey($url = null): string |
79
|
|
|
{ |
80
|
|
|
$url = $url ?: $this->generateGoogleSearchUrl(); |
|
|
|
|
81
|
|
|
$url = preg_replace('/&(gbv=1|sei=([a-z0-9]+)(&|$))/i', '&', $url); |
82
|
|
|
$url = trim($url, '&'); |
83
|
|
|
|
84
|
|
|
return sha1($this->page.(int) $this->mobile.':'.$url); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
protected function getCache($url): string |
88
|
|
|
{ |
89
|
|
|
if ($this->cacheFolder) { |
90
|
|
|
$source = $this->getCacheManager()->get($this->getCacheKey($url), $this->cacheExpireTime); |
91
|
|
|
$this->previousRequestUsedCache = true; |
92
|
|
|
|
93
|
|
|
return $source; |
|
|
|
|
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
return ''; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
public function setCache($url, $source): void |
100
|
|
|
{ |
101
|
|
|
if ($this->cacheFolder) { |
102
|
|
|
$this->getCacheManager()->set($this->getCacheKey($url), $source); |
103
|
|
|
$this->previousRequestUsedCache = false; |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|