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