1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace ByJG\Cache\Engine; |
4
|
|
|
|
5
|
|
|
use ByJG\Cache\CacheEngineInterface; |
6
|
|
|
|
7
|
|
|
class SessionCacheEngine implements CacheEngineInterface |
8
|
|
|
{ |
9
|
|
|
|
10
|
|
|
protected $prefix = null; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* SessionCacheEngine constructor. |
14
|
|
|
* |
15
|
|
|
* @param string $prefix |
16
|
|
|
*/ |
17
|
|
|
public function __construct($prefix = 'cache') |
18
|
|
|
{ |
19
|
|
|
$this->prefix = $prefix; |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
|
23
|
|
|
protected function checkSession() |
24
|
|
|
{ |
25
|
|
|
if (session_status() == PHP_SESSION_NONE) { |
26
|
|
|
session_start(); |
27
|
|
|
} |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
protected function keyName($key) |
31
|
|
|
{ |
32
|
|
|
return $this->prefix . '-' . $key; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
public function append($key, $str) |
36
|
|
|
{ |
37
|
|
|
$this->checkSession(); |
38
|
|
|
|
39
|
|
|
$keyName = $this->keyName($key); |
40
|
|
|
|
41
|
|
|
$current = $this->get($keyName); |
42
|
|
|
if ($current === false) { |
43
|
|
|
$this->set($keyName, $str); |
|
|
|
|
44
|
|
|
} else { |
45
|
|
|
$this->set($keyName, $current . $str); |
|
|
|
|
46
|
|
|
} |
47
|
|
|
} |
48
|
|
|
|
49
|
|
View Code Duplication |
public function get($key, $ttl = 0) |
|
|
|
|
50
|
|
|
{ |
51
|
|
|
$this->checkSession(); |
52
|
|
|
|
53
|
|
|
$keyName = $this->keyName($key); |
54
|
|
|
|
55
|
|
|
if (isset($_SESSION[$keyName])) { |
56
|
|
|
return $_SESSION[$keyName]; |
57
|
|
|
} else { |
58
|
|
|
return null; |
59
|
|
|
} |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
public function lock($key) |
63
|
|
|
{ |
64
|
|
|
$this->checkSession(); |
65
|
|
|
|
66
|
|
|
// Nothing to implement here; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
View Code Duplication |
public function release($key) |
|
|
|
|
70
|
|
|
{ |
71
|
|
|
$this->checkSession(); |
72
|
|
|
|
73
|
|
|
$keyName = $this->keyName($key); |
74
|
|
|
|
75
|
|
|
if (isset($_SESSION[$keyName])) { |
76
|
|
|
unset($_SESSION[$keyName]); |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
public function set($key, $object, $ttl = 0) |
|
|
|
|
81
|
|
|
{ |
82
|
|
|
$this->checkSession(); |
83
|
|
|
|
84
|
|
|
$keyName = $this->keyName($key); |
85
|
|
|
$_SESSION[$keyName] = $object; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
public function unlock($key) |
89
|
|
|
{ |
90
|
|
|
$this->checkSession(); |
91
|
|
|
|
92
|
|
|
// Nothing to implement here; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
public function isAvailable() |
96
|
|
|
{ |
97
|
|
|
try { |
98
|
|
|
$this->checkSession(); |
99
|
|
|
return true; |
100
|
|
|
} catch (\Exception $ex) { |
101
|
|
|
return false; |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: