@@ -32,6 +32,7 @@ public function __construct($identifier, Client $client, $ttl = 10000, LoggerInt
32
32
$ this ->ttl = $ ttl ;
33
33
$ this ->logger = $ logger ?: new NullLogger ;
34
34
$ this ->id = mt_rand ();
35
+ register_shutdown_function ($ this ->releaseClosure ());
35
36
}
36
37
37
38
public function acquire ()
@@ -48,18 +49,32 @@ public function acquire()
48
49
49
50
public function release ()
50
51
{
51
- $ script = <<<LUA
52
- if redis.call("get", KEYS[1]) == ARGV[1] then
53
- return redis.call("del", KEYS[1])
54
- end
55
- LUA ;
56
- if ($ this ->client ->eval ($ script , 1 , $ this ->identifier , $ this ->id )) {
57
- $ this ->logger ->debug ("lock released on {identifier} " , ["identifier " => $ this ->identifier ]);
58
- }
52
+ $ closure = $ this ->releaseClosure ();
53
+ $ closure ();
59
54
}
60
55
61
56
public function __destruct ()
62
57
{
63
58
$ this ->release ();
64
59
}
60
+
61
+ private function releaseClosure ()
62
+ {
63
+ $ client = $ this ->client ;
64
+ $ id = $ this ->id ;
65
+ $ identifier = $ this ->identifier ;
66
+ $ logger = $ this ->logger ;
67
+
68
+ $ closure = function () use ($ client , $ identifier , $ id , $ logger ) {
69
+ $ script = <<<LUA
70
+ if redis.call("get", KEYS[1]) == ARGV[1] then
71
+ return redis.call("del", KEYS[1])
72
+ end
73
+ LUA ;
74
+ if ($ client ->eval ($ script , 1 , $ identifier , $ id )) {
75
+ $ logger ->debug ("lock released on {identifier} " , ["identifier " => $ identifier ]);
76
+ }
77
+ };
78
+ return $ closure ->bindTo (null );
79
+ }
65
80
}
0 commit comments