Skip to content

Instantly share code, notes, and snippets.

@shoman4eg
Created May 19, 2016 17:34
Show Gist options
  • Save shoman4eg/8f4b4ebd2ec4ce29a0f99baeedea14c9 to your computer and use it in GitHub Desktop.
Save shoman4eg/8f4b4ebd2ec4ce29a0f99baeedea14c9 to your computer and use it in GitHub Desktop.
<?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