Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cache or store in session?

Tags:

php

I have a page that has quite a lot of data loading from my db. I'd like to speed-up loading time. I already cache query, but still the loading time is longer than I'd like it to be.

Is it possible to render a table with data and store it in a session to load on every new page refresh? I was even thinking of putting it in an external text file using ob_start();

What's the best way to handle it?

like image 623
santa Avatar asked Feb 22 '12 19:02

santa


People also ask

Which is better session or cache?

Session data is stored at the user level but caching data is stored at the application level and shared by all the users. Sessions may not improve performance whereas Cache will improve site performance. Items in cache can expire after given time to cache while items in session will stay till session expires.

What is cache and session?

A session cache allows a server to store session information from multiple clients. WebSEAL uses two types of session caches to accommodate both HTTPS and HTTP session state information between clients and WebSEAL: WebSEAL session cache.

What is the difference between session cookies and cache?

The main difference between Cache and Cookie is that, Cache is used to store online page resources during a browser for the long run purpose or to decrease the loading time. On the other hand, cookies are employed to store user choices such as browsing session to trace the user preferences.

What type of data is suitable to store in the session cache?

Images, videos, static HTML pages, JavaScript libraries and style sheets are examples of data that are often stored in cache.


1 Answers

Storing it in sessions is probably not the best idea, when you add data to a session (by default) the data is written to a file on the OS, usually in /tmp/ which means you're going to be hitting the disk quite a lot and storing just as much data.

If the data is not user specific then you could store it on disk, or in memory - (see: php.net/apc)

If the data is user specific, I recommend storing it in a distributed cache, such as Memcached (memcached.org) PHP has a library you can enable (php.net/memcached)

(by user specific I mean data like a users transactions, items, shopping cart, etc)

The logic is basically the same for any method you choose:

Memcached, user specific data example:

<?php

$memcached = new Memcached();
$data = $memcached->get('shopping-cart_' . $user_id);

if (!$data) {
    $sql = $db->query("..");
    $data = array();
    while($row = $query->fetch_assoc()) {
        $data[] = $row;
    } 
    $memcached->set('shopping-cart_' . $user_id, $data);
} 
?>
<table>
<?php 
foreach ($data as $item) {
    echo '<tr><td>' . $item['name'] .' </td></tr>';
}
?>
</table>

Global data (not user specific)

<?php
$cache_file = '/cached/pricing-structure.something';

if (file_exists($cache_file)) {
    echo $cache_file;
} else {
    // heavy query here 
    $h = fopen('/cached/pricing-structure.something', 'w+');
    fwrite($h, $data_from_query);
    fclose($h);
}
like image 83
Kieran Allen Avatar answered Sep 21 '22 16:09

Kieran Allen