|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Ion |
|
4
|
|
|
* |
|
5
|
|
|
* Building blocks for web development |
|
6
|
|
|
* |
|
7
|
|
|
* @package Ion |
|
8
|
|
|
* @author Timothy J. Warren |
|
9
|
|
|
* @copyright Copyright (c) 2015 - 2016 |
|
10
|
|
|
* @license MIT |
|
11
|
|
|
*/ |
|
12
|
|
|
|
|
13
|
|
|
namespace Aviat\Ion\Cache\Driver; |
|
14
|
|
|
|
|
15
|
|
|
use Aviat\Ion\Di\ContainerInterface; |
|
16
|
|
|
use Aviat\Ion\Json; |
|
17
|
|
|
use Aviat\AnimeClient\Model\DB; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Driver for caching via a traditional SQL database |
|
21
|
|
|
*/ |
|
22
|
|
|
class SQLDriver extends DB implements \Aviat\Ion\Cache\CacheDriverInterface { |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* The query builder object |
|
26
|
|
|
* @var object $db |
|
27
|
|
|
*/ |
|
28
|
|
|
protected $db; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* Create the driver object |
|
32
|
|
|
*/ |
|
33
|
|
|
public function __construct(ContainerInterface $container) |
|
34
|
|
|
{ |
|
35
|
|
|
parent::__construct($container); |
|
36
|
|
|
$this->db = \Query($this->db_config['collection']); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* Retreive a value from the cache backend |
|
41
|
|
|
* |
|
42
|
|
|
* @param string $key |
|
43
|
|
|
* @return mixed |
|
44
|
|
|
*/ |
|
45
|
|
|
public function get($key) |
|
46
|
|
|
{ |
|
47
|
|
|
$query = $this->db->select('value') |
|
48
|
|
|
->from('cache') |
|
49
|
|
|
->where('key', $key) |
|
50
|
|
|
->get(); |
|
51
|
|
|
|
|
52
|
|
|
$row = $query->fetch(\PDO::FETCH_ASSOC); |
|
53
|
|
|
if ( ! empty($row)) |
|
54
|
|
|
{ |
|
55
|
|
|
return unserialize($row['value']); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
return NULL; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* Set a cached value |
|
63
|
|
|
* |
|
64
|
|
|
* @param string $key |
|
65
|
|
|
* @param mixed $value |
|
66
|
|
|
* @return CacheDriverInterface |
|
|
|
|
|
|
67
|
|
|
*/ |
|
68
|
|
|
public function set($key, $value) |
|
69
|
|
|
{ |
|
70
|
|
|
$this->db->set([ |
|
71
|
|
|
'key' => $key, |
|
72
|
|
|
'value' => serialize($value), |
|
73
|
|
|
]); |
|
74
|
|
|
|
|
75
|
|
|
$this->db->insert('cache'); |
|
76
|
|
|
|
|
77
|
|
|
return $this; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* Invalidate a cached value |
|
82
|
|
|
* |
|
83
|
|
|
* @param string $key |
|
84
|
|
|
* @return CacheDriverInterface |
|
|
|
|
|
|
85
|
|
|
*/ |
|
86
|
|
|
public function invalidate($key) |
|
87
|
|
|
{ |
|
88
|
|
|
$this->db->where('key', $key) |
|
89
|
|
|
->delete('cache'); |
|
90
|
|
|
|
|
91
|
|
|
return $this; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* Clear the contents of the cache |
|
96
|
|
|
* |
|
97
|
|
|
* @return void |
|
98
|
|
|
*/ |
|
99
|
|
|
public function invalidateAll() |
|
100
|
|
|
{ |
|
101
|
|
|
$this->db->truncate('cache'); |
|
102
|
|
|
} |
|
103
|
|
|
} |
|
104
|
|
|
// End of SQLDriver.php |
This check compares the return type specified in the
@returnannotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.