Final Class Yiisoft\Yii\RateLimiter\Storage\ApcuStorage
| Inheritance | Yiisoft\ |
|---|---|
| Implements | Yiisoft\ |
To use this storage, the APCu PHP extension must be loaded, And you should add "apc.enabled = 1" to your php.ini.
In order to enable APCu for CLI you should add "apc.enable_cli = 1" to your php.ini.
Public Methods
| Method | Description | Defined By |
|---|---|---|
| __construct() | Apcu_cas of ACPu does not support float, and yet supports int. | Yiisoft\ |
| get() | Yiisoft\ |
|
| saveCompareAndSwap() | Yiisoft\ |
|
| saveIfNotExists() | Yiisoft\ |
Constants
| Constant | Value | Description | Defined By |
|---|---|---|---|
| DEFAULT_FIX_PRECISION_RATE | 1000 | Yiisoft\ |
Method Details
Apcu_cas of ACPu does not support float, and yet supports int.
APCu's stored value multiply by $fixPrecisionRate converts to int, AND the getter's value divide by $fixPrecisionRate converts to float. So use it to improve precision.
| public mixed __construct ( integer $fixPrecisionRate = self::DEFAULT_FIX_PRECISION_RATE ) | ||
| $fixPrecisionRate | integer | |
public function __construct(
private int $fixPrecisionRate = self::DEFAULT_FIX_PRECISION_RATE,
) {}
| public ?float get ( string $key ) | ||
| $key | string | |
public function get(string $key): ?float
{
/** @psalm-suppress MixedAssignment */
$value = apcu_fetch($key);
return (is_int($value) || is_float($value))
? (float) $value / $this->fixPrecisionRate
: null;
}
| public boolean saveCompareAndSwap ( string $key, float $oldValue, float $newValue, integer $ttl ) | ||
| $key | string | |
| $oldValue | float | |
| $newValue | float | |
| $ttl | integer | |
public function saveCompareAndSwap(string $key, float $oldValue, float $newValue, int $ttl): bool
{
$oldValue *= $this->fixPrecisionRate;
$newValue *= $this->fixPrecisionRate;
return apcu_cas($key, (int) $oldValue, (int) $newValue);
}
User Contributed Notes
Leave a comment
Join the conversation to share a note.