Download the PHP package elephant-php/ttl without Composer
On this page you can find all versions of the php package elephant-php/ttl. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download elephant-php/ttl
More information about elephant-php/ttl
Files in elephant-php/ttl
Package ttl
Short Description An immutable Time To Live value object for the cache and other places where you need to specify a time rather than a magic value.
License MIT
Informations about the package ttl
TTL - Time To Live
An immutable Time To Live value object for the cache and other places where you need to specify a time rather than a magic value.
Requirements
- PHP 8.1 or higher.
Installation
The package could be installed with Composer:
Configuration
Abstract example for your cache
Ttl object
Ttl is a simple immutable value object that represents cache time-to-live (TTL) in seconds.
It eliminates magic numbers (like 60 * 60 or 3600), improves readability, and provides convenient factory methods.
Below are examples on how to use it.
If you're using PSR-16 cache adapter directly:
- TTL must be an integer number of seconds or
nullfor infinite lifetime. - Always use
->toSeconds()when usingTtlobject.
Creating and Normalizing TTL
The Ttl::from() method normalizes various TTL representations (Ttl, DateInterval, int, string, or null) into a Ttl object.
When using Ttl with You cache wrapper:
- You can pass a
Ttlobject in the constructor as the default value. - You can pass it to methods like
getOrSet()which expect integer number of seconds ornull.
Additional Features
Checking Infinite TTL:
Use isForever() to check if a TTL represents "forever" (i.e., no expiration). It returns true when the TTL value is null.
`
Accessing TTL Value
Use toSeconds() to get the TTL in seconds (int) or null for "forever". The public $value property can be accessed directly (e.g., Ttl::seconds(30)->value), but toSeconds() is preferred for clarity.
Invalid TTL values
License
MIT License
Please see LICENSE for more information.