File Cache
The Luminova FileCache
system allows you to store reusable data in the file system, reducing the need for repeated database queries or API requests. By caching responses, you can enhance performance and efficiency when the same content is requested multiple times.
Additionally, cache can also be useful for third-party API requests. By caching responses, you can enhance performance and efficiency when the same content is requested multiple times.
Note:
The Luminova DatabaseBuilder
class already includes a built-in caching mechanism. You only need to implement additional caching if you are working with prepared statements or directly with theDatabase
connection object.
- Class namespace:
\Luminova\Cache\FileCache
- This class is marked as final and can't be subclassed
- This class is a Final class
Constants
Constant | Visibility | Type | Value |
---|---|---|---|
TTL_7DAYS | public | int | 7 24 60 * 60 |
TTL_24HR | public | int | 24 60 60 |
TTL_30MIN | public | int | 30 * 60 |
PHP | public | string | ".catch.php" |
JSON | public | string | ".json" |
TEXT | public | string | ".txt" |
Methods
constructor
Initialize cache constructor, with optional storage name and subfolder.
public __construct(string|null $storage = null, string $folder = ''): mixed
Parameters:
Parameter | Type | Description |
---|---|---|
$storage | string|null | cache storage filename to hash. |
$folder | string | cache storage sub folder. |
getInstance
Get static Singleton Class.
public static getInstance(string|null $storage = null, string $folder = ''): static
Parameters:
Parameter | Type | Description |
---|---|---|
$storage | string|null | cache storage filename to hash |
$folder | string | cache storage sub folder. |
setPath
Set the new cache directory path.
public setPath(string $path): self
Parameters:
Parameter | Type | Description |
---|---|---|
$path | string | cache directory must end with. |
Return Value:
self
- Return class instance.
setStorage
Sets the new cache file name.
public setStorage(string $storage): self
Parameters:
Parameter | Type | Description |
---|---|---|
$storage | string | cache storage filename, |
Return Value:
self
- Return class instance.
setExtension
Set the cache file extension type to use in storing cache.
public setExtension(string $extension): self
Parameters:
Parameter | Type | Description |
---|---|---|
$extension | string | The file extension. |
Return Value:
self
- Return class instance.
setExpire
Set the expiration time of the cache item.
public setExpire(\DateTimeInterface|null $expiration): self
Parameters:
Parameter | Type | Description |
---|---|---|
$expiration | \DateTimeInterface|null | The expiration time of the cache item. |
Return Value:
self
- The current instance.
expiresAfter
Sets the expiration time of the cache item relative to the current time.
public expiresAfter(int|\DateInterval|null $time): self
Parameters:
Parameter | Type | Description |
---|---|---|
$time | int|\DateInterval|null | The expiration time in seconds or as a DateInterval. |
Return Value:
self
- The current instance.
setLock
Sets the cache lock mode, to avoid deletion even when cache has expired.
public setLock(bool $lock): self
Parameters:
Parameter | Type | Description |
---|---|---|
$lock | bool | lock flag to be used. |
Return Value:
self
- Return class instance.
enableBase64
Enable the cache to store data in base64 encoded.
public enableBase64(bool $encoding): self
Parameters:
Parameter | Type | Description |
---|---|---|
$encoding | bool | Enable base64 encoding or disable. |
Return Value:
self
- Return class instance.
enableDeleteExpired
Enable automatic cache deletion if expired.
public enableDeleteExpired(bool $allow): self
Parameters:
Parameter | Type | Description |
---|---|---|
$allow | bool | Auto deletion flag. |
Return Value:
self
- Return class instance.
enableSecureAccess
Enable cache file access security for caches stored in PHP
file extension.
public enableSecureAccess(bool $secure): self
Parameters:
Parameter | Type | Description |
---|---|---|
$secure | bool | The secure flag to use. |
Return Value:
self
- Return class instance.
getPath
Gets Combines directory, filename and extension into a full filepath.
public getPath(): string
Return Value:
string
- Return full cache path.
onExpired
Read cache content, of update the content with new item if it has expired.
public onExpired(string $key, \Closure $callback): mixed
Parameters:
Parameter | Type | Description |
---|---|---|
$key | string | cache key. |
$callback | \Closure |
Return Value:
mixed
- Return cache content currently stored under key.
Throws:
- \Luminova\Exceptions\ErrorException - if the file cannot be saved
refresh
Refresh cache content with new data and expiration if necessary.
public refresh(string $key, mixed $content, int $expiration, int $expireAfter = null, bool $lock = true): bool
Parameters:
Parameter | Type | Description |
---|---|---|
$key | string | cache key. |
$content | mixed | New content to update. |
$expiration | int | cache expiry time. |
$expireAfter | int | cache expiry time after. |
$lock | bool | lock catch to avoid deletion even when cache time expired. |
Return Value:
bool
- Return true if item was successfully updated, otherwise false.
Throws:
- \Luminova\Exceptions\ErrorException - if the file cannot be saved
getItem
Get cache content from disk
public getItem(string $key, bool $onlyContent = true): mixed
Parameters:
Parameter | Type | Description |
---|---|---|
$key | string | cache key |
$onlyContent | bool | Weather to return only cache content or with metadata (default: true). |
Return Value:
mixed
- Returns data if key is valid and not expired, NULL otherwise
Throws:
- \Luminova\Exceptions\ErrorException - if the file cannot be saved
create
Creates, Reloads and retrieve cache once class is created
public create(): self
Return Value:
self
- Return cache class instance.
Throws:
- \Luminova\Exceptions\ErrorException - if there is a problem loading the cache
hasItem
Checks if cache key exist.
public hasItem(string $key): bool
Parameters:
Parameter | Type | Description |
---|---|---|
$key | string | The cache key. |
Return Value:
bool
- Return true if cache key exists, otherwise false.
deleteIfExpired
Remove expired cache by key
public deleteIfExpired(): int
Return Value:
int
- Return number of deleted expired items.
deleteItem
Deletes cache item associated with a given key.
public deleteItem(string $key): bool
Parameters:
Parameter | Type | Description |
---|---|---|
$key | string | The cache key to delete. |
Return Value:
bool
- Return true if cache was successfully deleted, otherwise false.
deleteItems
Delete cache by array keys.
public deleteItems(iterable $keys): bool
Parameters:
Parameter | Type | Description |
---|---|---|
$keys | iterable | The array of cache keys. |
Return Value:
bool
- Return true if cache was successfully deleted, otherwise false.
removeList
Remove a list of items from the collection.
public removeList(iterable<int,string> $iterable): \Generator
Parameters:
Parameter | Type | Description |
---|---|---|
$iterable | iterable<int,string> | An iterable list of keys representing items to be removed. |
Return Value:
\Generator
- Yields the result of the delete operation for each item.
Throws:
- \Luminova\Exceptions\ErrorException - if the file cannot be saved.
hasExpired
Checks if the cache timestamp has expired by key
public hasExpired(string $key): bool
Parameters:
Parameter | Type | Description |
---|---|---|
$key | string | cache key |
Return Value:
bool
- Return true if cache has expired, otherwise false.
setItem
Builds cache data and save it.
public setItem(string $key, mixed $data, int|\DateTimeInterface|null $expiration, int|\DateInterval|null $expireAfter = null, bool $lock = false): bool
Parameters:
Parameter | Type | Description |
---|---|---|
$key | string | The cache keys. |
$data | mixed | The cache contents. |
$expiration | int|\DateTimeInterface|null | cache expiration time. |
$expireAfter | int|\DateInterval|null | cache expiration time after. |
$lock | bool | Lock catch to avoid deletion even when cache time expired. |
Return Value:
bool
- Return true if cache was saved, otherwise false.
Throws:
- \Luminova\Exceptions\ErrorException - if the file cannot be saved
clear
Wipes clean the entire cache's.
public clear(bool $clearDisk = false): bool
Parameters:
Parameter | Type | Description |
---|---|---|
$clearDisk | bool | Whether to clear all cache disk |
Return Value:
bool
- Return true if cache was successfully cleared, false otherwise.
clearStorage
Remove current cache file
public clearStorage(): bool
Return Value:
bool
- Return true if file path exist else false
delete
Remove cached storage file from disk with full path.
public delete(string $storage, string $extension = self::JSON): bool
This will use the current storage path
Parameters:
Parameter | Type | Description |
---|---|---|
$storage | string | cache storage names |
$extension | string | cache file extension type |
Return Value:
bool
- Return true on success, false on failure.
deleteDisk
Remove cache file from disk with full path
public static deleteDisk(string $path, string $storage, string $extension = self::JSON): bool
Parameters:
Parameter | Type | Description |
---|---|---|
$path | string | cache full path / |
$storage | string | cache file array names |
$extension | string | cache file extension type |