Source of file Cache.php
Size: 3,017 Bytes - Last Modified: 2015-12-22T09:12:14-05:00
../src/Cache.php
12345678910111213141516171819202122232425262728293031323334353637383940414243444546
Covered by 2 test(s):
47
Covered by 2 test(s):
48
Covered by 2 test(s):
49505152535455565758596061626364
Covered by 3 test(s):
65
Covered by 3 test(s):
66
Covered by 3 test(s):
6768
Covered by 3 test(s):
6970717273747576777879
Covered by 2 test(s):
808182838485868788899091929394
Covered by 3 test(s):
95
Covered by 3 test(s):
96
Covered by 3 test(s):
9798
Covered by 3 test(s):
99100101102103104105106107108109110111
Covered by 1 test(s):
112113
Covered by 1 test(s):
114
Covered by 1 test(s):
115
Covered by 1 test(s):
116
Covered by 1 test(s):
117
Covered by 1 test(s):
118119
Covered by 1 test(s):
120
Covered by 1 test(s):
121
Covered by 1 test(s):
122
Covered by 1 test(s):
123
Covered by 1 test(s):
124125126
| <?php /** * Defines the caching mechanism * * @category PHP * @package MvcLite * @subpackage Cache * @since File available since release 2.0.0 * @author Cory Collier <corycollier@corycollier.com> */ namespace MvcLite; use MvcLite\Filter; use MvcLite\Traits\Singleton as SingletonTrait; use MvcLite\Traits\Filepath as FilepathTrait; /** * Defines the caching mechanism * * @category PHP * @package MvcLite * @subpackage Cache * @since File available since release 2.0.0 * @author Cory Collier <corycollier@corycollier.com> */ class Cache extends ObjectAbstract { use SingletonTrait; use FilepathTrait; /** * property to store the configuration of the cache object * * @var array $config */ protected $config; /** * initialize the cache instance * * @return MvcLite\Cache $this for object-chaining. */ public function init($data = []) { $defaults = ['prefix' => 'cache']; $this->config = array_merge($defaults, $data); return $this; } /** * stores data from an object. * * The object is required, to determine the namespacing of the storage * * @param MvcLite\ObjectAbstract $object * @param string $name * @param unknown_type $data * * @return MvcLite\Cache $this for object-chaining. */ public function set(ObjectAbstract $object, $name, $data) { $key = $this->getCacheKey($object, $name); $file = $this->getFilePath($key); file_put_contents($file, serialize($data)); return $this; } /** * Returns the relative filepath for a given filename * * @param string $filename * @return string */ protected function getFilePath($filename) { return $this->filepath([$this->config['directory'], $filename]); } /** * gets data for an object, and a value * * The object is required, to determine the namespacing of the storage * * @param MvcLite\ObjectAbstract $object * @param string $name * * @return mixed */ public function get(ObjectAbstract $object, $name) { $key = $this->getCacheKey($object, $name); $file = $this->getFilePath($key); $data = unserialize(file_get_contents($file)); return $data; } /** * Returns a string to namespace a cache entry. * * @param MvcLite\ObjectAbstract $object * @param string $name * * @return string */ protected function getCacheKey(ObjectAbstract $object, $name) { static $filter; if (! $filter) { $filter = new FilterChain; $filter->addFilter(new Filter\UnderscoreToDash); $filter->addFilter(new Filter\StringToLower); } return $filter->filter(implode('_', [ $this->config['prefix'], get_class($object), $name, ])); } } |