Created
March 22, 2016 17:38
-
-
Save SerafimArts/af64ac4edae86a2285fa to your computer and use it in GitHub Desktop.
Laravel <=> Doctrine Cache adapter (bridge)
This file contains 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 Serafim <nesk@xakep.ru> | |
* @date 22.03.2016 20:24 | |
* | |
* For the full copyright and license information, please view the LICENSE | |
* file that was distributed with this source code. | |
*/ | |
use Doctrine\Common\Cache\Cache; | |
use Illuminate\Cache\Repository; | |
/** | |
* Class DoctrineCacheBridge | |
*/ | |
class DoctrineCacheBridge implements Cache | |
{ | |
/** | |
* @var int | |
*/ | |
private $hitsCount = 0; | |
/** | |
* @var int | |
*/ | |
private $missesCount = 0; | |
/** | |
* @var int | |
*/ | |
private $upTime; | |
/** | |
* @var Repository | |
*/ | |
private $driver; | |
/** | |
* DoctrineCacheBridge constructor. | |
* @param Repository $repository | |
*/ | |
public function __construct(Repository $repository) | |
{ | |
$this->upTime = time(); | |
$this->driver = $repository; | |
} | |
/** | |
* @param string $id | |
* @return mixed | |
*/ | |
public function fetch($id) | |
{ | |
$result = $this->driver->get($id, false); | |
if (!$result) { | |
$this->missesCount++; | |
} | |
$this->hitsCount++; | |
return $result; | |
} | |
/** | |
* @param string $id | |
* @return bool | |
*/ | |
public function contains($id) | |
{ | |
return $this->driver->has($id); | |
} | |
/** | |
* @param string $id | |
* @param mixed $data | |
* @param int $lifeTime | |
* @return bool | |
*/ | |
public function save($id, $data, $lifeTime = 0) | |
{ | |
return $this->driver->add($id, $data, $lifeTime / 60); | |
} | |
/** | |
* @param string $id | |
* @return bool | |
*/ | |
public function delete($id) | |
{ | |
return $this->driver->forget($id); | |
} | |
/** | |
* @return array | |
*/ | |
public function getStats() | |
{ | |
return [ | |
Cache::STATS_HITS => $this->hitsCount, | |
Cache::STATS_MISSES => $this->missesCount, | |
Cache::STATS_UPTIME => $this->upTime, | |
Cache::STATS_MEMORY_USAGE => null, | |
Cache::STATS_MEMORY_AVAILABLE => null, | |
]; | |
} | |
} |
This file contains 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
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE | |
Version 2, December 2004 | |
Copyright (C) 2016 Kirill Nesmeyanov <nesk@xakep.ru> | |
Everyone is permitted to copy and distribute verbatim or modified | |
copies of this license document, and changing it is allowed as long | |
as the name is changed. | |
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE | |
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION | |
0. You just DO WHAT THE FUCK YOU WANT TO. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment