Welcome Guest, Not a member yet? Register   Sign In
Flashdata is cached and repeat with refresh
#1

(This post was last modified: 02-25-2025, 01:17 AM by ozornick. Edit Reason: Reformat code )

Hi, I am using ci4 and made a project on it, now i have added cache filter to it and cache is working fine, but the issue is i have some forms in various places of project, where user can send query, but the issue is the flashdata which is showing after submitting the form is displaying with refresh means 1st time showing, which is good but when you refresh the page its showing again and is being cached, if i remove cache filter then its working fine, I tried many solutions but still no luck, added cache clear in before method, but its not working, its same, check the code,

    
PHP Code:
public function before(RequestInterface $request$arguments null)
{
    $cache Services::cache();
    $response service('response');
    $uri $request->uri->getPath();
    $cacheKey $this->sanitizeCacheKey($uri);
    // ✅ If Flashdata exists, clear cache for this page & disable caching
    if (session()->getFlashdata('success') || session()->getFlashdata('error')) {
        $cache->delete($cacheKey); // Clear cache for this page
        log_message('info'"? Cache Cleared due to Flashdata for: " $cacheKey);
        $response->setHeader('Cache-Control''no-store, no-cache, must-revalidate, max-age=0');
        $response->setHeader('Pragma''no-cache');
        return;
    }
    // ✅ Proceed with cache check
    $cachedResponse $cache->get($cacheKey);
    if ($cachedResponse instanceof ResponseInterface) {
        log_message('info'"? Cache HIT: " $cacheKey);
        return $cachedResponse;
    }
    log_message('info'"❌ Cache MISS: " $cacheKey);

Reply




Theme © iAndrew 2016 - Forum software by © MyBB