Final Class Yiisoft\Cache\Ttl
| Inheritance | Yiisoft\Cache\Ttl |
|---|
Value object representing a time-to-live (TTL) duration in seconds.
$ttl = Ttl::minutes(5);
$seconds = $ttl->toSeconds(); // 300
Public Properties
| Property | Type | Description | Defined By |
|---|---|---|---|
| $value | integer|null | Yiisoft\Cache\Ttl |
Public Methods
| Method | Description | Defined By |
|---|---|---|
| create() | Create TTL from a combination of seconds, minutes, hours and days. | Yiisoft\Cache\Ttl |
| days() | Create TTL from days. | Yiisoft\Cache\Ttl |
| forever() | Creates a TTL representing "forever" (no expiration). | Yiisoft\Cache\Ttl |
| from() | Creates a Ttl object from various TTL representations. | Yiisoft\Cache\Ttl |
| fromInterval() | Creates a Ttl object from a DateInterval. | Yiisoft\Cache\Ttl |
| hours() | Create TTL from hours. | Yiisoft\Cache\Ttl |
| isForever() | Checks whether the TTL represents "forever". | Yiisoft\Cache\Ttl |
| minutes() | Create TTL from minutes. | Yiisoft\Cache\Ttl |
| seconds() | Create TTL from seconds. | Yiisoft\Cache\Ttl |
| toSeconds() | Get TTL value in seconds or null if forever. | Yiisoft\Cache\Ttl |
Constants
| Constant | Value | Description | Defined By |
|---|---|---|---|
| SECONDS_IN_DAY | 86400 | Yiisoft\Cache\Ttl | |
| SECONDS_IN_HOUR | 3600 | Yiisoft\Cache\Ttl | |
| SECONDS_IN_MINUTE | 60 | Yiisoft\Cache\Ttl |
Property Details
Method Details
Create TTL from a combination of seconds, minutes, hours and days.
| public static self create ( integer $seconds = 0, integer $minutes = 0, integer $hours = 0, integer $days = 0 ) | ||
| $seconds | integer |
Number of seconds. |
| $minutes | integer |
Number of minutes. |
| $hours | integer |
Number of hours. |
| $days | integer |
Number of days. |
public static function create(
int $seconds = 0,
int $minutes = 0,
int $hours = 0,
int $days = 0,
): self {
$totalSeconds = $seconds
+ $minutes * self::SECONDS_IN_MINUTE
+ $hours * self::SECONDS_IN_HOUR
+ $days * self::SECONDS_IN_DAY;
return new self($totalSeconds);
}
Create TTL from days.
| public static self days ( integer $days ) | ||
| $days | integer |
Number of days. |
| return | self |
TTL instance. |
|---|---|---|
public static function days(int $days): self
{
return new self($days * self::SECONDS_IN_DAY);
}
Creates a TTL representing "forever" (no expiration).
| public static self forever ( ) |
public static function forever(): self
{
return new self(null);
}
Creates a Ttl object from various TTL representations.
Handles null, integers, numeric strings, DateInterval, and Ttl objects.
| public static self from ( DateInterval|integer|string|Yiisoft\Cache\Ttl|null $ttl ) | ||
| $ttl | DateInterval|integer|string|Yiisoft\Cache\Ttl|null |
Raw TTL value (string must be numeric, e.g., '3600') |
| throws | TypeError |
For invalid TTL values types. |
|---|---|---|
public static function from(self|DateInterval|int|string|null $ttl): self
{
return match (true) {
$ttl === null => self::forever(),
$ttl instanceof self => $ttl,
$ttl instanceof DateInterval => self::fromInterval($ttl),
is_string($ttl) => self::seconds((int) $ttl),
is_int($ttl) => self::seconds($ttl),
};
}
Creates a Ttl object from a DateInterval.
| public static self fromInterval ( DateInterval $interval ) | ||
| $interval | DateInterval |
The interval to convert to TTL. |
| return | self |
TTL instance. |
|---|---|---|
| throws | InvalidArgumentException |
If the DateInterval results in a negative TTL. |
public static function fromInterval(DateInterval $interval): self
{
$seconds = (new DateTime('@0'))
->add($interval)
->getTimestamp();
return new self($seconds);
}
Create TTL from hours.
| public static self hours ( integer $hours ) | ||
| $hours | integer |
Number of hours. |
| return | self |
TTL instance. |
|---|---|---|
public static function hours(int $hours): self
{
return new self($hours * self::SECONDS_IN_HOUR);
}
Checks whether the TTL represents "forever".
| public boolean isForever ( ) |
public function isForever(): bool
{
return $this->value === null;
}
Create TTL from minutes.
| public static self minutes ( integer $minutes ) | ||
| $minutes | integer |
Number of minutes. |
| return | self |
TTL instance. |
|---|---|---|
public static function minutes(int $minutes): self
{
return new self($minutes * self::SECONDS_IN_MINUTE);
}
Create TTL from seconds.
| public static self seconds ( integer $seconds ) | ||
| $seconds | integer |
Number of seconds. |
| return | self |
TTL instance. |
|---|---|---|
public static function seconds(int $seconds): self
{
return new self($seconds);
}
Signup or Login in order to comment.