Class Phalcon\Cache\Multiple

Source on GitHub

Allows to read to chained backend adapters writing to multiple backends

  1. <?php
  2. use Phalcon\Cache\Frontend\Data as DataFrontend;
  3. use Phalcon\Cache\Multiple;
  4. use Phalcon\Cache\Backend\Apc as ApcCache;
  5. use Phalcon\Cache\Backend\Memcache as MemcacheCache;
  6. use Phalcon\Cache\Backend\File as FileCache;
  7. $ultraFastFrontend = new DataFrontend(
  8. [
  9. "lifetime" => 3600,
  10. ]
  11. );
  12. $fastFrontend = new DataFrontend(
  13. [
  14. "lifetime" => 86400,
  15. ]
  16. );
  17. $slowFrontend = new DataFrontend(
  18. [
  19. "lifetime" => 604800,
  20. ]
  21. );
  22. //Backends are registered from the fastest to the slower
  23. $cache = new Multiple(
  24. [
  25. new ApcCache(
  26. $ultraFastFrontend,
  27. [
  28. "prefix" => "cache",
  29. ]
  30. ),
  31. new MemcacheCache(
  32. $fastFrontend,
  33. [
  34. "prefix" => "cache",
  35. "host" => "localhost",
  36. "port" => "11211",
  37. ]
  38. ),
  39. new FileCache(
  40. $slowFrontend,
  41. [
  42. "prefix" => "cache",
  43. "cacheDir" => "../app/cache/",
  44. ]
  45. ),
  46. ]
  47. );
  48. //Save, saves in every backend
  49. $cache->save("my-key", $data);

Methods

public __construct ([Phalcon\Cache\BackendInterface[] $backends])

Phalcon\Cache\Multiple constructor

public push (Phalcon\Cache\BackendInterface $backend)

Adds a backend

public mixed get (string | int $keyName, [int $lifetime])

Returns a cached content reading the internal backends

public start (string | int $keyName, [int $lifetime])

Starts every backend

public save ([string $keyName], [string $content], [int $lifetime], [boolean $stopBuffer])

Stores cached content into all backends and stops the frontend

public boolean delete (string | int $keyName)

Deletes a value from each backend

public exists ([string | int $keyName], [int $lifetime])

Checks if cache exists in at least one backend

public flush ()

Flush all backend(s)