1

I have an array that gets queried each time a page is loaded, so I want to minimize the overhead and store the array into a session variable. The array file is 15kb so far. I still have to add about 300 words to each array sub key. So the file size might grow to anywhere from 100kb to 500kb. Just a guess.

The array is used to store the page data such as title, description and post content.

Here is the structure of the array:

11 main keys.

Within those main keys are sub keys anywhere from 1 to 20. Most have about 3 to 7.

each sub key has it's own array with title, description and post.

Title and description do not hold much, but post will hold approximately 300 words or less.

The values in the array will remain static.

Here's a sample of what it looks like with 2 main keys and 2 sub keys under each.

$pages = array(

'Administrator' => array(
    'network-administrator' => array('title' => 'title info here', 'description' => 'description info here', 'post' => '<p>post info here - about 300 words.</p>'),
    'database administrator' => array('title' => 'title info here', 'description' => 'description info here', 'post' => '<p>post info here - about 300 words.</p>'),
),

'Analyst' => array(
    'business systems analyst' => array('title' => 'title info here', 'description' => 'description info here', 'post' => '<p>post info here - about 300 words.</p>'),
    'data-analyst' => array('title' => 'title info here', 'description' => 'description info here', 'post' => '<p>post info here - about 300 words.</p>'),
),

);

My questions are three part.

1) Can I put this into a session variable and still be able to access the data from the session array the same way I'm accessing it directly from the array itself?

2) Is there any benefit to putting the array into a session to lessen the overhead of looping through the array on each page load?

This is how I access a value from the array

$content = $pages['Administrator']['network-administrator'];

$title = $content['title'];

$description = $content['description'];

$post = $content['post'];

3) Would I now access the array value using the same as above or writing it like this?

$pages = $_SESSION[$pages];

$content = $pages['Administrator']['network-administrator'];

$title = $content['title'];

$description = $content['description'];

$post = $content['post'];

Need some clarity, thanks for your help.

6
  • How are you maintaining the array available atm? Commented Oct 2, 2014 at 20:48
  • Biggest problem I see is space efficiency. You're going to save the same data to disk for every user who accesses your site? Commented Oct 2, 2014 at 20:48
  • why do you think it would help performance? IMO it would just put additional copy of your array to session, in addition to what you already have. I don't see how that helps. Commented Oct 2, 2014 at 20:48
  • @TheWolf If you mean from MySQL, no, I'm adding the values to the array manually. The array is kept in a separate file and Included in the template file. Commented Oct 2, 2014 at 20:52
  • 1
    Sessions are the same file storage (if not configured to work with something else). You will store a copy of the array in every session file - do you need it? I would prefer to keep serialized or jsoned data in a separate file (or use memcache or apc) Commented Oct 2, 2014 at 21:14

2 Answers 2

2

Having them in the session would increase the overhead and decrease the performance, since it would be once more stored for each user. By default sessions are stored as files, so you'd introduce extra file I/O overhead as well, increasing the load - and I don't see how storing them in the database either would be a lot better.

If you really want to increase performance of handling that data, they should be in a memory cache. Memcache or APC (as already mentioned by Cheery) are good alternatives.

However, that will only help if your array handling is really a bottleneck. Based on your description I'm really not convinced. Measure first, and only after that try to optimize.

Sign up to request clarification or add additional context in comments.

Comments

1

If the table values are "static" (not different for each user) there is no benefit putting it in session, and I think it will not improve performance at all.

Though, here are my answers to your questions :

1) You will be able to access the table like you already do, sessions can handle arrays

2) it won't lessen the overhead. Sessions are stored in files, data are serialized.

3) $pages = $_SESSION['pages'] or directly $_SESSION['pages']['Administrator']

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.