Class Phalcon\Cache\Frontend\Data

implements Phalcon\Cache\FrontendInterface

Source on GitHub

Allows to cache native PHP data in a serialized form

  1. <?php
  2. use Phalcon\Cache\Backend\File;
  3. use Phalcon\Cache\Frontend\Data;
  4. // Cache the files for 2 days using a Data frontend
  5. $frontCache = new Data(
  6. [
  7. "lifetime" => 172800,
  8. ]
  9. );
  10. // Create the component that will cache "Data" to a 'File' backend
  11. // Set the cache file directory - important to keep the '/' at the end of
  12. // of the value for the folder
  13. $cache = new File(
  14. $frontCache,
  15. [
  16. "cacheDir" => "../app/cache/",
  17. ]
  18. );
  19. $cacheKey = "robots_order_id.cache";
  20. // Try to get cached records
  21. $robots = $cache->get($cacheKey);
  22. if ($robots === null) {
  23. // $robots is null due to cache expiration or data does not exist
  24. // Make the database call and populate the variable
  25. $robots = Robots::find(
  26. [
  27. "order" => "id",
  28. ]
  29. );
  30. // Store it in the cache
  31. $cache->save($cacheKey, $robots);
  32. }
  33. // Use $robots :)
  34. foreach ($robots as $robot) {
  35. echo $robot->name, "\n";
  36. }

Methods

public __construct ([array $frontendOptions])

Phalcon\Cache\Frontend\Data constructor

public getLifetime ()

Returns the cache lifetime

public isBuffering ()

Check whether if frontend is buffering output

public start ()

Starts output frontend. Actually, does nothing

public string getContent ()

Returns output cached content

public stop ()

Stops output frontend

public beforeStore (mixed $data)

Serializes data before storing them

public afterRetrieve (mixed $data)

Unserializes data after retrieval