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\Database\Connection;
6
use Illuminate\Database\QueryException;
7
use Illuminate\Support\Carbon;
8
 
9
class DatabaseLock extends Lock
10
{
11
    /**
12
     * The database connection instance.
13
     *
14
     * @var \Illuminate\Database\Connection
15
     */
16
    protected $connection;
17
 
18
    /**
19
     * The database table name.
20
     *
21
     * @var string
22
     */
23
    protected $table;
24
 
25
    /**
26
     * The prune probability odds.
27
     *
28
     * @var array
29
     */
30
    protected $lottery;
31
 
32
    /**
33
     * Create a new lock instance.
34
     *
35
     * @param  \Illuminate\Database\Connection  $connection
36
     * @param  string  $table
37
     * @param  string  $name
38
     * @param  int  $seconds
39
     * @param  string|null  $owner
40
     * @param  array  $lottery
41
     * @return void
42
     */
43
    public function __construct(Connection $connection, $table, $name, $seconds, $owner = null, $lottery = [2, 100])
44
    {
45
        parent::__construct($name, $seconds, $owner);
46
 
47
        $this->connection = $connection;
48
        $this->table = $table;
49
        $this->lottery = $lottery;
50
    }
51
 
52
    /**
53
     * Attempt to acquire the lock.
54
     *
55
     * @return bool
56
     */
57
    public function acquire()
58
    {
59
        try {
60
            $this->connection->table($this->table)->insert([
61
                'key' => $this->name,
62
                'owner' => $this->owner,
63
                'expiration' => $this->expiresAt(),
64
            ]);
65
 
66
            $acquired = true;
67
        } catch (QueryException $e) {
68
            $updated = $this->connection->table($this->table)
69
                ->where('key', $this->name)
70
                ->where(function ($query) {
71
                    return $query->where('owner', $this->owner)->orWhere('expiration', '<=', time());
72
                })->update([
73
                    'owner' => $this->owner,
74
                    'expiration' => $this->expiresAt(),
75
                ]);
76
 
77
            $acquired = $updated >= 1;
78
        }
79
 
80
        if (random_int(1, $this->lottery[1]) <= $this->lottery[0]) {
81
            $this->connection->table($this->table)->where('expiration', '<=', time())->delete();
82
        }
83
 
84
        return $acquired;
85
    }
86
 
87
    /**
88
     * Get the UNIX timestamp indicating when the lock should expire.
89
     *
90
     * @return int
91
     */
92
    protected function expiresAt()
93
    {
94
        return $this->seconds > 0 ? time() + $this->seconds : Carbon::now()->addDays(1)->getTimestamp();
95
    }
96
 
97
    /**
98
     * Release the lock.
99
     *
100
     * @return bool
101
     */
102
    public function release()
103
    {
104
        if ($this->isOwnedByCurrentProcess()) {
105
            $this->connection->table($this->table)
106
                        ->where('key', $this->name)
107
                        ->where('owner', $this->owner)
108
                        ->delete();
109
 
110
            return true;
111
        }
112
 
113
        return false;
114
    }
115
 
116
    /**
117
     * Releases this lock in disregard of ownership.
118
     *
119
     * @return void
120
     */
121
    public function forceRelease()
122
    {
123
        $this->connection->table($this->table)
124
                    ->where('key', $this->name)
125
                    ->delete();
126
    }
127
 
128
    /**
129
     * Returns the owner value written into the driver for this lock.
130
     *
131
     * @return string
132
     */
133
    protected function getCurrentOwner()
134
    {
135
        return optional($this->connection->table($this->table)->where('key', $this->name)->first())->owner;
136
    }
137
 
138
    /**
139
     * Get the name of the database connection being used to manage the lock.
140
     *
141
     * @return string
142
     */
143
    public function getConnectionName()
144
    {
145
        return $this->connection->getName();
146
    }
147
}