0 follower

Final Class Yiisoft\Yii\RateLimiter\Storage\ApcuStorage

InheritanceYiisoft\Yii\RateLimiter\Storage\ApcuStorage
ImplementsYiisoft\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.

Constants

Hide inherited constants

Constant Value Description Defined By
DEFAULT_FIX_PRECISION_RATE 1000 Yiisoft\Yii\RateLimiter\Storage\ApcuStorage

Method Details

Hide inherited methods

__construct() public method

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
) {
}

            
get() public method

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;
}

            
saveCompareAndSwap() public method

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);
}

            
saveIfNotExists() public method

public boolean saveIfNotExists ( string $key, float $value, integer $ttl )
$key string
$value float
$ttl integer

                public function saveIfNotExists(string $key, float $value, int $ttl): bool
{
    $value *= $this->fixPrecisionRate;
    return apcu_add($key, (int) $value, $ttl);
}