Search code examples
cachingsymfony1

Different version of the cache in Symfony


I'm new in Symfony and I have a problem with logical code organisation.

The problem is connected with cache and different version of webpage for guests, logged in users and owner.

For example. I have 'user' module, which has 'show' action, and the URL is /user/show/:id and URL is the same for every visitor. But the content of the page depends on visitor and is selected with 'if' conditions so... If I clear the cache and the first visitor is guest, then others (including owner and logged in users) will see the guest's cached page.

Some kind of solution can be separating each view (owner, guest, logged in user) to partial, but it's against the DRY rule.

How to do this?


Solution

  • You can use the sf_cache_key parameter. See here how. I think you could use the user_id for logged in user, prepended with an arbitrary string for the owner, and for the guests, the string "guest" would do.

    A bit of pseudo-code to help you further:

    $sf_cache_key = '';
    if ($visitor->isLogged())
    {
        if ($visitor->getId() == $userId )
        {
           $sf_cache_key = 'owner' . $userId;
        }
        else
        {
            $sf_cache_key = 'logged_in' . $userId;
        }
    }
    else
    {
        $sf_cache_key = 'guest' . $userId;
    }