|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Nip\Records\Mapping\MappingManager; |
|
4
|
|
|
|
|
5
|
|
|
|
|
6
|
|
|
use DateInterval; |
|
7
|
|
|
use Nip\Cache\Stores\Repository; |
|
8
|
|
|
use Nip\Container\Container; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* Trait CanCache |
|
12
|
|
|
* @package Nip\Records\Mapping\MappingManager |
|
13
|
|
|
*/ |
|
14
|
|
|
trait CanCache |
|
15
|
|
|
{ |
|
16
|
|
|
protected $needsCaching = false; |
|
17
|
|
|
|
|
18
|
|
|
protected $cacheStore = null; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @param null $needCaching |
|
|
|
|
|
|
22
|
|
|
* @return bool |
|
23
|
|
|
*/ |
|
24
|
3 |
|
public function needsCaching($needCaching = null): bool |
|
25
|
|
|
{ |
|
26
|
3 |
|
if (is_bool($needCaching)) { |
|
|
|
|
|
|
27
|
3 |
|
$this->needsCaching = $needCaching; |
|
28
|
|
|
} |
|
29
|
3 |
|
return $this->needsCaching; |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
protected function checkInitFromCache() |
|
33
|
|
|
{ |
|
34
|
|
|
|
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
1 |
|
protected function initFromCache() |
|
38
|
|
|
{ |
|
39
|
1 |
|
$data = $this->cacheStore()->get('orm.mapping.data'); |
|
40
|
1 |
|
$this->repository->initFromCache($data); |
|
41
|
1 |
|
} |
|
42
|
|
|
|
|
43
|
|
|
|
|
44
|
1 |
|
protected function checkSaveCache() |
|
45
|
|
|
{ |
|
46
|
1 |
|
if ($this->needsCaching() !== true) { |
|
47
|
1 |
|
return; |
|
48
|
|
|
} |
|
49
|
|
|
$data = $this->repository->generateCache(); |
|
50
|
|
|
$this->cacheStore()->set('orm.mapping.data', $data, DateInterval::createFromDateString('10 years')); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* @return Repository |
|
56
|
|
|
*/ |
|
57
|
1 |
|
protected function cacheStore() |
|
58
|
|
|
{ |
|
59
|
1 |
|
if ($this->cacheStore === null) { |
|
60
|
1 |
|
$this->cacheStore = $this->generateCacheStore(); |
|
61
|
|
|
} |
|
62
|
1 |
|
return $this->cacheStore; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* @return Repository |
|
67
|
|
|
*/ |
|
68
|
1 |
|
protected function generateCacheStore() |
|
69
|
|
|
{ |
|
70
|
1 |
|
if (function_exists('app')) { |
|
71
|
|
|
return app('cache.store'); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
1 |
|
return Container::getInstance()->get('cache.store'); |
|
75
|
|
|
} |
|
76
|
|
|
} |
|
77
|
|
|
|