Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a web search interface that can compare products in a table. This data set changes a few times a week.

I have been storing a "DISTINCT" list (used for parametric selection) in a cache table. The query is computationally expensive because it involves table joins and thousands of records, hence the reason to cache it.

I was wondering if it is a good idea to "cache" certain data in PHP in dynamically generated code.

The idea is that I could create a cache.php file that is 'included' which has the data in arrays for PHP to use without going to the database. This PHP file could be cached using any of the PHP compiler caches out there.

I'm not having speed issues (yet), but I don't like the idea of having to ask the database for cache data, as it seems the overhead of the php->mysql transaction is expensive.

share|improve this question
up vote 0 down vote accepted

Yes, it's fine. It's a very common practice, and I can't think of a mature framework I've used that doesn't have some sort of code generation caching as part of the optimization scheme.

That said, if your database supports views, I would recommend using that.

share|improve this answer
Generic database views won't help performance. But specifically Materialized Views (Oracle) or Indexed Views may be more helpful. – codingoutloud Feb 23 '12 at 5:37

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.