Alternative PHP Cache

Discussion in 'PHP' started by kmccarth1, Oct 7, 2008.

  1. #1
    Hey everyone,

    I'm debating installing APC for my PHP but I have some questions. For example, I read a great article about installing APC here= http://howtoforge.com/apc-php5-apache2-fedora8, but it doesn't say anything about implementation.

    If I enable my APC, will it automatically work? That doesn't sound right...
     
    kmccarth1, Oct 7, 2008 IP
  2. jestep

    jestep Prominent Member

    Messages:
    3,659
    Likes Received:
    215
    Best Answers:
    19
    Trophy Points:
    330
    #2
    The general settings give a pretty much automatic gain in performance. You can also do a lot of individual tweaking. The manual gives some decent examples of usage and different available options: http://us.php.net/apc.

    Generally, a apc.shm_size = 30 or 32 value is your primary php.ini file is good for a noticeable difference. You can them customize per domain as needed.

    It's definitely worth testing, as it is extremely easy to install and doesn't normally require recompiling apache. I use it on all of my cpanel servers, and definitely recommend it with any php server that supports it. It is supposed to be included in php6 when it is finally released.
     
    jestep, Oct 7, 2008 IP
  3. kmccarth1

    kmccarth1 Peon

    Messages:
    8
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thanks jestep- it's been awhile since I've checked out my forum post.

    I've installed APC and it was extremely easy. While benchmarking, I noticed my time/requests went down by about 15%.

    I do have a area of concern however, and it doesn't look like its really addressed online: Fragmentation. I'm operating at about 99% hit rate but my fragmentation % starts out very small then skyrockets within a couple of hours to 100%.

    Basically, I don't understand what this fragmentation means. Can anyone please explain it to this newb? Thanks forum,

    Kevin
     
    kmccarth1, Dec 11, 2008 IP
  4. jestep

    jestep Prominent Member

    Messages:
    3,659
    Likes Received:
    215
    Best Answers:
    19
    Trophy Points:
    330
    #4
    Try increasing your apc.shm_size. Try 48 at first, and then possibly 60 or even more if you keep getting fragmentation.

    If you're not, use the apc.php script to benchmark how your server reacts to changing the size.
     
    jestep, Dec 11, 2008 IP
  5. jestep

    jestep Prominent Member

    Messages:
    3,659
    Likes Received:
    215
    Best Answers:
    19
    Trophy Points:
    330
    #5
    To specifically answer the fragmentation question. APC allocates a certain amount of memory "apc.shm_size" for caching. If your server needs to cache more than what is available, the cache starts to get fragmented due the APC removing and adding to the cache (Just like a standard hard drive). If you increase the size, less fragmentation will occur. Ideally, you want 0 fragmentation, but depending on the number of php scripts and the amount of available memory, this may not be possible. After that you may want to look at using patterns to cache only certain file types, like class.php, or inc.php, etc...
     
    jestep, Dec 11, 2008 IP
  6. kmccarth1

    kmccarth1 Peon

    Messages:
    8
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    thanks for the quick reply jestep. I had my apc.shm_size value at 30 yesterday then I tried upping it to 48 today. After four hours, I get 100% fragmentation with over 155 .php files cached. You think I should try and only cache the files that are hit most regularly?

    Kevin
     
    kmccarth1, Dec 11, 2008 IP
  7. jestep

    jestep Prominent Member

    Messages:
    3,659
    Likes Received:
    215
    Best Answers:
    19
    Trophy Points:
    330
    #7
    I would definitely adjust and test. On one of my busier servers, I have it set at 128 and after a day my fragmentation is at 0 with a cache request rate of about 2000 / second.

    If you have the available memory, try setting it at 128 or so, and work your way backwards.

    If you want to cache specifics, the best are going to be controllers, configs, ORMs, and other scripts that you don't update regularly. Start with the ones that are requested the most first.
     
    jestep, Dec 11, 2008 IP
  8. kmccarth1

    kmccarth1 Peon

    Messages:
    8
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    hey jestep- I have a lot of available memory and i'm now operating at 60M. My cache request rate is really low (5/s) but my hit % is very high.

    I'm assuming that the large amount of .php files being cached is causing my fragmentation. I'm assuming I'd use the apc.filters parameter to cache only the most frequented files?
     
    kmccarth1, Dec 11, 2008 IP