1212
1313final  class  LockService implements  LockServiceInterface
1414{
15-     private  const  PREFIX  = 'wallet_lock:: ' ;
15+     private  const  LOCK_KEY  = 'wallet_lock:: ' ;
1616
17-     /** 
18-      * @var array<string, bool> 
19-      */ 
20-     private  array  $ lockedKeys  = [];
17+     private  const  INNER_KEYS  = 'inner_keys:: ' ;
18+ 
19+     private  ?LockProvider   $ lockProvider  = null ;
20+ 
21+     private  CacheRepository   $ lockedKeys ;
2122
2223    private  CacheRepository   $ cache ;
2324
2425    private  int  $ seconds ;
2526
2627    public  function  __construct (CacheFactory   $ cacheFactory )
2728    {
28-         $ this  ->seconds  = (int ) config ('wallet.lock.seconds ' , 1 );
2929        $ this  ->cache  = $ cacheFactory ->store (config ('wallet.lock.driver ' , 'array ' ));
30+         $ this  ->seconds  = (int ) config ('wallet.lock.seconds ' , 1 );
31+         $ this  ->lockedKeys  = $ cacheFactory ->store ('array ' );
3032    }
3133
3234    /** 
@@ -38,21 +40,20 @@ public function block(string $key, callable $callback): mixed
3840            return  $ callback ();
3941        }
4042
41-         $ this  ->lockedKeys [$ key ] = true ;
43+         $ lock  = $ this  ->getLockProvider ()
44+             ->lock (self ::LOCK_KEY  . $ key , $ this  ->seconds );
45+         $ this  ->lockedKeys ->put (self ::INNER_KEYS  . $ key , true , $ this  ->seconds );
4246
4347        try  {
44-             return  $ this  ->getLockProvider ()
45-                 ->lock (self ::PREFIX  . $ key )
46-                 ->block ($ this  ->seconds , $ callback )
47-             ;
48+             return  $ callback ();
4849        } finally  {
49-             unset( $ this  ->lockedKeys [ $ key] );
50+             $ this  ->lockedKeys -> delete ( self :: INNER_KEYS  .  $ key );
5051        }
5152    }
5253
5354    public  function  isBlocked (string  $ key ): bool 
5455    {
55-         return  $ this  ->lockedKeys [ $ key] ??  false ;
56+         return  $ this  ->lockedKeys -> get ( self :: INNER_KEYS  .  $ key) ===  true ;
5657    }
5758
5859    /** 
@@ -61,14 +62,18 @@ public function isBlocked(string $key): bool
6162     */ 
6263    private  function  getLockProvider (): LockProvider 
6364    {
64-         $ store  = $ this  ->cache ->getStore ();
65-         if  ($ store  instanceof  LockProvider) {
66-             return  $ store ;
65+         if  ($ this  ->lockProvider  === null ) {
66+             $ store  = $ this  ->cache ->getStore ();
67+             if  (! ($ store  instanceof  LockProvider)) {
68+                 throw  new  LockProviderNotFoundException (
69+                     'Lockable cache not found ' ,
70+                     ExceptionInterface::LOCK_PROVIDER_NOT_FOUND 
71+                 );
72+             }
73+ 
74+             $ this  ->lockProvider  = $ store ;
6775        }
6876
69-         throw  new  LockProviderNotFoundException (
70-             'Lockable cache not found ' ,
71-             ExceptionInterface::LOCK_PROVIDER_NOT_FOUND 
72-         );
77+         return  $ this  ->lockProvider ;
7378    }
7479}
0 commit comments