-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathBlock.php
More file actions
141 lines (123 loc) · 2.58 KB
/
Block.php
File metadata and controls
141 lines (123 loc) · 2.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
<?php declare(strict_types=1);
namespace SHMCache;
/**
* Shared Memory Block
* Class Block
* @package SHMCache
*/
class Block extends shmop
{
/**
* Enable read & write
* @var bool
*/
protected $enable = true;
/**
* Is enable read & write
* @return bool
*/
public function isEnable(): bool
{
return $this->enable;
}
/**
* timeout seconds
* @var int
*/
protected $timeout = 0;
/**
* Get timeout seconds
* @return int
*/
public function getTimeout(): int
{
return $this->timeout;
}
/**
* Block constructor.
* @param int $timeout [optional] seconds
* @param int $id [optional]
*/
public function __construct($timeout = 0, $id = 0)
{
$this->timeout = $timeout;
parent::__construct($id > 0 ? $id : 0);
}
/**
* Hook to package the mixed data
* @param mixed $data
* @return mixed
*/
protected function toPack($data)
{
return $data;
}
/**
* Hook to unpacking the mixed data
* @param mixed $data
* @return mixed
*/
protected function toUnpack($data)
{
return $data;
}
/**
* Save $value by $key to cache
* @param string $key
* @param mixed $value
* @param int $seconds
* @return bool
* @throws \ErrorException
*/
public function save(string $key, $value, int $seconds = 0): bool
{
if (empty($key)) {
throw new \ErrorException('"key" should not be empty!');
} elseif (!$this->isEnable()) {
return false;
}
$data = $this->read();
if (!is_array($data)) {
$data = array();
}
$data[$key] = $value;
return parent::write($data, $seconds ? $seconds : $this->timeout);
}
/**
* Get the $value by $key from cache
* @param string $key
* @return bool|mixed
*/
public function get(string $key)
{
if (empty($key) || !$this->isEnable()) {
return false;
}
$value = $this->read();
if (!is_array($value) || !isset($value[$key])) {
return false;
}
return $value[$key];
}
/**
* Clean all cache data
*/
public function clean()
{
parent::clean();
}
/**
* Open to enable read & write
*/
public function open()
{
$this->enable = true;
}
/**
* Close to disable read and write
*/
public function close()
{
$this->enable = false;
}
}