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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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?
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.
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

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.
Julian HansenCommented:
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.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.