upgraded dependencies
This commit is contained in:
@@ -2,7 +2,7 @@
|
||||
|
||||
namespace Illuminate\Cache;
|
||||
|
||||
use Carbon\Carbon;
|
||||
use Illuminate\Support\Carbon;
|
||||
|
||||
class ArrayLock extends Lock
|
||||
{
|
||||
|
||||
@@ -23,6 +23,24 @@ class ArrayStore extends TaggableStore implements LockProvider
|
||||
*/
|
||||
public $locks = [];
|
||||
|
||||
/**
|
||||
* Indicates if values are serialized within the store.
|
||||
*
|
||||
* @var bool
|
||||
*/
|
||||
protected $serializesValues;
|
||||
|
||||
/**
|
||||
* Create a new Array store.
|
||||
*
|
||||
* @param bool $serializesValues
|
||||
* @return void
|
||||
*/
|
||||
public function __construct($serializesValues = false)
|
||||
{
|
||||
$this->serializesValues = $serializesValues;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve an item from the cache by key.
|
||||
*
|
||||
@@ -45,7 +63,7 @@ class ArrayStore extends TaggableStore implements LockProvider
|
||||
return;
|
||||
}
|
||||
|
||||
return $item['value'];
|
||||
return $this->serializesValues ? unserialize($item['value']) : $item['value'];
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -59,7 +77,7 @@ class ArrayStore extends TaggableStore implements LockProvider
|
||||
public function put($key, $value, $seconds)
|
||||
{
|
||||
$this->storage[$key] = [
|
||||
'value' => $value,
|
||||
'value' => $this->serializesValues ? serialize($value) : $value,
|
||||
'expiresAt' => $this->calculateExpiration($seconds),
|
||||
];
|
||||
|
||||
@@ -75,15 +93,17 @@ class ArrayStore extends TaggableStore implements LockProvider
|
||||
*/
|
||||
public function increment($key, $value = 1)
|
||||
{
|
||||
if (! isset($this->storage[$key])) {
|
||||
$this->forever($key, $value);
|
||||
if (! is_null($existing = $this->get($key))) {
|
||||
return tap(((int) $existing) + $value, function ($incremented) use ($key) {
|
||||
$value = $this->serializesValues ? serialize($incremented) : $incremented;
|
||||
|
||||
return $this->storage[$key]['value'];
|
||||
$this->storage[$key]['value'] = $value;
|
||||
});
|
||||
}
|
||||
|
||||
$this->storage[$key]['value'] = ((int) $this->storage[$key]['value']) + $value;
|
||||
$this->forever($key, $value);
|
||||
|
||||
return $this->storage[$key]['value'];
|
||||
return $value;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -138,11 +138,12 @@ class CacheManager implements FactoryContract
|
||||
/**
|
||||
* Create an instance of the array cache driver.
|
||||
*
|
||||
* @param array $config
|
||||
* @return \Illuminate\Cache\Repository
|
||||
*/
|
||||
protected function createArrayDriver()
|
||||
protected function createArrayDriver(array $config)
|
||||
{
|
||||
return $this->repository(new ArrayStore);
|
||||
return $this->repository(new ArrayStore($config['serialize'] ?? false));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -213,7 +214,11 @@ class CacheManager implements FactoryContract
|
||||
|
||||
return $this->repository(
|
||||
new DatabaseStore(
|
||||
$connection, $config['table'], $this->getPrefix($config)
|
||||
$connection,
|
||||
$config['table'],
|
||||
$this->getPrefix($config),
|
||||
$config['lock_table'] ?? 'cache_locks',
|
||||
$config['lock_lottery'] ?? [2, 100]
|
||||
)
|
||||
);
|
||||
}
|
||||
@@ -226,11 +231,21 @@ class CacheManager implements FactoryContract
|
||||
*/
|
||||
protected function createDynamodbDriver(array $config)
|
||||
{
|
||||
$client = $this->newDynamodbClient($config);
|
||||
$dynamoConfig = [
|
||||
'region' => $config['region'],
|
||||
'version' => 'latest',
|
||||
'endpoint' => $config['endpoint'] ?? null,
|
||||
];
|
||||
|
||||
if ($config['key'] && $config['secret']) {
|
||||
$dynamoConfig['credentials'] = Arr::only(
|
||||
$config, ['key', 'secret', 'token']
|
||||
);
|
||||
}
|
||||
|
||||
return $this->repository(
|
||||
new DynamoDbStore(
|
||||
$client,
|
||||
new DynamoDbClient($dynamoConfig),
|
||||
$config['table'],
|
||||
$config['attributes']['key'] ?? 'key',
|
||||
$config['attributes']['value'] ?? 'value',
|
||||
@@ -240,28 +255,6 @@ class CacheManager implements FactoryContract
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create new DynamoDb Client instance.
|
||||
*
|
||||
* @return DynamoDbClient
|
||||
*/
|
||||
protected function newDynamodbClient(array $config)
|
||||
{
|
||||
$dynamoConfig = [
|
||||
'region' => $config['region'],
|
||||
'version' => 'latest',
|
||||
'endpoint' => $config['endpoint'] ?? null,
|
||||
];
|
||||
|
||||
if (isset($config['key']) && isset($config['secret'])) {
|
||||
$dynamoConfig['credentials'] = Arr::only(
|
||||
$config, ['key', 'secret', 'token']
|
||||
);
|
||||
}
|
||||
|
||||
return new DynamoDbClient($dynamoConfig);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new cache repository with the given implementation.
|
||||
*
|
||||
|
||||
139
vendor/laravel/framework/src/Illuminate/Cache/DatabaseLock.php
vendored
Normal file
139
vendor/laravel/framework/src/Illuminate/Cache/DatabaseLock.php
vendored
Normal file
@@ -0,0 +1,139 @@
|
||||
<?php
|
||||
|
||||
namespace Illuminate\Cache;
|
||||
|
||||
use Illuminate\Database\Connection;
|
||||
use Illuminate\Database\QueryException;
|
||||
use Illuminate\Support\Carbon;
|
||||
|
||||
class DatabaseLock extends Lock
|
||||
{
|
||||
/**
|
||||
* The database connection instance.
|
||||
*
|
||||
* @var \Illuminate\Database\Connection
|
||||
*/
|
||||
protected $connection;
|
||||
|
||||
/**
|
||||
* The database table name.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $table;
|
||||
|
||||
/**
|
||||
* The prune probability odds.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $lottery;
|
||||
|
||||
/**
|
||||
* Create a new lock instance.
|
||||
*
|
||||
* @param \Illuminate\Database\Connection $connection
|
||||
* @param string $table
|
||||
* @param string $name
|
||||
* @param int $seconds
|
||||
* @param string|null $owner
|
||||
* @param array $lottery
|
||||
* @return void
|
||||
*/
|
||||
public function __construct(Connection $connection, $table, $name, $seconds, $owner = null, $lottery = [2, 100])
|
||||
{
|
||||
parent::__construct($name, $seconds, $owner);
|
||||
|
||||
$this->connection = $connection;
|
||||
$this->table = $table;
|
||||
$this->lottery = $lottery;
|
||||
}
|
||||
|
||||
/**
|
||||
* Attempt to acquire the lock.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function acquire()
|
||||
{
|
||||
$acquired = false;
|
||||
|
||||
try {
|
||||
$this->connection->table($this->table)->insert([
|
||||
'key' => $this->name,
|
||||
'owner' => $this->owner,
|
||||
'expiration' => $this->expiresAt(),
|
||||
]);
|
||||
|
||||
$acquired = true;
|
||||
} catch (QueryException $e) {
|
||||
$updated = $this->connection->table($this->table)
|
||||
->where('key', $this->name)
|
||||
->where(function ($query) {
|
||||
return $query->where('owner', $this->owner)->orWhere('expiration', '<=', time());
|
||||
})->update([
|
||||
'owner' => $this->owner,
|
||||
'expiration' => $this->expiresAt(),
|
||||
]);
|
||||
|
||||
$acquired = $updated >= 1;
|
||||
}
|
||||
|
||||
if (random_int(1, $this->lottery[1]) <= $this->lottery[0]) {
|
||||
$this->connection->table($this->table)->where('expiration', '<=', time())->delete();
|
||||
}
|
||||
|
||||
return $acquired;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the UNIX timestamp indicating when the lock should expire.
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
protected function expiresAt()
|
||||
{
|
||||
return $this->seconds > 0 ? time() + $this->seconds : Carbon::now()->addDays(1)->getTimestamp();
|
||||
}
|
||||
|
||||
/**
|
||||
* Release the lock.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function release()
|
||||
{
|
||||
if ($this->isOwnedByCurrentProcess()) {
|
||||
$this->connection->table($this->table)
|
||||
->where('key', $this->name)
|
||||
->where('owner', $this->owner)
|
||||
->delete();
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Releases this lock in disregard of ownership.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function forceRelease()
|
||||
{
|
||||
$this->connection->table($this->table)
|
||||
->where('key', $this->name)
|
||||
->delete();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the owner value written into the driver for this lock.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
protected function getCurrentOwner()
|
||||
{
|
||||
return optional($this->connection->table($this->table)->where('key', $this->name)->first())->owner;
|
||||
}
|
||||
}
|
||||
@@ -4,13 +4,15 @@ namespace Illuminate\Cache;
|
||||
|
||||
use Closure;
|
||||
use Exception;
|
||||
use Illuminate\Contracts\Cache\LockProvider;
|
||||
use Illuminate\Contracts\Cache\Store;
|
||||
use Illuminate\Database\ConnectionInterface;
|
||||
use Illuminate\Database\PostgresConnection;
|
||||
use Illuminate\Database\QueryException;
|
||||
use Illuminate\Support\InteractsWithTime;
|
||||
use Illuminate\Support\Str;
|
||||
|
||||
class DatabaseStore implements Store
|
||||
class DatabaseStore implements LockProvider, Store
|
||||
{
|
||||
use InteractsWithTime, RetrievesMultipleKeys;
|
||||
|
||||
@@ -35,19 +37,41 @@ class DatabaseStore implements Store
|
||||
*/
|
||||
protected $prefix;
|
||||
|
||||
/**
|
||||
* The name of the cache locks table.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $lockTable;
|
||||
|
||||
/**
|
||||
* An array representation of the lock lottery odds.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $lockLottery;
|
||||
|
||||
/**
|
||||
* Create a new database store.
|
||||
*
|
||||
* @param \Illuminate\Database\ConnectionInterface $connection
|
||||
* @param string $table
|
||||
* @param string $prefix
|
||||
* @param string $lockTable
|
||||
* @param array $lockLottery
|
||||
* @return void
|
||||
*/
|
||||
public function __construct(ConnectionInterface $connection, $table, $prefix = '')
|
||||
public function __construct(ConnectionInterface $connection,
|
||||
$table,
|
||||
$prefix = '',
|
||||
$lockTable = 'cache_locks',
|
||||
$lockLottery = [2, 100])
|
||||
{
|
||||
$this->table = $table;
|
||||
$this->prefix = $prefix;
|
||||
$this->connection = $connection;
|
||||
$this->lockTable = $lockTable;
|
||||
$this->lockLottery = $lockLottery;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -94,9 +118,7 @@ class DatabaseStore implements Store
|
||||
public function put($key, $value, $seconds)
|
||||
{
|
||||
$key = $this->prefix.$key;
|
||||
|
||||
$value = $this->serialize($value);
|
||||
|
||||
$expiration = $this->getTime() + $seconds;
|
||||
|
||||
try {
|
||||
@@ -108,6 +130,35 @@ class DatabaseStore implements Store
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Store an item in the cache if the key doesn't exist.
|
||||
*
|
||||
* @param string $key
|
||||
* @param mixed $value
|
||||
* @param int $seconds
|
||||
* @return bool
|
||||
*/
|
||||
public function add($key, $value, $seconds)
|
||||
{
|
||||
$key = $this->prefix.$key;
|
||||
$value = $this->serialize($value);
|
||||
$expiration = $this->getTime() + $seconds;
|
||||
|
||||
try {
|
||||
return $this->table()->insert(compact('key', 'value', 'expiration'));
|
||||
} catch (QueryException $e) {
|
||||
return $this->table()
|
||||
->where('key', $key)
|
||||
->where('expiration', '<=', $this->getTime())
|
||||
->update([
|
||||
'value' => $value,
|
||||
'expiration' => $expiration,
|
||||
]) >= 1;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Increment the value of an item in the cache.
|
||||
*
|
||||
@@ -205,6 +256,38 @@ class DatabaseStore implements Store
|
||||
return $this->put($key, $value, 315360000);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a lock instance.
|
||||
*
|
||||
* @param string $name
|
||||
* @param int $seconds
|
||||
* @param string|null $owner
|
||||
* @return \Illuminate\Contracts\Cache\Lock
|
||||
*/
|
||||
public function lock($name, $seconds = 0, $owner = null)
|
||||
{
|
||||
return new DatabaseLock(
|
||||
$this->connection,
|
||||
$this->lockTable,
|
||||
$this->prefix.$name,
|
||||
$seconds,
|
||||
$owner,
|
||||
$this->lockLottery
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Restore a lock instance using the owner identifier.
|
||||
*
|
||||
* @param string $name
|
||||
* @param string $owner
|
||||
* @return \Illuminate\Contracts\Cache\Lock
|
||||
*/
|
||||
public function restoreLock($name, $owner)
|
||||
{
|
||||
return $this->lock($name, 0, $owner);
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove an item from the cache.
|
||||
*
|
||||
|
||||
@@ -525,14 +525,4 @@ class DynamoDbStore implements LockProvider, Store
|
||||
{
|
||||
$this->prefix = ! empty($prefix) ? $prefix.':' : '';
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the DynamoDb Client instance.
|
||||
*
|
||||
* @return DynamoDbClient
|
||||
*/
|
||||
public function getClient()
|
||||
{
|
||||
return $this->dynamo;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -32,6 +32,13 @@ abstract class Lock implements LockContract
|
||||
*/
|
||||
protected $owner;
|
||||
|
||||
/**
|
||||
* The number of milliseconds to wait before re-attempting to acquire a lock while blocking.
|
||||
*
|
||||
* @var int
|
||||
*/
|
||||
protected $sleepMilliseconds = 250;
|
||||
|
||||
/**
|
||||
* Create a new lock instance.
|
||||
*
|
||||
@@ -98,7 +105,7 @@ abstract class Lock implements LockContract
|
||||
*
|
||||
* @param int $seconds
|
||||
* @param callable|null $callback
|
||||
* @return mixed
|
||||
* @return bool
|
||||
*
|
||||
* @throws \Illuminate\Contracts\Cache\LockTimeoutException
|
||||
*/
|
||||
@@ -107,7 +114,7 @@ abstract class Lock implements LockContract
|
||||
$starting = $this->currentTime();
|
||||
|
||||
while (! $this->acquire()) {
|
||||
usleep(250 * 1000);
|
||||
usleep($this->sleepMilliseconds * 1000);
|
||||
|
||||
if ($this->currentTime() - $seconds >= $starting) {
|
||||
throw new LockTimeoutException;
|
||||
@@ -144,4 +151,17 @@ abstract class Lock implements LockContract
|
||||
{
|
||||
return $this->getCurrentOwner() === $this->owner;
|
||||
}
|
||||
|
||||
/**
|
||||
* Specify the number of milliseconds to sleep in between blocked lock aquisition attempts.
|
||||
*
|
||||
* @param int $milliseconds
|
||||
* @return $this
|
||||
*/
|
||||
public function betweenBlockedAttemptsSleepFor($milliseconds)
|
||||
{
|
||||
$this->sleepMilliseconds = $milliseconds;
|
||||
|
||||
return $this;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -36,8 +36,6 @@ class RateLimiter
|
||||
*/
|
||||
public function tooManyAttempts($key, $maxAttempts)
|
||||
{
|
||||
$key = $this->cleanRateLimiterKey($key);
|
||||
|
||||
if ($this->attempts($key) >= $maxAttempts) {
|
||||
if ($this->cache->has($key.':timer')) {
|
||||
return true;
|
||||
@@ -58,8 +56,6 @@ class RateLimiter
|
||||
*/
|
||||
public function hit($key, $decaySeconds = 60)
|
||||
{
|
||||
$key = $this->cleanRateLimiterKey($key);
|
||||
|
||||
$this->cache->add(
|
||||
$key.':timer', $this->availableAt($decaySeconds), $decaySeconds
|
||||
);
|
||||
@@ -83,8 +79,6 @@ class RateLimiter
|
||||
*/
|
||||
public function attempts($key)
|
||||
{
|
||||
$key = $this->cleanRateLimiterKey($key);
|
||||
|
||||
return $this->cache->get($key, 0);
|
||||
}
|
||||
|
||||
@@ -96,8 +90,6 @@ class RateLimiter
|
||||
*/
|
||||
public function resetAttempts($key)
|
||||
{
|
||||
$key = $this->cleanRateLimiterKey($key);
|
||||
|
||||
return $this->cache->forget($key);
|
||||
}
|
||||
|
||||
@@ -110,8 +102,6 @@ class RateLimiter
|
||||
*/
|
||||
public function retriesLeft($key, $maxAttempts)
|
||||
{
|
||||
$key = $this->cleanRateLimiterKey($key);
|
||||
|
||||
$attempts = $this->attempts($key);
|
||||
|
||||
return $maxAttempts - $attempts;
|
||||
@@ -125,8 +115,6 @@ class RateLimiter
|
||||
*/
|
||||
public function clear($key)
|
||||
{
|
||||
$key = $this->cleanRateLimiterKey($key);
|
||||
|
||||
$this->resetAttempts($key);
|
||||
|
||||
$this->cache->forget($key.':timer');
|
||||
@@ -140,19 +128,6 @@ class RateLimiter
|
||||
*/
|
||||
public function availableIn($key)
|
||||
{
|
||||
$key = $this->cleanRateLimiterKey($key);
|
||||
|
||||
return $this->cache->get($key.':timer') - $this->currentTime();
|
||||
}
|
||||
|
||||
/**
|
||||
* Clean the rate limiter key from unicode characters.
|
||||
*
|
||||
* @param string $key
|
||||
* @return string
|
||||
*/
|
||||
public function cleanRateLimiterKey($key)
|
||||
{
|
||||
return preg_replace('/&([a-z])[a-z]+;/i', '$1', htmlentities($key));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -15,8 +15,8 @@
|
||||
],
|
||||
"require": {
|
||||
"php": "^7.2.5|^8.0",
|
||||
"illuminate/contracts": "^6.0",
|
||||
"illuminate/support": "^6.0"
|
||||
"illuminate/contracts": "^7.0",
|
||||
"illuminate/support": "^7.0"
|
||||
},
|
||||
"autoload": {
|
||||
"psr-4": {
|
||||
@@ -25,15 +25,15 @@
|
||||
},
|
||||
"extra": {
|
||||
"branch-alias": {
|
||||
"dev-master": "6.x-dev"
|
||||
"dev-master": "7.x-dev"
|
||||
}
|
||||
},
|
||||
"suggest": {
|
||||
"ext-memcached": "Required to use the memcache cache driver.",
|
||||
"illuminate/database": "Required to use the database cache driver (^6.0).",
|
||||
"illuminate/filesystem": "Required to use the file cache driver (^6.0).",
|
||||
"illuminate/redis": "Required to use the redis cache driver (^6.0).",
|
||||
"symfony/cache": "Required to PSR-6 cache bridge (^4.3.4)."
|
||||
"illuminate/database": "Required to use the database cache driver (^7.0).",
|
||||
"illuminate/filesystem": "Required to use the file cache driver (^7.0).",
|
||||
"illuminate/redis": "Required to use the redis cache driver (^7.0).",
|
||||
"symfony/cache": "Required to PSR-6 cache bridge (^5.0)."
|
||||
},
|
||||
"config": {
|
||||
"sort-packages": true
|
||||
|
||||
Reference in New Issue
Block a user