File tree Expand file tree Collapse file tree 1 file changed +39
-3
lines changed
lib/internal/Magento/Framework/Lock/Backend Expand file tree Collapse file tree 1 file changed +39
-3
lines changed Original file line number Diff line number Diff line change @@ -24,6 +24,13 @@ class Cache implements \Magento\Framework\Lock\LockManagerInterface
24
24
*/
25
25
private $ cache ;
26
26
27
+ /**
28
+ * Sign for locks, helps to avoid removing a lock that was created by another client
29
+ *
30
+ * @string
31
+ */
32
+ private $ lockSign ;
33
+
27
34
/**
28
35
* @param FrontendInterface $cache
29
36
*/
@@ -37,19 +44,48 @@ public function __construct(FrontendInterface $cache)
37
44
*/
38
45
public function lock (string $ name , int $ timeout = -1 ): bool
39
46
{
40
- if ((bool )$ this ->cache ->test ($ this ->getIdentifier ($ name ))) {
47
+ if (empty ($ this ->lockSign )) {
48
+ $ this ->lockSign = \bin2hex (\random_bytes (8 ));
49
+ }
50
+
51
+ $ data = $ this ->cache ->load ($ this ->getIdentifier ($ name ));
52
+
53
+ if (false !== $ data ) {
41
54
return false ;
42
55
}
43
56
44
- return $ this ->cache ->save ('1 ' , $ this ->getIdentifier ($ name ), [], $ timeout );
57
+ $ saveResult = $ this ->cache ->save ($ this ->lockSign , $ this ->getIdentifier ($ name ), [], $ timeout * 100 );
58
+
59
+ $ data = $ this ->cache ->load ($ this ->getIdentifier ($ name ));
60
+
61
+ if ($ data === $ this ->lockSign && $ saveResult ) {
62
+ return true ;
63
+ }
64
+
65
+ return false ;
45
66
}
46
67
47
68
/**
48
69
* @inheritdoc
49
70
*/
50
71
public function unlock (string $ name ): bool
51
72
{
52
- return (bool )$ this ->cache ->remove ($ this ->getIdentifier ($ name ));
73
+ if (empty ($ this ->lockSign )) {
74
+ return false ;
75
+ }
76
+
77
+ $ data = $ this ->cache ->load ($ this ->getIdentifier ($ name ));
78
+
79
+ if (false === $ data ) {
80
+ return false ;
81
+ }
82
+
83
+ $ removeResult = false ;
84
+ if ($ data === $ this ->lockSign ) {
85
+ $ removeResult = (bool )$ this ->cache ->remove ($ this ->getIdentifier ($ name ));
86
+ }
87
+
88
+ return $ removeResult ;
53
89
}
54
90
55
91
/**
You can’t perform that action at this time.
0 commit comments