Jump to content
nik_jain

Caching a PHP array in memory across different requests.

Recommended Posts

Purpose: Building a search function for a site, that's supposed to be fast, give results as a user types.

Queries would be something like: "brand1 brand2 brand3" .

My idea is , instead of querying the database on each ajax request. A keyed array is created once. Like: ['brand1' => id , 'brand2 => id2 ]. This array is stored in memory. The next time a query is sent, the array which is instantly available in memory, and can be simply queried $storedArray['brand1']to fetch any existing ids instantly. The array size would be about 750 Kb. 60000 rows.

I don't have much experience with caching , so looking for advise whether what I am trying to do even makes any sense or necessary. I know there are solutions like memcache. But not sure if my tiny project requires them. Also does opcache help here ? Would serializing the array be too slow ? 

Please ask if any questions.

Thanks

Share this post


Link to post
Share on other sites

opcache is for code, not data.

Don't serialize: whatever you use to cache will serialize for you.

A real memory cache like memcached or redis would be best. Can you?

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.