Repository navigation
Expand file tree
/
Copy pathLockManager.php
More file actions
63 lines (54 loc) · 1.58 KB
/
Copy pathLockManager.php
File metadata and controls
63 lines (54 loc) · 1.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
<?php
declare(strict_types=1);
/**
* This file is part of Hyperf.
*
* @link https://www.hyperf.io
* @document https://hyperf.wiki
* @contact [email protected]
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
*/
namespace Hyperf\Memory;
use RuntimeException;
use Swoole\Lock;
class LockManager
{
/**
* A container that use to store Lock.
*/
private static array $container = [];
/**
* You should initialize a Lock with the identifier before use it.
*/
public static function initialize(string $identifier, int $type = SWOOLE_RWLOCK, string $filename = ''): void
{
static::$container[$identifier] = new Lock($type, $filename);
}
/**
* Get an initialized Lock from container by the identifier.
*
* @throws RuntimeException when the Lock with the identifier has not initialization
*/
public static function get(string $identifier): Lock
{
if (! isset(static::$container[$identifier])) {
throw new RuntimeException('The Lock has not initialization yet.');
}
return static::$container[$identifier];
}
/**
* Check if a lock with the given identifier exists in the container.
*/
public static function exists(string $identifier): bool
{
return isset(static::$container[$identifier]);
}
/**
* Remove the Lock by the identifier from container after used,
* otherwise will occur memory leaks.
*/
public static function clear(string $identifier): void
{
unset(static::$container[$identifier]);
}
}