Cache.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. <?php
  2. /**
  3. * Pimf
  4. *
  5. * @copyright Copyright (c) Gjero Krsteski (http://krsteski.de)
  6. * @license http://krsteski.de/new-bsd-license New BSD License
  7. */
  8. namespace Pimf;
  9. use Pimf\Util\String;
  10. use Pimf\Cache\Storages as CS;
  11. /**
  12. * Cache usage
  13. *
  14. * <code>
  15. * // Get the default cache storage instance
  16. * $storage = Cache::storage();
  17. *
  18. * // Get a specific cache storage instance by name
  19. * $storage = Cache::storage('memcached');
  20. *
  21. * // Call the "get" method on the default cache storage
  22. * $name = Cache::get('name');
  23. *
  24. * // Call the "put" method on the default cache storage
  25. * Cache::put('name', 'Robin', 15);
  26. * </code>
  27. *
  28. * @package Pimf
  29. * @author Gjero Krsteski <[email protected]>
  30. */
  31. class Cache
  32. {
  33. /**
  34. * All of the active cache storages.
  35. *
  36. * @var \Pimf\Cache\Storages\Storage[]
  37. */
  38. public static $storages = array();
  39. /**
  40. * Get a cache storage instance.
  41. *
  42. * @param string $storage
  43. *
  44. * @return CS\Apc|CS\Dba|CS\File|CS\Memcached|CS\Memory|CS\Pdo|CS\Redis|CS\WinCache
  45. */
  46. public static function storage($storage = 'memory')
  47. {
  48. if (!isset(static::$storages[$storage])) {
  49. static::$storages[$storage] = static::factory($storage);
  50. }
  51. return static::$storages[$storage];
  52. }
  53. /**
  54. * Create a new cache storage instance.
  55. *
  56. * @param string $storage
  57. *
  58. * @return CS\Apc|CS\Dba|CS\File|CS\Memcached|CS\Memory|CS\Pdo|CS\Redis|CS\WinCache
  59. * @throws \RuntimeException
  60. */
  61. protected static function factory($storage)
  62. {
  63. $conf = Registry::get('conf');
  64. switch ($storage) {
  65. case 'apc':
  66. return new CS\Apc($conf['cache']['key']);
  67. case 'file':
  68. return new CS\File($conf['cache']['storage_path']);
  69. case 'pdo':
  70. return new CS\Pdo(Pdo\Factory::get($conf['cache']['database']), $conf['cache']['key']);
  71. case 'memcached':
  72. return new CS\Memcached(Memcached::connection(), $conf['cache']['key']);
  73. case 'memory':
  74. return new CS\Memory();
  75. case 'redis':
  76. return new CS\Redis(Redis::database());
  77. case 'wincache':
  78. return new CS\WinCache($conf['cache']['key']);
  79. case 'dba':
  80. return new CS\Dba(String::ensureTrailing('/', $conf['cache']['storage_path']) . $conf['cache']['key']);
  81. default:
  82. throw new \RuntimeException("Cache storage {$storage} is not supported.");
  83. }
  84. }
  85. /**
  86. * Magic Method for calling the methods on the default cache storage.
  87. *
  88. * @param $method
  89. * @param $parameters
  90. *
  91. * @return mixed
  92. */
  93. public static function __callStatic($method, $parameters)
  94. {
  95. return call_user_func_array(
  96. array(static::storage(), $method), $parameters
  97. );
  98. }
  99. }