Created
November 19, 2015 12:27
-
-
Save alexander-torosh/c7eb71d305fc4c71600d to your computer and use it in GitHub Desktop.
Phalcon CacheManager Helper
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 | |
| // ... | |
| $di->set('cacheManager', new CacheManager()); |
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 | |
| /** | |
| * @author Oleksandr Torosh <[email protected]> | |
| * @property \Phalcon\Cache\Backend\File $cache | |
| */ | |
| namespace Demio\Cache; | |
| use Phalcon\Mvc\User\Component; | |
| class Manager extends Component | |
| { | |
| /** | |
| * Get cache data. If data not exists, generate data and save to cache | |
| */ | |
| public function load($key, $closure, $lifetime = 60) | |
| { | |
| if ($lifetime == 0) { | |
| return $closure(); | |
| } | |
| $data = $this->cache->get($key, $lifetime); | |
| if (!$data) { | |
| $data = $closure(); | |
| $this->cache->save($key, $data, $lifetime); | |
| } | |
| return $data; | |
| } | |
| public function save($key, $data, $lifetime) | |
| { | |
| $this->cache->save($key, $data, $lifetime); | |
| } | |
| public function key(array $params = []) | |
| { | |
| return md5(serialize($params)); | |
| } | |
| public function delete($key) | |
| { | |
| $this->cache->delete($key); | |
| } | |
| } |
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 | |
| // $param1, $param2 ... | |
| // Cache Key | |
| $key = $this->cacheManager([ | |
| 'entityName', | |
| $param1, | |
| $param2 | |
| ]); | |
| // Cache Usage | |
| $result = $this->cacheManager($key, function() use ($param1, $param2){ | |
| // Any Logics ... | |
| $qb = $this->modelsManager->createBuilder() | |
| ->addFrom('/* ... */') | |
| ->where('/* ... */'); | |
| $result = $qb->getQuery()->execute(); | |
| return $result; | |
| }, 60); | |
| // Cache Delete | |
| $this->cacheManager->delete($key); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment