|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Nip\Database\Metadata; |
|
4
|
|
|
|
|
5
|
|
|
use Nip\Cache\Manager as CacheManager; |
|
6
|
|
|
use Nip\Database\Connections\Connection; |
|
7
|
|
|
use Nip\Database\Connections\HasConnectionTrait; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Class Cache |
|
11
|
|
|
* @package Nip\Database\Metadata |
|
12
|
|
|
*/ |
|
13
|
|
|
class Cache extends CacheManager |
|
14
|
|
|
{ |
|
15
|
|
|
protected $metadata; |
|
16
|
|
|
|
|
17
|
1 |
|
public function __construct() |
|
18
|
|
|
{ |
|
19
|
1 |
|
$this->setTtl(10 * 24 * 60 * 60); |
|
20
|
1 |
|
$this->setActive(true); |
|
21
|
1 |
|
} |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @param $table |
|
25
|
|
|
* @return mixed |
|
26
|
|
|
*/ |
|
27
|
|
|
public function describeTable($table) |
|
28
|
|
|
{ |
|
29
|
|
|
$cacheId = $this->getCacheId($table); |
|
30
|
|
|
|
|
31
|
|
|
return $this->get($cacheId); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @param $table |
|
36
|
|
|
* @return string |
|
37
|
|
|
*/ |
|
38
|
|
|
public function getCacheId($table) |
|
39
|
|
|
{ |
|
40
|
|
|
return $this->getConnection()->getDatabase() . '.' . $table; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @return Connection |
|
45
|
|
|
*/ |
|
46
|
|
|
public function getConnection() |
|
47
|
|
|
{ |
|
48
|
|
|
return $this->getMetadata()->getConnection(); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* @return Manager |
|
53
|
|
|
*/ |
|
54
|
|
|
public function getMetadata() |
|
55
|
|
|
{ |
|
56
|
|
|
return $this->metadata; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* @param $metadata |
|
61
|
|
|
* @return $this |
|
62
|
|
|
*/ |
|
63
|
|
|
public function setMetadata($metadata) |
|
64
|
|
|
{ |
|
65
|
|
|
$this->metadata = $metadata; |
|
66
|
|
|
|
|
67
|
|
|
return $this; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* @param $cacheId |
|
72
|
|
|
* @return mixed |
|
73
|
|
|
*/ |
|
74
|
|
|
public function get($cacheId) |
|
75
|
|
|
{ |
|
76
|
|
|
if (!$this->valid($cacheId)) { |
|
77
|
|
|
$this->reload($cacheId); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
return $this->getData($cacheId); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* @param $cacheId |
|
85
|
|
|
* @return mixed |
|
86
|
|
|
*/ |
|
87
|
|
|
public function reload($cacheId) |
|
88
|
|
|
{ |
|
89
|
|
|
$data = $this->generate($cacheId); |
|
90
|
|
|
if (is_array($data) && isset($data['fields'])) { |
|
91
|
|
|
return $this->saveData($cacheId, $data); |
|
92
|
|
|
} |
|
93
|
|
|
return false; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* @param $cacheId |
|
98
|
|
|
* @return mixed |
|
99
|
|
|
*/ |
|
100
|
|
|
public function generate($cacheId) |
|
101
|
|
|
{ |
|
102
|
|
|
$data = $this->getConnection()->describeTable($cacheId); |
|
103
|
|
|
$this->data[$cacheId] = $data; |
|
104
|
|
|
|
|
105
|
|
|
return $data; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* @return string |
|
110
|
|
|
*/ |
|
111
|
|
|
public function cachePath() |
|
112
|
|
|
{ |
|
113
|
|
|
return parent::cachePath() . '/db-metadata/'; |
|
114
|
|
|
} |
|
115
|
|
|
} |
|
116
|
|
|
|