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\Contracts\Cache\Lock as LockContract;
6
use Illuminate\Contracts\Cache\LockTimeoutException;
7
use Illuminate\Support\InteractsWithTime;
8
use Illuminate\Support\Str;
9
 
10
abstract class Lock implements LockContract
11
{
12
    use InteractsWithTime;
13
 
14
    /**
15
     * The name of the lock.
16
     *
17
     * @var string
18
     */
19
    protected $name;
20
 
21
    /**
22
     * The number of seconds the lock should be maintained.
23
     *
24
     * @var int
25
     */
26
    protected $seconds;
27
 
28
    /**
29
     * The scope identifier of this lock.
30
     *
31
     * @var string
32
     */
33
    protected $owner;
34
 
35
    /**
36
     * The number of milliseconds to wait before re-attempting to acquire a lock while blocking.
37
     *
38
     * @var int
39
     */
40
    protected $sleepMilliseconds = 250;
41
 
42
    /**
43
     * Create a new lock instance.
44
     *
45
     * @param  string  $name
46
     * @param  int  $seconds
47
     * @param  string|null  $owner
48
     * @return void
49
     */
50
    public function __construct($name, $seconds, $owner = null)
51
    {
52
        if (is_null($owner)) {
53
            $owner = Str::random();
54
        }
55
 
56
        $this->name = $name;
57
        $this->owner = $owner;
58
        $this->seconds = $seconds;
59
    }
60
 
61
    /**
62
     * Attempt to acquire the lock.
63
     *
64
     * @return bool
65
     */
66
    abstract public function acquire();
67
 
68
    /**
69
     * Release the lock.
70
     *
71
     * @return bool
72
     */
73
    abstract public function release();
74
 
75
    /**
76
     * Returns the owner value written into the driver for this lock.
77
     *
78
     * @return string
79
     */
80
    abstract protected function getCurrentOwner();
81
 
82
    /**
83
     * Attempt to acquire the lock.
84
     *
85
     * @param  callable|null  $callback
86
     * @return mixed
87
     */
88
    public function get($callback = null)
89
    {
90
        $result = $this->acquire();
91
 
92
        if ($result && is_callable($callback)) {
93
            try {
94
                return $callback();
95
            } finally {
96
                $this->release();
97
            }
98
        }
99
 
100
        return $result;
101
    }
102
 
103
    /**
104
     * Attempt to acquire the lock for the given number of seconds.
105
     *
106
     * @param  int  $seconds
107
     * @param  callable|null  $callback
108
     * @return mixed
109
     *
110
     * @throws \Illuminate\Contracts\Cache\LockTimeoutException
111
     */
112
    public function block($seconds, $callback = null)
113
    {
114
        $starting = $this->currentTime();
115
 
116
        while (! $this->acquire()) {
117
            usleep($this->sleepMilliseconds * 1000);
118
 
119
            if ($this->currentTime() - $seconds >= $starting) {
120
                throw new LockTimeoutException;
121
            }
122
        }
123
 
124
        if (is_callable($callback)) {
125
            try {
126
                return $callback();
127
            } finally {
128
                $this->release();
129
            }
130
        }
131
 
132
        return true;
133
    }
134
 
135
    /**
136
     * Returns the current owner of the lock.
137
     *
138
     * @return string
139
     */
140
    public function owner()
141
    {
142
        return $this->owner;
143
    }
144
 
145
    /**
146
     * Determines whether this lock is allowed to release the lock in the driver.
147
     *
148
     * @return bool
149
     */
150
    protected function isOwnedByCurrentProcess()
151
    {
152
        return $this->getCurrentOwner() === $this->owner;
153
    }
154
 
155
    /**
156
     * Specify the number of milliseconds to sleep in between blocked lock acquisition attempts.
157
     *
158
     * @param  int  $milliseconds
159
     * @return $this
160
     */
161
    public function betweenBlockedAttemptsSleepFor($milliseconds)
162
    {
163
        $this->sleepMilliseconds = $milliseconds;
164
 
165
        return $this;
166
    }
167
}