Skip to content

Commit

Permalink
Merge pull request #6 from websmurf/laravel-lock
Browse files Browse the repository at this point in the history
use laravel lock instead of manual cache
  • Loading branch information
websmurf authored Jan 21, 2021
2 parents db723bb + 29f474b commit 96f9682
Showing 1 changed file with 13 additions and 9 deletions.
22 changes: 13 additions & 9 deletions src/LaravelExactOnline.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

namespace Websmurf\LaravelExactOnline;

use Illuminate\Cache\Lock;
use Illuminate\Contracts\Auth\Authenticatable;
use Illuminate\Contracts\Cache\LockProvider;
use Illuminate\Contracts\Cache\Repository;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Storage;
Expand Down Expand Up @@ -319,9 +321,13 @@ class LaravelExactOnline
{
/** @var Connection */
private $connection;

/** @var string */
private static $lockKey = 'exactonline.refreshLock';

/** @var null|Lock */
public static $lock = null;

/**
* LaravelExactOnline constructor.
*/
Expand Down Expand Up @@ -413,24 +419,22 @@ public static function acquireLock(): bool
{
/** @var Repository $cache */
$cache = app()->make(Repository::class);
$store = $cache->getStore();

// See if lock was set already
if ($cache->get(self::$lockKey)) {
sleep(15); // Sleep for 15 seconds to avoid an invalid unlock
throw new RuntimeException('Exact online lock already set');
if (!$store instanceof LockProvider) {
return false;
}

return $cache->set(self::$lockKey, 1, 15);
self::$lock = $store->lock(self::$lockKey, 60);
return self::$lock->block(30);
}

/**
* Release lock that was set.
*/
public static function releaseLock(): bool
public static function releaseLock()
{
/** @var Repository $cache */
$cache = app()->make(Repository::class);
return $cache->delete(self::$lockKey);
return optional(self::$lock)->release();
}

/**
Expand Down

0 comments on commit 96f9682

Please sign in to comment.