你好,我正在创建一个自定义缓存服务类,它将抽象出我的存储库中的缓存层。然而,我遇到了一些问题,因为我得到了这个错误:
Argument 1 passed to Task::__construct() must implement interface MyApp\Cache\CacheInterface, none given, called in /var/www/app/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php on line 792 and defined
我的类如下所示:
<?php namespace MyApp\Cache;
use Illuminate\Cache\CacheManager;
class CacheService {
/**
* @var Illuminate\Cache\CacheManager
*/
protected $cache;
/**
* @var integer
*/
protected $minutes;
/**
* Construct
*
* @param Illuminate\Cache\CacheManager $cache
* @param string $tag
* @param integer $minutes
*/
public function __construct(CacheManager $cache, $minutes = 60)
{
$this->cache = $cache;
$this->tag = $tag;
$this->minutes = $minutes;
}
/**
* Get
*
* @param string $key
* @return mixed
*/
public function get($key)
{
return $this->cache->tags($this->tag)->get($key);
}
/**
* Put
*
* @param string $key
* @param mixed $value
* @param integer $minutes
* @return mixed
*/
public function put($key, $value, $minutes = null)
{
if( is_null($minutes) )
{
$minutes = $this->minutes;
}
return $this->cache->tags($this->tag)->put($key, $value, $minutes);
}
/**
* Has
*
* @param string $key
* @return bool
*/
public function has($key)
{
return $this->cache->tags($this->tag)->has($key);
}
}
在我的模型中,我有以下内容;
<?php
use Abstracts\Model as AbstractModel;
use Illuminate\Support\Collection;
use CMS\APIv2\Objects\Entity;
use MyApp/Cache\CacheInterface;
class SprintTask extends AbstractModel
{
/**
* @var CacheInterface
*/
protected $cache;
public function __construct(CacheInterface $cache)
{
$this->cache = $cache;
}
public static function scopegetAssignedSprint($id) {
$key = md5('id.'.$id.get_class());
if($this->cache->has($key))
{
return $this->cache->get($key);
}
$user = static::where('uid', $id)->lists('sprint_id');
$this->cache->put($key, $user);
return $user;
}
我有一个缓存服务提供者,如下所示:
<?php
namespace MyApp\Cache;
use MyApp\Cache\CacheInterface;
use Illuminate\Support\ServiceProvider;
class CacheServiceProvider extends ServiceProvider
{
/**
* Indicates if loading of the provider is deferred.
*
* @var bool
*/
protected $defer = false;
/**
* Register
*/
public function register()
{
$this->app->bind
('MyApp\Cache\CacheInterface',
'MyApp\Cache\CacheService');
}
}
有没有什么想法可以正确设置这个服务提供商,以便在任何模式/控制器/存储库等中使用?
Class 'MyApp\Cache\MyApp\Services\Cache\CacheService'
not found,并且不得不在CacheManager和$cache之间添加逗号。有任何想法出了什么问题吗?我现在正在使用这段代码;$this->app->bind('MyApp\Cache\CacheInterface',function(){ return new MyApp\Cache\CacheService(CacheManager, $cache); });
- user0129e021939232composer dump
来卸载你的数据,以便它们可以再次生成加载器?还有一件事是你不应该在CacheManage
和$cache
之间添加逗号,最后你应该检查你的代码,因为在CacheService
构造函数中缺少了$tag
参数。 - Zaher