<?php
/*
* This file is part of the overtrue/wechat.
*
* (c) overtrue <i@overtrue.me>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace EasyWeChat\Kernel\Traits;
use EasyWeChat\Kernel\ServiceContainer;
use Psr\SimpleCache\CacheInterface;
use Symfony\Component\Cache\Simple\FilesystemCache;
/**
* Trait InteractsWithCache.
*
* @author overtrue <i@overtrue.me>
*/
trait InteractsWithCache
{
protected $cache;
public function getCache()
{
if ($this->cache) {
return $this->cache;
}
if (property_exists($this, 'app') && $this->app instanceof ServiceContainer
&& isset($this->app['cache']) && $this->app['cache'] instanceof CacheInterface) {
return $this->cache = $this->app['cache'];
}
return $this->cache = $this->createDefaultCache();
}
public function setCache(CacheInterface $cache)
{
$this->cache = $cache;
return $this;
}
protected function createDefaultCache()
{
return new FilesystemCache();
}
}