Zebra_Cache Zebra Cache

Class: Storage_File

source file: /Storage/Storage_File.php

A file-based implementation of the Storage_Interface.

This class provides a caching mechanism that uses files on disk for storage.

It employs file locking to ensure proper functionality under heavy load. In a concurrent environment, an exclusive lock is acquired when writing to a cache file, ensuring that reads wait until the write operation is complete before fetching the cached data.

Author(s):

License:

Copyright:

  • © 2022 - 2025 Stefan Gabos

Implements interfaces:

Class methods

constructor __construct()

void __construct ( string $path , [ string $extension = '' ] )

Constructor of the class.

  1. // file-based storage
  2. $storage = new stefangabos\Zebra_Cache\Storage\Storage_File('/path/to/cache');
  3.  
  4. // instantiate the caching library using the file-based storage
  5. $cache = new stefangabos\Zebra_Cache\Zebra_Cache($storage);
  6.  
  7. // if a cached, non-expired value for the sought key does not exist
  8. if (!($my_data = $cache->get('my-key'))) {
  9.  
  10.     // do whatever you need to retrieve data
  11.     $my_data = 'my data';
  12.  
  13.     // cache the values for 10 minutes (10 x 60 seconds)
  14.     $cache->set('my-key', $my_data, 10 * 60);
  15.  
  16. }
  17.  
  18. // at this point $my_data will always contain data, either from cache, or fresh
Arguments
string $path The path where the cache files to be stored at.
string $extension

Optional The extension to suffix cache files with.

The . (dot) is not implied - you need to also add the dot if you need it

Default is '' (an empty string, nothing)

top

method delete()

void delete ( string $key )

See Zebra_Cache::delete() for documentation.

Implementation of:
Storage_Interface::delete()
Deletes a cache entry.
top

method get()

void get ( string $key )

See Zebra_Cache::get() for documentation.

Implementation of:
Storage_Interface::get()
Retrieves an item from the cache.
top

method has()

mixed has ( string $key )

See Zebra_Cache::has() for full documentation.

Tags
return:  

If a cached values exists and it is not expired, the returned array will look like

  1. [
  2.     'path'       => '', //  path to the cache file
  3.     'timeout'    => 0,  //  the original number of seconds the cache entry was supposed to be valid
  4.     'ttl'        => 0,  //  time to live, the number of seconds remaining until the cache expires
  5. ]
Implementation of:
Storage_Interface::has()
Checks if a cache entry exists.
top

method set()

void set ( string $key , $data , [ int $timeout = 0 ] )

The data will be stored in a file on disk at the path specified in the constructor, having the name:

md5($key) . '-' . $timeout . $extension

(where $extension is the one defined in the constructor)

Therefore, a cache file for the key my-key, having a timeout of 3600 seconds (one hour) and with extension set to .cache would look like

515780610702189dabd912e9c9ef6f38-3600.cache

See Zebra_Cache::set() for full documentation.

Implementation of:
Storage_Interface::set()
Stores an item in the cache.
top