1: <?php
2: /**
3: * This file is part of the planetubuntu package.
4: *
5: * (c) Daniel González <daniel@desarrolla2.com>
6: *
7: * For the full copyright and license information, please view the LICENSE
8: * file that was distributed with this source code.
9: */
10: namespace FastFeed\Cache;
11:
12: use FastFeed\Exception\LogicException;
13: use FastFeed\Exception;
14: use FastFeed\FastFeed as FastFeedBase;
15: use Desarrolla2\Cache\CacheInterface;
16:
17: /**
18: * FastFeed
19: */
20: class FastFeed extends FastFeedBase
21: {
22:
23: /**
24: * @var CacheInterface;
25: */
26: protected $cache;
27:
28: /**
29: * @param CacheInterface $cache
30: */
31: public function setCache(CacheInterface $cache)
32: {
33: $this->cache = $cache;
34: }
35:
36: /**
37: * @return CacheInterface
38: */
39: public function getCache()
40: {
41: return $this->cache;
42: }
43:
44: /**
45: * @param string $channel
46: *
47: * @return array|CacheInterface
48: */
49: public function fetch($channel = 'default')
50: {
51: $items = $this->getFromCache($channel);
52: if (!$items) {
53: $items = parent::fetch($channel);
54: $this->setToCache($channel, $items);
55: }
56:
57: return $items;
58: }
59:
60: /**
61: * @param string $channel
62: *
63: * @return array
64: * @throws \FastFeed\Exception\LogicException
65: */
66: protected function getFromCache($channel)
67: {
68: if (!$this->cache) {
69: throw new LogicException('You need set to cache provider');
70: }
71: if ($this->getCache()->has($channel)) {
72: return $this->getCache()->get($channel);
73: }
74:
75: return false;
76: }
77:
78: /**
79: * @param string $channel
80: * @param array $items
81: */
82: protected function setToCache($channel, $items)
83: {
84: $this->getCache()->set($channel, $items);
85: }
86: }
87: