-
Notifications
You must be signed in to change notification settings - Fork 7.6k
Slightly better caching
I have extended the Output class to serve multiple versions of the same page to different types of users.
The problem: You need to cache a page, but it is not possible, because it differs for logged-in users and guests.
The solution: 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, number of unread messages, etc. with some tokens. Then, when we serve the cached page to the user, we substitute these tokens with values from the $_SESSION array.
The implementation: Download the 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:
/*
|--------------------------------------------------------------------------
| Cache page type source
|--------------------------------------------------------------------------
*/
$config['cache_type_source'] = array(':user:','type');
/*
|--------------------------------------------------------------------------
| Cache data source
|--------------------------------------------------------------------------
*/
$config['cache_data_source'] = array(':user:');
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 (very crude example):
<?php if ( isset($SESSION[:user:]) ): ?>
Hi {cached::name}. You are logged in as {cached::type}.
<?php else: ?>
You are not logged in.
<?php endif ?>