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\Cache;
4
 
5
use Illuminate\Support\Carbon;
6
 
7
class ArrayLock extends Lock
8
{
9
    /**
10
     * The parent array cache store.
11
     *
12
     * @var \Illuminate\Cache\ArrayStore
13
     */
14
    protected $store;
15
 
16
    /**
17
     * Create a new lock instance.
18
     *
19
     * @param  \Illuminate\Cache\ArrayStore  $store
20
     * @param  string  $name
21
     * @param  int  $seconds
22
     * @param  string|null  $owner
23
     * @return void
24
     */
25
    public function __construct($store, $name, $seconds, $owner = null)
26
    {
27
        parent::__construct($name, $seconds, $owner);
28
 
29
        $this->store = $store;
30
    }
31
 
32
    /**
33
     * Attempt to acquire the lock.
34
     *
35
     * @return bool
36
     */
37
    public function acquire()
38
    {
39
        $expiration = $this->store->locks[$this->name]['expiresAt'] ?? Carbon::now()->addSecond();
40
 
41
        if ($this->exists() && $expiration->isFuture()) {
42
            return false;
43
        }
44
 
45
        $this->store->locks[$this->name] = [
46
            'owner' => $this->owner,
47
            'expiresAt' => $this->seconds === 0 ? null : Carbon::now()->addSeconds($this->seconds),
48
        ];
49
 
50
        return true;
51
    }
52
 
53
    /**
54
     * Determine if the current lock exists.
55
     *
56
     * @return bool
57
     */
58
    protected function exists()
59
    {
60
        return isset($this->store->locks[$this->name]);
61
    }
62
 
63
    /**
64
     * Release the lock.
65
     *
66
     * @return bool
67
     */
68
    public function release()
69
    {
70
        if (! $this->exists()) {
71
            return false;
72
        }
73
 
74
        if (! $this->isOwnedByCurrentProcess()) {
75
            return false;
76
        }
77
 
78
        $this->forceRelease();
79
 
80
        return true;
81
    }
82
 
83
    /**
84
     * Returns the owner value written into the driver for this lock.
85
     *
86
     * @return string
87
     */
88
    protected function getCurrentOwner()
89
    {
90
        return $this->store->locks[$this->name]['owner'];
91
    }
92
 
93
    /**
94
     * Releases this lock in disregard of ownership.
95
     *
96
     * @return void
97
     */
98
    public function forceRelease()
99
    {
100
        unset($this->store->locks[$this->name]);
101
    }
102
}