manager.php 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. <?php namespace Laravel\Cache;
  2. use Laravel\Redis;
  3. use Laravel\Config;
  4. use Laravel\Memcached;
  5. class Manager {
  6. /**
  7. * All of the active cache drivers.
  8. *
  9. * @var array
  10. */
  11. protected static $drivers = array();
  12. /**
  13. * Get a cache driver instance.
  14. *
  15. * If no driver name is specified, the default cache driver will
  16. * be returned as defined in the cache configuration file.
  17. *
  18. * <code>
  19. * // Get the default cache driver instance
  20. * $driver = Cache::driver();
  21. *
  22. * // Get a specific cache driver instance by name
  23. * $driver = Cache::driver('memcached');
  24. * </code>
  25. *
  26. * @param string $driver
  27. * @return Cache\Driver
  28. */
  29. public static function driver($driver = null)
  30. {
  31. if (is_null($driver)) $driver = Config::get('cache.driver');
  32. if ( ! array_key_exists($driver, static::$drivers))
  33. {
  34. return static::$drivers[$driver] = static::factory($driver);
  35. }
  36. return static::$drivers[$driver];
  37. }
  38. /**
  39. * Create a new cache driver instance.
  40. *
  41. * @param string $driver
  42. * @return Driver
  43. */
  44. protected static function factory($driver)
  45. {
  46. switch ($driver)
  47. {
  48. case 'apc':
  49. return new Drivers\APC(Config::get('cache.key'));
  50. case 'file':
  51. return new Drivers\File(CACHE_PATH);
  52. case 'memcached':
  53. return new Drivers\Memcached(Memcached::instance(), Config::get('cache.key'));
  54. case 'redis':
  55. return new Drivers\Redis(Redis::db());
  56. default:
  57. throw new \DomainException("Cache driver {$driver} is not supported.");
  58. }
  59. }
  60. /**
  61. * Pass all other methods to the default cache driver.
  62. *
  63. * Passing method calls to the driver instance provides a convenient API
  64. * for the developer when always using the default cache driver.
  65. *
  66. * <code>
  67. * // Call the "get" method on the default driver
  68. * $name = Cache::get('name');
  69. *
  70. * // Call the "put" method on the default driver
  71. * Cache::put('name', 'Taylor', 15);
  72. * </code>
  73. */
  74. public static function __callStatic($method, $parameters)
  75. {
  76. return call_user_func_array(array(static::driver(), $method), $parameters);
  77. }
  78. }