Subversion-Projekte lars-tiefland.laravel_shop

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
148 lars 1
<?php
2
 
3
namespace Illuminate\Bus;
4
 
5
use Illuminate\Contracts\Cache\Repository as Cache;
6
 
7
class UniqueLock
8
{
9
    /**
10
     * The cache repository implementation.
11
     *
12
     * @var \Illuminate\Contracts\Cache\Repository
13
     */
14
    protected $cache;
15
 
16
    /**
17
     * Create a new unique lock manager instance.
18
     *
19
     * @param  \Illuminate\Contracts\Cache\Repository  $cache
20
     * @return void
21
     */
22
    public function __construct(Cache $cache)
23
    {
24
        $this->cache = $cache;
25
    }
26
 
27
    /**
28
     * Attempt to acquire a lock for the given job.
29
     *
30
     * @param  mixed  $job
31
     * @return bool
32
     */
33
    public function acquire($job)
34
    {
35
        $uniqueFor = method_exists($job, 'uniqueFor')
36
                    ? $job->uniqueFor()
37
                    : ($job->uniqueFor ?? 0);
38
 
39
        $cache = method_exists($job, 'uniqueVia')
40
                    ? $job->uniqueVia()
41
                    : $this->cache;
42
 
43
        return (bool) $cache->lock($this->getKey($job), $uniqueFor)->get();
44
    }
45
 
46
    /**
47
     * Release the lock for the given job.
48
     *
49
     * @param  mixed  $job
50
     * @return void
51
     */
52
    public function release($job)
53
    {
54
        $cache = method_exists($job, 'uniqueVia')
55
                    ? $job->uniqueVia()
56
                    : $this->cache;
57
 
58
        $cache->lock($this->getKey($job))->forceRelease();
59
    }
60
 
61
    /**
62
     * Generate the lock key for the given job.
63
     *
64
     * @param  mixed  $job
65
     * @return string
66
     */
67
    protected function getKey($job)
68
    {
69
        $uniqueId = method_exists($job, 'uniqueId')
70
                    ? $job->uniqueId()
71
                    : ($job->uniqueId ?? '');
72
 
73
        return 'laravel_unique_job:'.get_class($job).$uniqueId;
74
    }
75
}