1: <?php
2:
3: namespace Guzzle\Service;
4:
5: use Guzzle\Cache\CacheAdapterInterface;
6:
7: /**
8: * Decorator that adds caching to a service description loader
9: */
10: class CachingConfigLoader implements ConfigLoaderInterface
11: {
12: /**
13: * @var ConfigLoaderInterface
14: */
15: protected $loader;
16:
17: /**
18: * @var CacheAdapterInterface
19: */
20: protected $cache;
21:
22: /**
23: * Add caching to a config loader
24: *
25: * @param ConfigLoaderInterface $loader Loader used to load the config when there is a cache miss
26: * @param CacheAdapterInterface $cache Object used to cache the loaded result
27: */
28: public function __construct(ConfigLoaderInterface $loader, CacheAdapterInterface $cache)
29: {
30: $this->loader = $loader;
31: $this->cache = $cache;
32: }
33:
34: /**
35: * {@inheritdoc}
36: */
37: public function load($config, array $options = array())
38: {
39: if (!is_string($config)) {
40: $key = false;
41: } else {
42: $key = 'loader_' . crc32($config);
43: if ($result = $this->cache->fetch($key)) {
44: return $result;
45: }
46: }
47:
48: $result = $this->loader->load($config, $options);
49: if ($key) {
50: $this->cache->save($key, $result);
51: }
52:
53: return $result;
54: }
55: }
56: