Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    Regular Coder
    Join Date
    Apr 2006
    Location
    Northbrook, IL
    Posts
    394
    Thanks
    8
    Thanked 6 Times in 6 Posts

    Question caching of frequently referenced php classes/includes?

    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

  • #2
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,647
    Thanks
    2
    Thanked 406 Times in 398 Posts
    You probably wouldn't see much of a benefit to caching with so few users.

  • #3
    Regular Coder
    Join Date
    Mar 2005
    Location
    D0u$h!t3 k4?
    Posts
    512
    Thanks
    2
    Thanked 5 Times in 5 Posts
    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.
    PHP Code:
    $hello file_get_contents('hello.txt'); echo $hello
    hello

  • #4
    Senior Coder CFMaBiSmAd's Avatar
    Join Date
    Oct 2006
    Location
    Denver, Colorado USA
    Posts
    3,147
    Thanks
    2
    Thanked 333 Times in 325 Posts
    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.
    Last edited by CFMaBiSmAd; 11-27-2007 at 03:09 AM. Reason: add more
    If you are learning PHP, developing PHP code, or debugging PHP code, do yourself a favor and check your web server log for errors and/or turn on full PHP error reporting in php.ini or in a .htaccess file to get PHP to help you.

  • #5
    Senior Coder
    Join Date
    Sep 2005
    Posts
    1,791
    Thanks
    5
    Thanked 36 Times in 35 Posts
    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'.
    My thoughts on some things: http://codemeetsmusic.com
    And my scrapbook of cool things: http://gjones.tumblr.com

  • #6
    Senior Coder
    Join Date
    Jan 2007
    Posts
    1,648
    Thanks
    1
    Thanked 58 Times in 54 Posts
    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)

  • #7
    Regular Coder
    Join Date
    Apr 2006
    Location
    Northbrook, IL
    Posts
    394
    Thanks
    8
    Thanked 6 Times in 6 Posts
    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


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •