How to run code once per session?

How can I run php code once for a Wordpress session. My site reads data from the database and stores it in arrays. At the moment I have functions that are called and each time they are called they load the array from the database which is rather a waste of resource. The data won't change for the duration of the session so it would be good to load up the arrays when the wordpress site is loaded.
Is this possible?
Who is Participating?
Julian HansenConnect With a Mentor Commented:
The usual practice for solving this problem is to use dependency injection. This is where we create a resource that is required globally throughout the application and then we inject those resources into the functions or objects that require them. However, in WordPress this is not always easy to do as much of the time you are using system hooks to run code.

What you can do is create singleton functions - functions that return a saved value if it exists or look it up if it does not.

For instance

function getImportantDataFromDB()
   static $data = false;
   if (!$data) {
       // get data from DB and store it in $data

   return $data;

Open in new window

In the above code the static local variable will retain the value of the DB data when it is first retrieved - on subsequent calls it just returns the already saved value.
Jan LouwerensSoftware EngineerCommented:
How about just creating a cache for the data. Instead of loading from the database with each request, get the data from the cache. If the cache is not yet initialized, then load the data from the database.
ClintonKAuthor Commented:
Sounds like a good idea. Not something I thought was possible in php. Got any pointers as to where to get more info?
Improve Your Query Performance Tuning

In this FREE six-day email course, you'll learn from Janis Griffin, Database Performance Evangelist. She'll teach 12 steps that you can use to optimize your queries as much as possible and see measurable results in your work. Get started today!

Jan LouwerensSoftware EngineerCommented:
I'll admit I don't have a lot of experience personally using php, but maybe this link could help provide some useful information:

just where you normally load the data from the database, load it from the session instead. If it's not in the session, then load it from the database and store it in the session.
Dave BaldwinFixer of ProblemsCommented:
Even if you store it in $_SESSION variables, it still gets loaded from disk each time the page runs.  While I do this for user or session specific data, I get it from the database for all other purposes.  The 'arrays' you're talking about are not going to stay in memory between page requests.  That is especially true if you are on shared hosting.
ClintonKAuthor Commented:
I've created a function for each of the arrays and the site now appears to be far more responsive. The code is also a lot easier to read now too.
Many thanks
Julian HansenCommented:
You are welcome.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.