SitePoint Sponsor

User Tag List

Results 1 to 13 of 13

Thread: Caching

  1. #1
    SitePoint Member
    Join Date
    Apr 2007
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Caching

    Is it better to cache to a mysql database or to a flat file?

    I am thinking in terms of caching a whole load of categories, about 120 each with id, parent_id, name, alias. So instead of running SELECT * FROM categories, I can do something like SELECT * FROM cache WHERE table IN (categories). This would yield a serialized table and all I would have to do is unserialize it to get the data in an array.

    I see this concept being using in invision power board where all the forums are cached.

    Thanks.

  2. #2
    SitePoint Addict
    Join Date
    Jul 2006
    Location
    Kansas City, MO
    Posts
    280
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    this is a bad idea.

  3. #3
    SitePoint Enthusiast
    Join Date
    Feb 2005
    Posts
    27
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

  4. #4
    SitePoint Addict chestertondevelopment's Avatar
    Join Date
    Dec 2005
    Location
    Essex, UK
    Posts
    241
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm not sure to be honest. Most of the time caching is used to lower access to the database. If you're accessing the database to get the cached data and then unserializing it aswell, then what advantages do you gain? Serializing/unserializing is also quite a slow activity in PHP.

    File-based caching can be useful but you again have overhead from reading the disc.

    I don't know if you've looked into other alternatives but have a look at memcached, it stores data in RAM and is very fast. Unfortuantely it's not really possible unless you have a dedicated server so it's not always a suitable option.

    Many people start caching before it's even needed (premature optimization) and I would recommend doing tests to see if it really is needed before investing time and possibly money into it.

  5. #5
    SitePoint Member
    Join Date
    May 2007
    Posts
    12
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The latest version of MySQL has a query cache. I think such a query as the OP described would be cached. However, MySQL query cache has some serious limitation that should be considered before implementing.

  6. #6
    SitePoint Addict
    Join Date
    Sep 2006
    Posts
    368
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    you want speed?

    cache that data in APC ram cache (or use memcached on a larger scale)
    http://framework.zend.com/manual/en/...e.backends.apc

    the scenario u described shouldnt result in much more than few KBs as u described and APC is perfect and fast for just that

    also for larger files for example i have a lucene search index of about 50MB i store that on a ram disk on the server. the search performance is unreal

  7. #7
    SitePoint Evangelist
    Join Date
    Aug 2004
    Posts
    428
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    lucene search implemented in zend framework

    although i've never used lucene search here is zend's implementation
    http://framework.zend.com/manual/en/...ch.lucene.html

    how is this used for caching? also could it be used as an alternative for postgresql tsearch? how does it compare? which is better?

  8. #8
    PHP/Rails Developer Czaries's Avatar
    Join Date
    May 2004
    Location
    Central USA
    Posts
    806
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Man I can't wait until all hard drives go to Solid State Disks... That would eliminate 99% of the "speed debates" we developers face...

  9. #9
    SitePoint Addict
    Join Date
    Sep 2006
    Posts
    368
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    which is better?
    zend lucene

    and even better than that a zend lucence index stored on a ramdisk

    i have a very large community forum on another site (alexa top 1000) and switching to lucene was best decision ever, cut down load from 3 servers to one

  10. #10
    SitePoint Addict Jasper Bekkers's Avatar
    Join Date
    May 2007
    Location
    The Netherlands
    Posts
    282
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Czaries View Post
    Man I can't wait until all hard drives go to Solid State Disks... That would eliminate 99% of the "speed debates" we developers face...
    We'll find another bottleneck

    CPU caches are still slow due to the speed of electrons in copper which limits the distance they can travel to like 3 cm when the CPU is at 3 Ghz if you'd want to fetch the instruction in one clock-tick (preferably). Also note that you RAM is far further way form the CPU and thus slower. So I'll imagine that even the copper in you PC will one day be gone.

    (Numbers come form memory so they are wrong, but close).
    Design patterns: trying to do Smalltalk in Java.
    I blog too, you know.

  11. #11
    PHP/Rails Developer Czaries's Avatar
    Join Date
    May 2004
    Location
    Central USA
    Posts
    806
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Jasper Bekkers View Post
    Also note that you RAM is far further way form the CPU and thus slower. So I'll imagine that even the copper in you PC will one day be gone.
    Hah! I'm still waiting for the day a full fiber-optic PC is released to the public so we can stop using precious metals like copper and gold as connectors (not to mention the tremendous speed gains of using light vs. vibration/sound waves).

  12. #12
    SitePoint Enthusiast
    Join Date
    Dec 2003
    Location
    norway
    Posts
    61
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If you use HEAP (RAM based) tables for mysql then at least you dont read from disc...

  13. #13
    SitePoint Evangelist
    Join Date
    Aug 2004
    Posts
    428
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    how much does it change based on server architecture

    how does caching change once you start scaling horizontally?

    how do you go about invalidating the cache?


    Start up plan:
    router firewall/load balancer,
    poweredge app server ,
    storage server containing database, filesystem files, cache

    future:
    router firewall/load balancer,
    poweredge app server ,
    storage server containing database
    storage server containing half of filesystem files and associated cache
    storage server containing other half of filesystem files and associated cache


    what happens if you move cache to it's own server? do any of you replicate cache?






    Scenarios:

    Say i have a photogallery. Loading up an image into gd and resizing is an expensive operation - especially if its a huge image. In the past I've used 1 machine to check if the cached existed on the file system if not i would create a resized cached version and send that to the browser. Invailidating the cache was a simple deletion process of the cached files on the filesystem.


    Page or section level caching. Another expensive operation i have is generating a full year calendar. so i save a version to filesystem. when user updates there calendar i invalidate the cache by deleting the cache on the filesystem.

    My app servers won't have the space to contain a copy of the cache.


    how do ya cache in a real life situations? cool techniques/solutions anyone?
    Last edited by leblanc; Jun 7, 2007 at 09:08.


Bookmarks

Posting Permissions

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