Skip to content

Commit

Permalink
add getCacheLockOwner helper method
Browse files Browse the repository at this point in the history
  • Loading branch information
d8vjork committed Aug 19, 2024
1 parent f585a81 commit d6fc9a2
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions src/Helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

use Illuminate\Contracts\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Str;
use ReflectionClass;
use Throwable;

Expand Down Expand Up @@ -120,4 +122,23 @@ public static function queryFrom($model)

return false;
}

/**
* Get lock owner by key or false if lock not existing.
*/
public static function getCacheLockOwner(string $key = '*'): array|string|false
{
$lockClient = Cache::lockConnection()->client();

if (Str::contains($key, '*')) {
return Str::replace(
// TODO: Is there any other way to get this from Cache, CacheManager, Lock or Redis?
config('database.redis.locks.prefix', '') . Cache::getPrefix(),
'',
$lockClient->keys(Cache::getPrefix() . $key),
);
}

return $lockClient->get(Cache::getPrefix() . $key);
}
}

0 comments on commit d6fc9a2

Please sign in to comment.