cache.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. case 'wincache':
  68. return new Cache\Drivers\WinCache(Config::get('cache.key'));
  69. default:
  70. throw new \Exception("Cache driver {$driver} is not supported.");
  71. }
  72. }
  73. /**
  74. * Register a third-party cache driver.
  75. *
  76. * @param string $driver
  77. * @param Closure $resolver
  78. * @return void
  79. */
  80. public static function extend($driver, Closure $resolver)
  81. {
  82. static::$registrar[$driver] = $resolver;
  83. }
  84. /**
  85. * Magic Method for calling the methods on the default cache driver.
  86. *
  87. * <code>
  88. * // Call the "get" method on the default cache driver
  89. * $name = Cache::get('name');
  90. *
  91. * // Call the "put" method on the default cache driver
  92. * Cache::put('name', 'Taylor', 15);
  93. * </code>
  94. */
  95. public static function __callStatic($method, $parameters)
  96. {
  97. return call_user_func_array(array(static::driver(), $method), $parameters);
  98. }
  99. }