|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* This file is part of the SVN-Buddy library. |
|
4
|
|
|
* For the full copyright and license information, please view |
|
5
|
|
|
* the LICENSE file that was distributed with this source code. |
|
6
|
|
|
* |
|
7
|
|
|
* @copyright Alexander Obuhovich <[email protected]> |
|
8
|
|
|
* @link https://github.com/console-helpers/svn-buddy |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace ConsoleHelpers\SVNBuddy\Database; |
|
12
|
|
|
|
|
13
|
|
|
|
|
14
|
|
|
use Aura\Sql\ExtendedPdoInterface; |
|
15
|
|
|
|
|
16
|
|
|
class DatabaseCache |
|
17
|
|
|
{ |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Database cache. |
|
21
|
|
|
* |
|
22
|
|
|
* @var array |
|
23
|
|
|
*/ |
|
24
|
|
|
private $_cache = array(); |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* Database. |
|
28
|
|
|
* |
|
29
|
|
|
* @var ExtendedPdoInterface |
|
30
|
|
|
*/ |
|
31
|
|
|
private $_database; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* Creates cache instance. |
|
35
|
|
|
* |
|
36
|
|
|
* @param ExtendedPdoInterface $database Database. |
|
37
|
|
|
*/ |
|
38
|
167 |
|
public function __construct(ExtendedPdoInterface $database) |
|
39
|
|
|
{ |
|
40
|
167 |
|
$this->_database = $database; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Adds table to caching. |
|
45
|
|
|
* |
|
46
|
|
|
* @param string $table Table. |
|
47
|
|
|
* |
|
48
|
|
|
* @return void |
|
49
|
|
|
*/ |
|
50
|
167 |
|
public function cacheTable($table) |
|
51
|
|
|
{ |
|
52
|
167 |
|
if ( !isset($this->_cache[$table]) ) { |
|
53
|
167 |
|
$this->_cache[$table] = array(); |
|
54
|
|
|
} |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* Gets cached data. |
|
59
|
|
|
* |
|
60
|
|
|
* @param string $table Table. |
|
61
|
|
|
* @param mixed $cache_key Key. |
|
62
|
|
|
* @param string|null $sql Fallback sql used to populate cache on the fly. |
|
63
|
|
|
* @param array $values Fallback values used together with above sql. |
|
64
|
|
|
* |
|
65
|
|
|
* @return array|boolean |
|
66
|
|
|
*/ |
|
67
|
84 |
|
public function getFromCache($table, $cache_key, $sql = null, array $values = array()) |
|
68
|
|
|
{ |
|
69
|
84 |
|
if ( isset($this->_cache[$table][$cache_key]) ) { |
|
70
|
49 |
|
return $this->_cache[$table][$cache_key]; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
83 |
|
if ( isset($sql) ) { |
|
74
|
81 |
|
$result = $this->_database->fetchOne($sql, $values); |
|
75
|
|
|
|
|
76
|
81 |
|
if ( $result !== false ) { |
|
|
|
|
|
|
77
|
54 |
|
$this->setIntoCache($table, $cache_key, $result); |
|
78
|
|
|
|
|
79
|
54 |
|
return $this->_cache[$table][$cache_key]; |
|
80
|
|
|
} |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
78 |
|
return false; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* Gets cached data. |
|
88
|
|
|
* |
|
89
|
|
|
* @param string $table Table. |
|
90
|
|
|
* @param mixed $cache_key Key. |
|
91
|
|
|
* @param array $data Data. |
|
92
|
|
|
* |
|
93
|
|
|
* @return void |
|
94
|
|
|
*/ |
|
95
|
65 |
|
public function setIntoCache($table, $cache_key, array $data) |
|
96
|
|
|
{ |
|
97
|
65 |
|
if ( isset($this->_cache[$table][$cache_key]) ) { |
|
98
|
46 |
|
$this->_cache[$table][$cache_key] = array_replace($this->_cache[$table][$cache_key], $data); |
|
99
|
|
|
} |
|
100
|
|
|
else { |
|
101
|
65 |
|
$this->_cache[$table][$cache_key] = $data; |
|
102
|
|
|
} |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* Clears cache. |
|
107
|
|
|
* |
|
108
|
|
|
* @return void |
|
109
|
|
|
*/ |
|
110
|
1 |
|
public function clear() |
|
111
|
|
|
{ |
|
112
|
1 |
|
$tables = array_keys($this->_cache); |
|
113
|
1 |
|
$this->_cache = array(); |
|
114
|
|
|
|
|
115
|
1 |
|
foreach ( $tables as $table_name ) { |
|
116
|
1 |
|
$this->cacheTable($table_name); |
|
117
|
|
|
} |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
} |
|
121
|
|
|
|