cache.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. <?php namespace Laravel; use Closure;
  2. class Cache {
  3. /**
  4. * All of the active cache drivers.
  5. *
  6. * @var array
  7. */
  8. public static $drivers = array();
  9. /**
  10. * The third-party driver registrar.
  11. *
  12. * @var array
  13. */
  14. public static $registrar = array();
  15. /**
  16. * Get a cache driver instance.
  17. *
  18. * If no driver name is specified, the default will be returned.
  19. *
  20. * <code>
  21. * // Get the default cache driver instance
  22. * $driver = Cache::driver();
  23. *
  24. * // Get a specific cache driver instance by name
  25. * $driver = Cache::driver('memcached');
  26. * </code>
  27. *
  28. * @param string $driver
  29. * @return Cache\Drivers\Driver
  30. */
  31. public static function driver($driver = null)
  32. {
  33. if (is_null($driver)) $driver = Config::get('cache.driver');
  34. if ( ! isset(static::$drivers[$driver]))
  35. {
  36. static::$drivers[$driver] = static::factory($driver);
  37. }
  38. return static::$drivers[$driver];
  39. }
  40. /**
  41. * Create a new cache driver instance.
  42. *
  43. * @param string $driver
  44. * @return Cache\Drivers\Driver
  45. */
  46. protected static function factory($driver)
  47. {
  48. if (isset(static::$registrar[$driver]))
  49. {
  50. $resolver = static::$registrar[$driver];
  51. return $resolver();
  52. }
  53. switch ($driver)
  54. {
  55. case 'apc':
  56. return new Cache\Drivers\APC(Config::get('cache.key'));
  57. case 'file':
  58. return new Cache\Drivers\File(path('storage').'cache'.DS);
  59. case 'memcached':
  60. return new Cache\Drivers\Memcached(Memcached::connection(), Config::get('cache.key'));
  61. case 'memory':
  62. return new Cache\Drivers\Memory;
  63. case 'redis':
  64. return new Cache\Drivers\Redis(Redis::db());
  65. case 'database':
  66. return new Cache\Drivers\Database(Config::get('cache.key'));
  67. default:
  68. throw new \Exception("Cache driver {$driver} is not supported.");
  69. }
  70. }
  71. /**
  72. * Register a third-party cache driver.
  73. *
  74. * @param string $driver
  75. * @param Closure $resolver
  76. * @return void
  77. */
  78. public static function extend($driver, Closure $resolver)
  79. {
  80. static::$registrar[$driver] = $resolver;
  81. }
  82. /**
  83. * Magic Method for calling the methods on the default cache driver.
  84. *
  85. * <code>
  86. * // Call the "get" method on the default cache driver
  87. * $name = Cache::get('name');
  88. *
  89. * // Call the "put" method on the default cache driver
  90. * Cache::put('name', 'Taylor', 15);
  91. * </code>
  92. */
  93. public static function __callStatic($method, $parameters)
  94. {
  95. return call_user_func_array(array(static::driver(), $method), $parameters);
  96. }
  97. }