Documentation

FileSystemCacheHandler
in package
implements CacheInterface, StorageInterface

Real cache handler (File System).

Initially use CacheInterface (psr 16) v3, but some other library stick to v1.0.1 so library need to adjust version.

Cache metadata key :

  1. key : Cache key.
  2. createAt : Created time (unixtime).
  3. ttl : Seconds time to live.
  4. deleteAt : Time cache will delete (unixtime).

Interfaces, Classes, Traits and Enums

CacheInterface
StorageInterface
Contract or guide for implement CacheHandler class.

Table of Contents

SEPARATOR_DATA  = '||'
public class constant.
$config  : array<string|int, mixed>
Assoc array configuration.
$currentKey  : string|null
Current cache key, so no need read metadata again if key still same.
$currentMetadata  : array<string|int, mixed>|null
Current cache metadata, for next use if key not change.
__construct()  : self
Init class properties.
clear()  : bool
Clear all caches by delete metadata and content files.
delete()  : bool
Delete cache metadata and content file.
deleteMultiple()  : bool
Delete multiple cache based $keys, always return true even it fail to delete one cache.
get()  : mixed
Get cache content or return $default if cache not exists.
getCurrentKey()  : string|null
Get current cache key.
getCurrentMetadata()  : array<string|int, mixed>|null
Get current cache metadata assoc array.
getMetadata()  : array<string|int, mixed>|null
Get cache metadata.
getMultiple()  : iteratable<string|int, mixed>
Get multiple cache from storage.
getTtl()  : int
Get cache TTL, default is 'default_ttl' value.
has()  : bool
Check if cache exists and not expired, true if exists and not expired else false.
set()  : bool
Save cache metadata and content to storage.
setMultiple()  : bool
Set/save multiple data to cache, process will break if one cache fail to save.
checkFolderLocation()  : bool
Check folder location or create new if not exists.
secondDateInterval()  : int
Get second from DateInterval object.

Constants

Properties

$currentKey

Current cache key, so no need read metadata again if key still same.

private string|null $currentKey = null

$currentMetadata

Current cache metadata, for next use if key not change.

private array<string|int, mixed>|null $currentMetadata = null

Methods

__construct()

Init class properties.

public __construct(array<string|int, mixed> $config) : self
Parameters
$config : array<string|int, mixed>

Assoc array application configuration.

Return values
self

Class instance.

clear()

Clear all caches by delete metadata and content files.

public clear() : bool
Return values
bool

Process result clear cache.

delete()

Delete cache metadata and content file.

public delete(mixed $key) : bool
Parameters
$key : mixed

Cache key (will be file name).

Return values
bool

Process result delete cache.

deleteMultiple()

Delete multiple cache based $keys, always return true even it fail to delete one cache.

public deleteMultiple(iteratable<string|int, mixed> $keys) : bool
Parameters
$keys : iteratable<string|int, mixed>

Iterable/Array string key.

Return values
bool

Always return true.

get()

Get cache content or return $default if cache not exists.

public get(mixed $key[, mixed $default = null ]) : mixed
Parameters
$key : mixed

Cache key.

$default : mixed = null

Default value if no cache.

Return values
mixed

Return cache content or $default.

getCurrentKey()

Get current cache key.

public getCurrentKey() : string|null
Return values
string|null

Current cache key.

getCurrentMetadata()

Get current cache metadata assoc array.

public getCurrentMetadata() : array<string|int, mixed>|null
Return values
array<string|int, mixed>|null

Assoc array current cache metadata.

getMetadata()

Get cache metadata.

public getMetadata(string $key) : array<string|int, mixed>|null
Parameters
$key : string

Cache key metadata.

Return values
array<string|int, mixed>|null

Return assoc array if success else null.

getMultiple()

Get multiple cache from storage.

public getMultiple(iteratable<string|int, mixed> $keys[, mixed $default = null ]) : iteratable<string|int, mixed>
Parameters
$keys : iteratable<string|int, mixed>

List/Iterable string key.

$default : mixed = null

Default value for all cache keys.

Return values
iteratable<string|int, mixed>

Yield iterable $key => $value.

getTtl()

Get cache TTL, default is 'default_ttl' value.

public getTtl() : int
Return values
int

Second TTL value.

has()

Check if cache exists and not expired, true if exists and not expired else false.

public has(mixed $key) : bool
Parameters
$key : mixed

Cache key.

Return values
bool

True if cache exists else false.

set()

Save cache metadata and content to storage.

public set(mixed $key, mixed $value[, mixed $ttl = null ]) : bool
Parameters
$key : mixed

Cache key (will be file name).

$value : mixed

Cache content.

$ttl : mixed = null

Seconds interval (int).

Return values
bool

Process result save cache.

setMultiple()

Set/save multiple data to cache, process will break if one cache fail to save.

public setMultiple(iteratable<string|int, mixed> $values[, null|int|DateInterval $ttl = null ]) : bool
Parameters
$values : iteratable<string|int, mixed>

Array data to store $key => $value format.

$ttl : null|int|DateInterval = null

Seconds Time to live for all $values.

Return values
bool

checkFolderLocation()

Check folder location or create new if not exists.

private checkFolderLocation(string $location) : bool
Parameters
$location : string

Absolute or relative location folder path.

Return values
bool

Result check or create location.

secondDateInterval()

Get second from DateInterval object.

private secondDateInterval(DateInterval $ttl) : int
Parameters
$ttl : DateInterval

DateInterval instance.

Return values
int

Seconds diff from $ttl.

Search results