Final Class Yiisoft\Yii\RateLimiter\Storage\ApcuStorage
| Inheritance | Yiisoft\Yii\RateLimiter\Storage\ApcuStorage |
|---|---|
| Implements | Yiisoft\Yii\RateLimiter\Storage\StorageInterface |
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\Yii\RateLimiter\Storage\ApcuStorage |
| get() | Yiisoft\Yii\RateLimiter\Storage\ApcuStorage | |
| saveCompareAndSwap() | Yiisoft\Yii\RateLimiter\Storage\ApcuStorage | |
| saveIfNotExists() | Yiisoft\Yii\RateLimiter\Storage\ApcuStorage |
Constants
| Constant | Value | Description | Defined By |
|---|---|---|---|
| DEFAULT_FIX_PRECISION_RATE | 1000 | Yiisoft\Yii\RateLimiter\Storage\ApcuStorage |
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|null 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);
}
Signup or Login in order to comment.