Luminova Framework

File Cache

Last updated: 2024-06-22 14:56:28

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 Database Builder 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 the Database 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

ConstantVisibilityTypeValue
TTL_7DAYSpublicint7 24 60 * 60
TTL_24HRpublicint24 60 60
TTL_30MINpublicint30 * 60
PHPpublicstring".catch.php"
JSONpublicstring".json"
TEXTpublicstring".txt"

Methods

constructor

Initialize cache constructor, with optional storage name and subfolder.

public __construct(string|null $storage = null, string $folder = ''): mixed

Parameters:

ParameterTypeDescription
$storagestring|nullcache storage filename to hash.
$folderstringcache storage sub folder.

getInstance

Get static Singleton Class.

public static getInstance(string|null $storage = null, string $folder = ''): static

Parameters:

ParameterTypeDescription
$storagestring|nullcache storage filename to hash
$folderstringcache storage sub folder.

setPath

Set the new cache directory path.

public setPath(string $path): self

Parameters:

ParameterTypeDescription
$pathstringcache directory must end with.

Return Value:

self - Return class instance.


setStorage

Sets the new cache file name.

public setStorage(string $storage): self

Parameters:

ParameterTypeDescription
$storagestringcache 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:

ParameterTypeDescription
$extensionstringThe file extension.

Return Value:

self - Return class instance.


setExpire

Set the expiration time of the cache item.

public setExpire(\DateTimeInterface|null $expiration): self

Parameters:

ParameterTypeDescription
$expiration\DateTimeInterface|nullThe 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:

ParameterTypeDescription
$timeint|\DateInterval|nullThe 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:

ParameterTypeDescription
$lockboollock 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:

ParameterTypeDescription
$encodingboolEnable base64 encoding or disable.

Return Value:

self - Return class instance.


enableDeleteExpired

Enable automatic cache deletion if expired.

public enableDeleteExpired(bool $allow): self

Parameters:

ParameterTypeDescription
$allowboolAuto 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:

ParameterTypeDescription
$secureboolThe 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:

ParameterTypeDescription
$keystringcache key.
$callback\Closure

Return Value:

mixed - Return cache content currently stored under key.

Throws:


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:

ParameterTypeDescription
$keystringcache key.
$contentmixedNew content to update.
$expirationintcache expiry time.
$expireAfterintcache expiry time after.
$lockboollock catch to avoid deletion even when cache time expired.

Return Value:

bool - Return true if item was successfully updated, otherwise false.

Throws:


getItem

Get cache content from disk

public getItem(string $key, bool $onlyContent = true): mixed

Parameters:

ParameterTypeDescription
$keystringcache key
$onlyContentboolWeather 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:


create

Creates, Reloads and retrieve cache once class is created

public create(): self

Return Value:

self - Return cache class instance.

Throws:


hasItem

Checks if cache key exist.

public hasItem(string $key): bool

Parameters:

ParameterTypeDescription
$keystringThe 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:

ParameterTypeDescription
$keystringThe 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:

ParameterTypeDescription
$keysiterableThe 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:

ParameterTypeDescription
$iterableiterable<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:


hasExpired

Checks if the cache timestamp has expired by key

public hasExpired(string $key): bool

Parameters:

ParameterTypeDescription
$keystringcache 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:

ParameterTypeDescription
$keystringThe cache keys.
$datamixedThe cache contents.
$expirationint|\DateTimeInterface|nullcache expiration time.
$expireAfterint|\DateInterval|nullcache expiration time after.
$lockboolLock catch to avoid deletion even when cache time expired.

Return Value:

bool - Return true if cache was saved, otherwise false.

Throws:


clear

Wipes clean the entire cache's.

public clear(bool $clearDisk = false): bool

Parameters:

ParameterTypeDescription
$clearDiskboolWhether 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:

ParameterTypeDescription
$storagestringcache storage names
$extensionstringcache 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:

ParameterTypeDescription
$pathstringcache full path /
$storagestringcache file array names
$extensionstringcache file extension type