peter joseph
peter joseph

Reputation: 15

Which caching technique to be used for apache php

Currently i m using shared hosting domain for my site .But we have currently near about 11,00,000 rows in one of the tables.So its taking a lot of time to load the webpage.So we want to implement the database caching techniques like APC or memcache for our site.But in shared domain we dont have those facilities available,we have only eaccelerator.But eaccelerator does not cache db calls,If i m not wrong.So considering all these points we want to move to VPS and in this case.which database caching technique we need to use APC or memcache to decrease the page load time...Please guide on VPS and better caching technique of two

Upvotes: 0

Views: 305

Answers (4)

deceze
deceze

Reputation: 522085

From your comment I take it you're doing a LIKE %..% query and want to paginate the result. First of all, investigate whether FULLTEXT indices are an option for you, as they should perform better. If that's not an option, you can add a simple cache like so:

  1. Treat each unique search term as an id, i.e. if in your URL you have ..?search=foobar, then "foobar" is the id of the result set. Keep that in all your links, e.g. ..?search=foobar&page=2.
  2. If the result set does not yet exist (see below), create it:
    1. Query the database with your slow query.
    2. Get all the results into an array. Don't overdo it, you don't want to be storing hundreds of megabytes.
    3. Create a unique filename per query, e.g. sha1($query), or maybe sha1(strtolower($query)).
    4. serialize the data and store it in the file.
  3. Get the data from the file, unserialize it, display the portion of the array corresponding to the requested page.
  4. Occasionally, delete old cached results. You can do that with something like if (rand(0, 100) == 1) .., which will run the cleanup job every 100 queries on average. Strike a balance between server load and data freshness. Cache invalidation is a topic whole books can be written about, BTW.

That's a simple poor man's cache implementation. It's not great, but if you have absolutely nothing else to work with, it's better than running slow queries over and over.

Upvotes: 0

tareq
tareq

Reputation: 1129

I had a task where i needed to fetch rows from a database table that had more than 100.000 record. it was a scrollable page. So what i did was to fetch the first 50 records and cache the next 50 in the first call. and on scroll down events i wrote an ajax request to check if the data is available in cache; if not i fetched it from the database and also cached the next 50. It worked pretty well and solved the inconvenient load time.

if you have a similar scenario you might benefit from this approach.

ps: I used memcache.

Upvotes: 0

Amol
Amol

Reputation: 918

we have similar website and we use APC

APC will cache the opcode as well the html that is generated. This helps to avoid unrequired hits to the page

you should also enable caching on mysql to cache results of your query

Upvotes: 0

Tristup
Tristup

Reputation: 3663

APC is Alternative PHP Cache and works only with PHP. Whereas Memcahced will work independently with any language.

Upvotes: -1

Related Questions