Вход Регистрация
Файл: vendor/laravel/framework/src/Illuminate/Bus/DebounceLock.php
Строк: 160
<?php

namespace IlluminateBus;

use 
IlluminateContractsCacheRepository as Cache;
use 
IlluminateQueueAttributesDebounceFor;
use 
IlluminateQueueAttributesReadsQueueAttributes;
use 
IlluminateSupportCarbon;
use 
IlluminateSupportStr;

class 
DebounceLock
{
    use 
ReadsQueueAttributes;

    
/**
     * The cache repository implementation.
     *
     * @var IlluminateContractsCacheRepository
     */
    
protected $cache;

    
/**
     * Create a new debounce lock manager instance.
     *
     * @param  IlluminateContractsCacheRepository  $cache
     */
    
public function __construct(Cache $cache)
    {
        
$this->cache $cache;
    }

    
/**
     * Store a debounce owner token for the given job.
     *
     * Overwrites any existing token, implementing last-writer-wins semantics.
     *
     * @param  mixed  $job
     * @param  int|null  $debounceFor
     * @param  int|null  $maxWait
     * @return array{owner: string, maxWaitExceeded: bool}
     */
    
public function acquire($job$debounceFor null$maxWait null)
    {
        
$cache $this->resolveCache($job);

        
$ttl max(($debounceFor ?? $this->getDebounceDelay($job)) * 10300);

        
$cache->put($key = static::getKey($job), $owner Str::random(40), $ttl);

        return [
            
'owner' => $owner,
            
'maxWaitExceeded' => $this->maxWaitExceeded(
                
$cache$key$ttl$maxWait ?? $this->getMaxDebounceWait($job)
            ),
        ];
    }

    
/**
     * Determine if the maximum debounce wait time has been exceeded.
     */
    
protected function maxWaitExceeded(Cache $cachestring $keyint $ttl, ?int $maxWait): bool
    
{
        if (
is_null($maxWait)) {
            return 
false;
        }

        
$timestampKey $key.':first_dispatched_at';

        if (! 
$cache->has($timestampKey)) {
            
$cache->put($timestampKeyCarbon::now()->getTimestamp(), $ttl);

            return 
false;
        }

        
$elapsed Carbon::now()->getTimestamp() - $cache->get($timestampKey);

        if (
$elapsed >= $maxWait) {
            
$cache->forget($timestampKey);

            return 
true;
        }

        return 
false;
    }

    
/**
     * Determine if the given owner is the current owner for this debounce key.
     *
     * @param  mixed  $job
     * @param  string  $owner
     * @return bool
     */
    
public function isCurrentOwner($jobstring $owner)
    {
        return 
$this->resolveCache($job)->get(static::getKey($job)) === $owner;
    }

    
/**
     * Determine if a debounce token exists for the given job.
     *
     * @param  mixed  $job
     * @return bool
     */
    
public function lockExists($job)
    {
        return ! 
is_null($this->resolveCache($job)->get(static::getKey($job)));
    }

    
/**
     * Remove the debounce token for the given job.
     *
     * @param  mixed  $job
     * @param  string  $owner
     * @return void
     */
    
public function release($jobstring $owner '')
    {
        
$key = static::getKey($job);

        
$cache $this->resolveCache($job);

        if (! empty(
$owner) && $cache->get($key) !== $owner) {
            return;
        }

        
$cache->forget($key);
        
$cache->forget($key.':first_dispatched_at');
    }

    
/**
     * Get the debounce delay for the given job.
     *
     * @param  mixed  $job
     * @return int|null
     */
    
public function getDebounceDelay($job)
    {
        return 
$this->getAttributeValue($jobDebounceFor::class, 'debounceFor');
    }

    
/**
     * Get the maximum debounce wait time for the given job.
     *
     * @param  mixed  $job
     * @return int|null
     */
    
public function getMaxDebounceWait($job)
    {
        return 
$this->getAttributeInstance($jobDebounceFor::class)?->maxWait ?? null;
    }

    
/**
     * Generate the cache key for the given job.
     *
     * @param  mixed  $job
     * @return string
     */
    
public static function getKey($job)
    {
        
$debounceId method_exists($job'debounceId')
            ? 
$job->debounceId()
            : (
$job->debounceId ?? '');

        
$jobName method_exists($job'displayName')
            ? 
hash('xxh128'$job->displayName())
            : 
get_class($job);

        return 
'laravel_debounced_job:'.$jobName.':'.$debounceId;
    }

    
/**
     * Resolve the cache store for the given job.
     *
     * @param  mixed  $job
     * @return IlluminateContractsCacheRepository
     */
    
protected function resolveCache($job)
    {
        return 
method_exists($job'debounceVia')
            ? (
$job->debounceVia() ?? $this->cache)
            : 
$this->cache;
    }
}
Онлайн: 1
Реклама