在Laravel 4中使用具有雄辩关系的缓存标签


Using cache tags with eloquent relationships in Laravel 4

我需要添加一个redis缓存标签雄辩的模型关系,例如:

 public function children() {
     return $this->belongsToMany('table1', 'table2', 'field1', 'field2')->where('field3', 'value')->orderBy('field1', 'asc')->remember(720);
 }

,但我目前没有办法清除这个缓存,除非等待它过期。我知道可能有一种方法可以添加一个缓存名称,它可以与cache::忘记($name)清除,但我想添加相同的缓存标签到它作为许多其他条目。

谢谢

如果你想使用Redi的标签,那么将关系方法与缓存方法分开:

public function children() {
     return $this->belongsToMany('table1', 'table2', 'field1', 'field2')->where('field3', 'value')->orderBy('field1', 'asc')->remember(720);
}
public function getChildren() {
     return Cache::tags(['tag1', 'teg2])->rememberForever('cacheKey', function() {
        return $this->children()->get();
    })
}

然后如果你想清除它使用Redis的标签选项flush像这样,即:

Cache::tags('tag1')->flush();