Created
May 19, 2016 17:34
-
-
Save shoman4eg/8f4b4ebd2ec4ce29a0f99baeedea14c9 to your computer and use it in GitHub Desktop.
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 | |
namespace Newkaliningrad; | |
use Bitrix\Main\Data\Cache; | |
trait CacheTrait | |
{ | |
protected $cacheTime = 3600; | |
protected $cachePath = '/datacache/'; | |
protected $cacheVar = 'result'; | |
public function setCacheTime($time) | |
{ | |
$this->cacheTime = $time; | |
} | |
public function setCachePath($path) | |
{ | |
$this->cachePath = $path; | |
} | |
public function getCache($key) | |
{ | |
$cache = Cache::createInstance(); | |
if ($cache->initCache($this->cacheTime, $key, $this->cachePath)) { | |
$vars = $cache->getVars(); | |
$result = $vars[$this->cacheVar]; | |
} else { | |
$result = false; | |
} | |
return $result; | |
} | |
public function setCache($key, $data) | |
{ | |
$cache = Cache::createInstance(); | |
$cache->startDataCache($this->cacheTime, $key, $this->cachePath); | |
$cache->endDataCache([$this->cacheVar => $data]); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment