在Laravel中,我们可以这样存储缓存:
Cache::put($dynamickey, 'value', $minutes);但这最终会导致越来越多的缓存文件被存储,即使在它过期之后也是如此。如果我们尝试使用php artisan cache:clear或Cache::flush();清理它,它将清除所有缓存,包括那些仍然有效的缓存。
有没有可能让自动清理只清除过期的缓存?谢谢。
发布于 2014-08-08 19:23:57
$value = Cache::remember('users', function()
{
return DB::table('users')->get();
});做这件事。它验证具有给定键缓存是否存在,并返回其值。如果它不存在或过期,则使用新值刷新给定的缓存键。
对于图像缓存,我使用的逻辑如下:
if (缓存::has($cacheKey_name) &&!缓存::has($cacheKey_content)){缓存::forget($cacheKey_name);缓存::forget($cacheKey_content);}
它将检查是否缓存了图像,并且只更改了内容。如果是,则删除旧缓存并缓存新图像(具有新内容)。使用此逻辑,您将始终具有最新的图像内容(具有覆盖的图像)。
或者,您可以随时创建工匠任务和创建控制器,以检查存储目录中的所有缓存数据,然后创建Cron任务。
发布于 2015-08-28 04:54:49
您可以创建如下所示的函数
function cache($key,$value,$min){
(Cache::has($key))?Cache::put($key,$value,$min):Cache::add($key,$value,$min);
if(Cache::has('caches')){
$cache=Cache::get('caches');
$cache[time()+(60*$min)]=$key;
Cache::forget('caches');
Cache::rememberForever('caches',function() use($cache){
return $cache;
});
}else{
$cache[time()+(60*$min)]=$key;
Cache::rememberForever('caches',function() use($cache){
return $cache;
});
}
$cache=Cache::get('caches');
foreach($cache as $key=>$value)
{
if($key<time())
{
Cache::forget($value);
array_forget($cache, $key);
}
}
Cache::forget('caches');
Cache::rememberForever('caches',function() use($cache){
return $cache;
});}要删除此缓存的空文件夹,您可以编辑
vendor\laravel\framework\src\Illuminate\Cache\FileStore.php在这段代码之后的第182行
public function forget($key)
{
$file = $this->path($key);
if ($this->files->exists($file))
{
$this->files->delete($file);添加一个函数来删除所有空文件夹,如blow代码
public function forget($key)
{
$file = $this->path($key);
if ($this->files->exists($file))
{
$this->files->delete($file);
RemoveEmptySubFolders($this->getDirectory());要使用此函数,您可以查看Remove empty subfolders with PHP
https://stackoverflow.com/questions/20111229
复制相似问题