Skip to content

Instantly share code, notes, and snippets.

@Ghostscypher
Created September 17, 2021 17:29
Show Gist options
  • Save Ghostscypher/fc43c8b4837dd238de60ebdc57beb719 to your computer and use it in GitHub Desktop.
Save Ghostscypher/fc43c8b4837dd238de60ebdc57beb719 to your computer and use it in GitHub Desktop.
<?php
namespace App\Traits;
use Illuminate\Cache\FileStore;
use Illuminate\Filesystem\Filesystem;
use Illuminate\Support\Facades\Cache;
trait LockEventTrait
{
protected FileStore $file_cache;
public function __construct(Filesystem $files)
{
// You can change the cache path for file store, this will be used if and only
// if the cache store is array since array is not persistent storage
$this->file_cache = new FileStore($files, storage_path('app/cache/data'));
}
/**
* @param $key
* @return string
*/
protected function getKey($key) : string {
// If user is authenticated then use this mechanism to generate key
// You can override this method of generating unique key
if(auth()->check()){
return $key . config('app.name') . auth()->user()->id;
}
// This is for unauthenticated user
return $key . implode( '|',[
config('app.name'),
request()->ip(),
request()->getRequestUri(),
request()->session()->getId()
]);
}
/**
* Lock the event using the specified key
*
* @param string $key - Unique key to lock the event
* @param int $decay - The time in seconds before the key expires, 0 for never, default 1 minute
* @param mixed $value - The value to store
* @return bool
*/
public function lock(string $key, int $decay = 60, $value = '') : bool {
if($this->isLocked($key)){
return true;
}
// The value to store
if($value === null){
$value = '';
}
// Check if app uses array as Cache driver, since it's not persistent
if($decay === 0){
if(!$this->isUsingCache()){
$this->file_cache->forever($key, $value);
} else {
Cache::forever($key, $value);
}
} else {
if(!$this->isUsingCache()){
$this->file_cache->add($key, $value, $decay);
} else {
Cache::add($key, $value, $decay);
}
}
// Lock success
return true;
}
/**
* Unlock the event with the specified key
* @param $key - Unique key to identify event
* @return bool|mixed|null
*/
public function unlock(string $key): ?bool
{
if(!$this->isLocked($key)){
return true;
}
$value = null;
// Check if app uses array as Cache driver, since it's not persistent
if(!$this->isUsingCache()){
$value = $this->file_cache->get($key);
$this->file_cache->forget($key);
} else {
$value = Cache::get($key);
Cache::forget($key);
}
return $value;
}
/**
* Check if the given action is locked, returns false if key does not exist
* @param $key - Unique key to identify the event
* @return bool
*/
public function isLocked(string $key) : bool {
// Check if app uses array as Cache driver, since it's not persistent
if(!$this->isUsingCache()){
return !is_null($this->file_cache->get($key));
} else {
return Cache::has($key);
}
}
/**
* Get the value stored in the cache associated with this event
* @param $key - Unique key to identify the event
* @return mixed|null
*/
public function getLockEventValue($key){
if(!$this->isUsingCache()){
return $this->file_cache->get($key);
} else {
return Cache::get($key);
}
}
/**
* Checks if the current cache store is using array,
* array is not persistent
* @return bool
*/
public function isUsingCache() : bool {
return config('cache.default') !== config('cache.stores.array.driver');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment