cache.php 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. <?php namespace Laravel;
  2. class Cache {
  3. /**
  4. * All of the active cache drivers.
  5. *
  6. * @var Cache\Driver
  7. */
  8. public static $drivers = array();
  9. /**
  10. * Get a cache driver instance.
  11. *
  12. * If no driver name is specified, the default cache driver will be returned
  13. * as defined in the cache configuration file.
  14. *
  15. * <code>
  16. * // Get the default cache driver
  17. * $driver = Cache::driver();
  18. *
  19. * // Get the APC cache driver
  20. * $apc = Cache::driver('apc');
  21. * </code>
  22. *
  23. * @param string $driver
  24. * @return Cache\Driver
  25. */
  26. public static function driver($driver = null)
  27. {
  28. if (is_null($driver)) $driver = Config::get('cache.driver');
  29. if ( ! array_key_exists($driver, static::$drivers))
  30. {
  31. switch ($driver)
  32. {
  33. case 'file':
  34. return static::$drivers[$driver] = new Cache\File;
  35. case 'memcached':
  36. return static::$drivers[$driver] = new Cache\Memcached;
  37. case 'apc':
  38. return static::$drivers[$driver] = new Cache\APC;
  39. default:
  40. throw new \Exception("Cache driver [$driver] is not supported.");
  41. }
  42. }
  43. return static::$drivers[$driver];
  44. }
  45. /**
  46. * Pass all other methods to the default cache driver.
  47. *
  48. * Passing method calls to the driver instance provides a convenient API for the developer
  49. * when always using the default cache driver.
  50. *
  51. * <code>
  52. * // Get an item from the default cache driver
  53. * $name = Cache::get('name');
  54. * </code>
  55. */
  56. public static function __callStatic($method, $parameters)
  57. {
  58. return call_user_func_array(array(static::driver(), $method), $parameters);
  59. }
  60. }