cache.php 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. <?php namespace Laravel; isset($GLOBALS['APP_PATH']) or die('No direct script access.');
  2. class Cache {
  3. /**
  4. * All of the active cache drivers.
  5. *
  6. * @var array
  7. */
  8. public static $drivers = array();
  9. /**
  10. * Get a cache driver instance.
  11. *
  12. * If no driver name is specified, the default will be returned.
  13. *
  14. * <code>
  15. * // Get the default cache driver instance
  16. * $driver = Cache::driver();
  17. *
  18. * // Get a specific cache driver instance by name
  19. * $driver = Cache::driver('memcached');
  20. * </code>
  21. *
  22. * @param string $driver
  23. * @return Cache\Driver
  24. */
  25. public static function driver($driver = null)
  26. {
  27. if (is_null($driver)) $driver = Config::get('cache.driver');
  28. if ( ! isset(static::$drivers[$driver]))
  29. {
  30. static::$drivers[$driver] = static::factory($driver);
  31. }
  32. return static::$drivers[$driver];
  33. }
  34. /**
  35. * Create a new cache driver instance.
  36. *
  37. * @param string $driver
  38. * @return Driver
  39. */
  40. protected static function factory($driver)
  41. {
  42. switch ($driver)
  43. {
  44. case 'apc':
  45. return new Cache\Drivers\APC(Config::get('cache.key'));
  46. case 'file':
  47. return new Cache\Drivers\File($GLOBALS['STORAGE_PATH'].'cache'.DS);
  48. case 'memcached':
  49. return new Cache\Drivers\Memcached(Memcached::connection(), Config::get('cache.key'));
  50. case 'redis':
  51. return new Cache\Drivers\Redis(Redis::db());
  52. case 'database':
  53. return new Cache\Drivers\Database(Config::get('cache.key'));
  54. default:
  55. throw new \Exception("Cache driver {$driver} is not supported.");
  56. }
  57. }
  58. /**
  59. * Magic Method for calling the methods on the default cache driver.
  60. *
  61. * <code>
  62. * // Call the "get" method on the default cache driver
  63. * $name = Cache::get('name');
  64. *
  65. * // Call the "put" method on the default cache driver
  66. * Cache::put('name', 'Taylor', 15);
  67. * </code>
  68. */
  69. public static function __callStatic($method, $parameters)
  70. {
  71. return call_user_func_array(array(static::driver(), $method), $parameters);
  72. }
  73. }