Created
May 5, 2016 20:27
-
-
Save gravataLonga/33960b71ecfd2964d66e6aa0b9b778ec to your computer and use it in GitHub Desktop.
Cache Manager, Cache Repository, Cache Factory
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| interface Factory | |
| { | |
| /** | |
| * Get a cache store instance by name. | |
| * | |
| * @param string|null $name | |
| * @return mixed | |
| */ | |
| public function store($name = null); | |
| } | |
| interface Store | |
| { | |
| /** | |
| * Retrieve an item from the cache by key. | |
| * | |
| * @param string|array $key | |
| * @return mixed | |
| */ | |
| public function get($key); | |
| /** | |
| * Store an item in the cache for a given number of minutes. | |
| * | |
| * @param string $key | |
| * @param mixed $value | |
| * @param int $minutes | |
| * @return void | |
| */ | |
| public function put($key, $value); | |
| } | |
| class ArrayStore implements Store | |
| { | |
| protected $storage = []; | |
| public function get($key) | |
| { | |
| if (array_key_exists($key, $this->storage)) { | |
| return $this->storage[$key]; | |
| } | |
| } | |
| /** | |
| * Store an item in the cache for a given number of minutes. | |
| * | |
| * @param string $key | |
| * @param mixed $value | |
| * @param int $minutes | |
| * @return void | |
| */ | |
| public function put($key, $value) | |
| { | |
| $this->storage[$key] = $value; | |
| } | |
| } | |
| interface CacheRepository | |
| { | |
| /** | |
| * Retrieve an item from the cache by key. | |
| * | |
| * @param string $key | |
| * @param mixed $default | |
| * @return mixed | |
| */ | |
| public function get($key, $default = null); | |
| public function put($key, $value); | |
| } | |
| class Repository implements CacheRepository | |
| { | |
| public $store = []; | |
| public function __construct(Store $store) | |
| { | |
| $this->store = $store; | |
| } | |
| public function put($key, $value) | |
| { | |
| $this->store->put($key, $value); | |
| } | |
| public function get($key, $default = null) | |
| { | |
| return $this->store->get($key); | |
| } | |
| } | |
| class CacheManager implements Factory | |
| { | |
| public function driver($driver = null) | |
| { | |
| return $this->store($driver); | |
| } | |
| public function store($name = null) | |
| { | |
| $name = $name ?: $this->getDefaultDriver(); | |
| return $this->stores[$name] = $this->get($name); | |
| } | |
| protected function get($name) | |
| { | |
| return isset($this->stores[$name]) ? $this->stores[$name] : $this->resolve($name); | |
| } | |
| public function getDefaultDriver() | |
| { | |
| return 'array'; | |
| } | |
| protected function resolve($name) | |
| { | |
| //$config = $this->getConfig($name); | |
| //if (is_null($config)) { | |
| // throw new InvalidArgumentException("Cache store [{$name}] is not defined."); | |
| //} | |
| $config = []; | |
| $config['driver'] = $name; | |
| $driverMethod = 'create'.ucfirst($config['driver']).'Driver'; | |
| if (method_exists($this, $driverMethod)) { | |
| return $this->{$driverMethod}($config); | |
| } else { | |
| throw new InvalidArgumentException("Driver [{$config['driver']}] is not supported."); | |
| } | |
| } | |
| protected function createArrayDriver(array $config) | |
| { | |
| return $this->repository(new ArrayStore); | |
| } | |
| public function repository(Store $store) | |
| { | |
| $repository = new Repository($store); | |
| return $repository; | |
| } | |
| } | |
| $cache = new CacheManager(); | |
| $apc = $cache->driver(); | |
| $apc->put("key", "valor"); | |
| echo $apc->get("key"); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment