...

View Full Version : caching of frequently referenced php classes/includes?



Leeoniya
11-26-2007, 11:17 PM
Hello everyone, I'm pretty proficient with structured/procedural php, but i'm starting to get into OOP and have a few questions.

I'm writing a webapp which will have between 1-5 users at any one time with a mySQL backend.

Is there a way to....or is it advisable to perhaps cache certain often used php classes inside session variables to avoid doing include and require at every script that needs to use them...such as a mySQL class for database connections, transaction processing...etc. since virtually every script will need these.

there certainly are ease of programming advantages to caching all needed classes/functions since you can readily use them during the session and read them in once during the login process.

are there performance benefits/hits for a small amount of users like in my case?

what needs to be taken into account when deciding what to cache other than how often it's needed?

thanks in advance,
Leon

Inigoesdr
11-27-2007, 12:22 AM
You probably wouldn't see much of a benefit to caching with so few users.

rpgfan3233
11-27-2007, 01:25 AM
A great list of tips on improving PHP performance can be found at http://reinholdweber.com/?p=3


Incrementing an object property (eg. $this->prop++) is 3 times slower than a local variable.

35. Not everything has to be OOP, often it is too much overhead, each method and object call consumes a lot of memory.
36. Do not implement every data structure as a class, arrays are useful, too

I'm not saying don't use OOP (it has its advantages after all), but I'm just noting that you should consider when it is proper to use OOP.

With that said, consider the amount of members an object has in addition to how often it is needed. After all, more members require more memory. Balance it out yourself and determine what works best for you since there is not really a definitive answer for this question.

CFMaBiSmAd
11-27-2007, 01:54 AM
Leeoniya, because PHP is a parsed, interpreted language, and web servers are stateless (every request is completely separate and independent), you cannot cache a portion of the code on a page, like you theorized, and certainly not in a session. The session data is by default stored in a file and doing the file access to perform an include or performing the file access to open/read the session data file would take about the same amount of time.

There are php caches (disk and memory based) that will hold the parsed byte-code version of your page (which by the way is 3-5 times the size of the original php code) that will speed up repeated access to the same page by eliminating the time taken for the parse phase.

Perhaps if you state what kind of problem you are having (expect to have) that you are trying to solve, someone can provide a solution that addresses the problem.

GJay
11-27-2007, 07:33 AM
Caching large things in the session is almost always bad, session initialisation will take longer is objects are stored and also, as the object is serialised, the class-definition still needs to be included so it can be properly unserialised.

The overhead of including files is negligigle compared to, for example, a single SQL query, look at what actually takes the time to execute and look there for optimisations. XDebug will help with this 'profiling'.

aedrin
11-27-2007, 04:19 PM
Is there a way to....or is it advisable to perhaps cache certain often used php classes inside session variables to avoid doing include and require at every script that needs to use them

Clean Answer: No, there is no way to.

You cannot cache code. You can only cache data.

(Though as mentioned, there are modules for PHP that allow you to automatically cache code. But these require modifying the server)

Leeoniya
11-28-2007, 02:30 AM
i guess that means i need to include/require the classes/functions i need to use at the beginning of each script.

thanks guys,
Leon



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum