JosephSilber / page-cache

Caches responses as static files on disk for lightning fast page loads.
MIT License
1.21k stars 118 forks source link

Each Request creates new static file #79

Open ghost opened 3 years ago

ghost commented 3 years ago

After deeply reviewing, I came to find that even if the page has got no change, it keeps updating the cached static html file. (As i see the time of the file) That is the reason I cannot request the cached page, since it keeps updating. Please help

champkris commented 3 years ago

Got this too, anybody got the solution?

champkris commented 3 years ago

Hi I seems to solve mine. I have 2 .htaccess, one in /public and another at root /

I have to place the apache mod_rewrite at root but for mine, I need to add /public in the rewrite rule & condition, so it become below:

Serve Cached Page If Available...

RewriteCond %{REQUEST_URI} ^/?$ RewriteCond %{DOCUMENT_ROOT}/public/page-cache/pcindexpc.html -f RewriteRule .? page-cache/pcindexpc.html [L] RewriteCond %{DOCUMENT_ROOT}/public/page-cache%{REQUEST_URI}.html -f RewriteRule . page-cache%{REQUEST_URI}.html [L] RewriteCond %{DOCUMENT_ROOT}/public/page-cache%{REQUEST_URI}.json -f RewriteRule . page-cache%{REQUEST_URI}.json [L]

ghost commented 3 years ago

Hi I seems to solve mine. I have 2 .htaccess, one in /public and another at root /

I have to place the apache mod_rewrite at root but for mine, I need to add /public in the rewrite rule & condition, so it become below:

Serve Cached Page If Available...

RewriteCond %{REQUEST_URI} ^/?$ RewriteCond %{DOCUMENT_ROOT}/public/page-cache/pcindexpc.html -f RewriteRule .? page-cache/pcindexpc.html [L] RewriteCond %{DOCUMENT_ROOT}/public/page-cache%{REQUEST_URI}.html -f RewriteRule . page-cache%{REQUEST_URI}.html [L] RewriteCond %{DOCUMENT_ROOT}/public/page-cache%{REQUEST_URI}.json -f RewriteRule . page-cache%{REQUEST_URI}.json [L]

Can you please come to me at TG @laptic

ghost commented 3 years ago

Hi I seems to solve mine. I have 2 .htaccess, one in /public and another at root /

I have to place the apache mod_rewrite at root but for mine, I need to add /public in the rewrite rule & condition, so it become below:

Serve Cached Page If Available...

RewriteCond %{REQUEST_URI} ^/?$ RewriteCond %{DOCUMENT_ROOT}/public/page-cache/pcindexpc.html -f RewriteRule .? page-cache/pcindexpc.html [L] RewriteCond %{DOCUMENT_ROOT}/public/page-cache%{REQUEST_URI}.html -f RewriteRule . page-cache%{REQUEST_URI}.html [L] RewriteCond %{DOCUMENT_ROOT}/public/page-cache%{REQUEST_URI}.json -f RewriteRule . page-cache%{REQUEST_URI}.json [L]

I did the same, it creates the cached file again and again even no update in page

champkris commented 3 years ago

Does your route or parameters contain any non latin character or space that will produce % sign when urldecoded? eg: having space in your route name or parameters will produce %20, then will cause the .htaccess rule not be able to find the cache file then re-cache it again. The same go to non-latin language in URL. I'm using Thai so I also face the same problem.

Good news is, I have figured out the solution by inserting urldecode() in original cache method.

Here is how I did it:

  1. go to Silber/page-cache/src/Cache.php
  2. in public function cache, just add this line before $this->files->makeDirectory($path, 0775, true, true);
$path=urldecode($path);
$file=urldecode($file); 

so your whole cache function should now look like this:

public function cache(Request $request, Response $response)
{       

        list($path, $file) = $this->getDirectoryAndFileNames($request, $response);
        $path=urldecode($path); // added urldecode to solve specialchar problem
        $file=urldecode($file); // added urldecode to solve specialchar problem

        $this->files->makeDirectory($path, 0775, true, true);

        $this->files->put(
            $this->join([$path, $file]),
            $response->getContent(),
            true
        );
}

Hope this helps.

Also some tips to test if cache is working, you can see significant decrease in request time. But there are more complicated way I also use by playing around with .htaccess in order to solve my problem. This library itself is very simple, I think you are facing the same problem as mine (% sign). Hope applying above code would be able to fix yours.

ghost commented 3 years ago

Requesting, if you can help, Above Tried but still issue.

On Sat, 26 Jun 2021, 09:49 champkris, @.***> wrote:

Does your route or parameters contain any non latin character or space that will produce % sign when urldecoded? I faced the same problem. Solve it by inserting urldecode() in original cache method.

Here is how I do it:

  1. go to Silber/page-cache/src/Cache.php
  2. in public function cache, just add this line before $this->files->makeDirectory($path, 0775, true, true);

$path=urldecode($path); $file=urldecode($file);

so your whole cache function should now look like this:

public function cache(Request $request, Response $response) { list($path, $file) = $this->getDirectoryAndFileNames($request, $response);

$path=urldecode($path);
$file=urldecode($file);

$this->files->makeDirectory($path, 0775, true, true);

$this->files->put(
    $this->join([$path, $file]),
    $response->getContent(),
    true
);

}

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/JosephSilber/page-cache/issues/79#issuecomment-868945107, or unsubscribe https://github.com/notifications/unsubscribe-auth/ALPODMT5UXL6CT2W62MUB4DTUVIOHANCNFSM4632FR2A .