Is it correct to use APC cache to store information that is linked to a particular session?

StackOverflow https://stackoverflow.com/questions/20925085

Вопрос

I have a huge multidimensional array with configurations that i want to keep in memory. Those configurations define were the user can go in the webpage.

Initially i tought APC would cache the data only during a particular session. But quickly i realize that I was wrong.

So I come up with this idea.

#Codeigniter sets a session_id

$sessionId = $this->session->userdata('session_id');

#and with that session_id i can prefix each key stored with apc cache

$key = $session_id . '_' . 'config'; 

#then i serialize array of data

$data = serialize($configurations_array);

#LOAD APC CACHE LIBRARY

$this->load->driver('cache', array('adapter' => 'apc', 'backup' => 'file'));

#SET DATA AND CACHE LIFETIME USING THE SAME SESSION DURATION FROM CONFIG FILE

$this->cache->save($key, $data_serialized, $this->config->item('sess_expiration'));

#READ DATA FROM CACHE

$cached_configurations = $this->cache->get($key);
var_dump($cached_configurations);

#REMOVE DATA ON LOGOUT

apc_delete($key);

What do you have to say about this?

EDIT

I forgot to mention that I tested it and it works fine.

Это было полезно?

Решение

Why don't you put the data linked to the session in the session itself (obviously not in the session cookie, but on the server side)? I guess CodeIgniter provides some facility to store session data on the server side (without using APC).

Your hand-crafted solution may work but seems an unconventional way to solve a very standard problem. In plain PHP I just use $_SESSION['foo'] = 'bar' after a session_start() to store session data on the server side. The session cookie only stores the session ID.

This article explains how to use native PHP sessions with CodeIgniter. Native PHP sessions are backed by plain files, purged on a regular basis. If the session data is very large, you may consider storing it in the database.

Whatever solution you choose, my advice is to keep it simple and use standard solutions.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top