cache.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. return static::$registrar[$driver]();
  51. }
  52. switch ($driver)
  53. {
  54. case 'apc':
  55. return new Cache\Drivers\APC(Config::get('cache.key'));
  56. case 'file':
  57. return new Cache\Drivers\File(path('storage').'cache'.DS);
  58. case 'memcached':
  59. return new Cache\Drivers\Memcached(Memcached::connection(), Config::get('cache.key'));
  60. case 'memory':
  61. return new Cache\Drivers\Memory;
  62. case 'redis':
  63. return new Cache\Drivers\Redis(Redis::db());
  64. case 'database':
  65. return new Cache\Drivers\Database(Config::get('cache.key'));
  66. default:
  67. throw new \Exception("Cache driver {$driver} is not supported.");
  68. }
  69. }
  70. /**
  71. * Register a third-party cache driver.
  72. *
  73. * @param string $driver
  74. * @param Closure $resolver
  75. * @return void
  76. */
  77. public static function extend($driver, Closure $resolver)
  78. {
  79. static::$registrar[$driver] = $resolver;
  80. }
  81. /**
  82. * Magic Method for calling the methods on the default cache driver.
  83. *
  84. * <code>
  85. * // Call the "get" method on the default cache driver
  86. * $name = Cache::get('name');
  87. *
  88. * // Call the "put" method on the default cache driver
  89. * Cache::put('name', 'Taylor', 15);
  90. * </code>
  91. */
  92. public static function __callStatic($method, $parameters)
  93. {
  94. return call_user_func_array(array(static::driver(), $method), $parameters);
  95. }
  96. }