繁体   English   中英

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. 然后

    如果(Cache :: has($ cacheKey_name)&&!Cache :: has($ cacheKey_content)){Cache :: forget($ cacheKey_name); Cache :: forget($ cacheKey_content); }

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

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

您可以创建一个这样的功能

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删除空的子文件夹

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM