Laravel 4清除所有过期的缓存


Laravel 4 clear all expired cache

在Laravel中,我们可以使用以下内容存储缓存:

Cache::put($dynamickey, 'value', $minutes);

但这将导致存储越来越多的缓存文件,即使在过期之后也是如此。如果我们尝试用php artisan cache:clearCache::flush();清理它,它将擦除所有缓存,包括那些仍然有效的缓存。

是否可以进行自动清理,只清除过期的缓存?谢谢

$value = Cache::remember('users', function()
{
    return DB::table('users')->get();
});

做这项工作。它验证具有给定键的缓存是否存在,并返回其值。如果它不存在或已过期,则使用新值刷新给定的缓存密钥。

对于图像缓存,我使用如下逻辑:

  1. 存储映像md5($file)//其中$file==完整图像路径图像名称
  2. 存储映像md5(file_get_contents($file))//自我解释方法:)
  3. 然后

    if(Cache::has($cacheKey_name)&amp!缓存::具有($cacheKey_content)){Cache::forget($cacheKey_name);Cache::forget($cacheKey_content);}

它将检查是否缓存了图像,并且只更改了内容。如果是,则删除旧缓存并缓存新图像(包含新内容)。使用此逻辑,您将始终拥有新鲜的图像内容(带有覆盖的图像)。

或者,您可以始终创建artisan任务并创建Controller来检查存储目录中的所有缓存数据,然后创建Cron任务。

您可以创建类似的函数

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);

添加一个删除所有空文件夹的功能,如破解代码

    public function forget($key)
{
    $file = $this->path($key);
    if ($this->files->exists($file))
    {
        $this->files->delete($file);
         RemoveEmptySubFolders($this->getDirectory());

要使用此功能,您可以看到使用PHP 删除空子文件夹