I am trying to implement a hashmap (associative array in PHP) in PHP which is available application wide i.e store it in application context, it should not be lost when the program ends. How I can I achieve this in PHP?
Thanks,
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
If you are using Zend’s version of php, it’s easy.
You do not need to serialize your data.
Only contents can be cached. Resources such as filehandles can not.
To store true/false, use 1,0 so you can differentiate a cache failure from a result with
===.Store:
Retrieve:
For long lived data you can use:
For those without zend, the corresponding APC versions of the above:
Store:
Retrieve:
Never used any of the other methods mentioned.
If you don’t have shared memory available to you, you could serialize/unserialize the data to disk. Of course shared memory is much faster and the nice thing about zend is it handles concurrency issues for you and allows namespaces:
Store:
Retrieve:
Edit: To handle concurrency issues yourself, I think the easiest way would be to use locking. I can still see the possiblity of a race condition in this psuedo code between lock exists and get lock, but you get the point.
Psuedo code: