cache.php 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. <?php
  2. /**
  3. * Lithium: the most rad php framework
  4. *
  5. * @copyright Copyright 2013, Union of RAD (http://union-of-rad.org)
  6. * @license http://opensource.org/licenses/bsd-license.php The BSD License
  7. */
  8. /**
  9. * This file creates a default cache configuration using the most optimized adapter available, and
  10. * uses it to provide default caching for high-overhead operations.
  11. */
  12. use lithium\storage\Cache;
  13. use lithium\core\Libraries;
  14. use lithium\core\Environment;
  15. use lithium\action\Dispatcher;
  16. use lithium\storage\cache\adapter\Apc;
  17. /**
  18. * If APC is not available and the cache directory is not writeable, bail out. This block should be
  19. * removed post-install, and the cache should be configured with the adapter you plan to use.
  20. */
  21. $cachePath = Libraries::get(true, 'resources') . '/tmp/cache';
  22. if (!(($apcEnabled = Apc::enabled()) || PHP_SAPI === 'cli') && !is_writable($cachePath)) {
  23. return;
  24. }
  25. /**
  26. * This configures the default cache, based on whether ot not APC user caching is enabled. If it is
  27. * not, file caching will be used. Most of this code is for getting you up and running only, and
  28. * should be replaced with a hard-coded configuration, based on the cache(s) you plan to use.
  29. */
  30. Cache::config(array('default' => $apcEnabled ? array('adapter' => 'Apc') : array(
  31. 'adapter' => 'File', 'strategies' => array('Serializer')
  32. )));
  33. /**
  34. * Caches paths for auto-loaded and service-located classes when in production.
  35. */
  36. Dispatcher::applyFilter('run', function($self, $params, $chain) {
  37. if (!Environment::is('production')) {
  38. return $chain->next($self, $params, $chain);
  39. }
  40. $key = md5(LITHIUM_APP_PATH) . '.core.libraries';
  41. if ($cache = Cache::read('default', $key)) {
  42. $cache = (array) $cache + Libraries::cache();
  43. Libraries::cache($cache);
  44. }
  45. $result = $chain->next($self, $params, $chain);
  46. if ($cache != Libraries::cache()) {
  47. Cache::write('default', $key, Libraries::cache(), '+1 day');
  48. }
  49. return $result;
  50. });
  51. ?>