-
Notifications
You must be signed in to change notification settings - Fork 26
Slightly better caching
I have extended the Output class to serve multiple versions of the same page to different types of users.
[b]The problem[/b]: You need to cache a page, but it is not possible, because it differs for logged-in users and guests.
[b]The solution[/b]: If we store information about the user in $_SESSION array, we may well cache the page, substituting the the user specific output such as name, unread message, etc . with some tokens. Then, when we serve the cached page to the user, we substitute these tokens with values from the $_SESSION array.
[b]The implementation[/b]: Download the attached class (File:MY_Output.zip) and move it into the application/libraries directory. Now you need to add two configuration options to your application/config/config.php file:
[code]/* |
---|
Cache page type source |
-------------------------------------------------------------------------- |
*/ |
$config['cache_type_source'] = array(':user:','type'); |
/* |
---|
Cache data source |
-------------------------------------------------------------------------- |
*/ |
$config['cache_data_source'] = array(':user:'); |
[/code] |
The first one determines where to find the user type in the $_SESSION. In this example $_SESSION[':user:']['type'] will be used for that purpose.
The second one determines where to find all user specific data in the $_SESSION. When outputting the cached file the script will use the $_SESSION[':user:'] all key => value pairs to substitute all {cached::key} tokens with values.
Now change your application so that all user data is stored in the $_SESSION (for instance, use PHP native session implementations from the wiki). Whenever you want to output this data inside your views, use tokens instead. For instance:
[code]<?php if ( isset($SESSION[:user:]) ): ?> Hi {cached::name}. You are logged in as {cached::type}. <?php else: ?> You are not logged in. <?php endif ?>[/code]