SitePoint Sponsor

User Tag List

Results 1 to 21 of 21
  1. #1
    SitePoint Enthusiast
    Join Date
    Aug 2010
    Location
    Cape Town, South Africa
    Posts
    50
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Implementing Caching

    I am looking for a decent caching solution for my php applications. The thing is we use shared hosting at the moment, so installing external modules or plugins are quite a mess.

    we will however be changing to a dedicated server on which I would like to run a memcache installation, thus I need a option that allows seemless changing to memcache.

    I can offcourse write something from scratch, but would rather spend that time improving on an already developed solutions.

    i am open for discussions, so let the comments roll.

  2. #2
    SitePoint Enthusiast
    Join Date
    Aug 2010
    Location
    Cape Town, South Africa
    Posts
    50
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Also, would you go for memcache if you had the choice, or would you stick to caching to the file system?

    Or maybe a combination of the both memcache and file system? I prefer the later.

  3. #3
    SitePoint Enthusiast
    Join Date
    Aug 2010
    Location
    Cape Town, South Africa
    Posts
    50
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Stumbled appon Cache_Lite. This is available as a pear package or a download. Seems to have functionality to cache to memory and file system. Does any one have experience with this package?

  4. #4
    SitePoint Guru bronze trophy TomB's Avatar
    Join Date
    Oct 2005
    Location
    Milton Keynes, UK
    Posts
    988
    Mentioned
    9 Post(s)
    Tagged
    2 Thread(s)
    what exactly do you want to cache?

    The whole page output? Output of specific views? Return values of specific resource heavy methods?

  5. #5
    SitePoint Enthusiast
    Join Date
    Aug 2010
    Location
    Cape Town, South Africa
    Posts
    50
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Mostly results from a database. And return types of resource intense functions.

  6. #6
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    http://framework.zend.com/manual/en/...roduction.html

    Can later swap out the means of caching, file or memcache, without changing your caching logic.
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  7. #7
    @php.net Salathe's Avatar
    Join Date
    Dec 2004
    Location
    Edinburgh
    Posts
    1,396
    Mentioned
    54 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by jaco.nel007 View Post
    I am looking for a decent caching solution
    Caching solution, singular? If you're going to be caching, do it at as many levels as works best; there are a wide range of caching strategies available at many points between the visitor's browser and the deepest, darkest parts of your application.
    Salathe
    Software Developer and PHP Manual Author.

  8. #8
    SitePoint Guru bronze trophy TomB's Avatar
    Join Date
    Oct 2005
    Location
    Milton Keynes, UK
    Posts
    988
    Mentioned
    9 Post(s)
    Tagged
    2 Thread(s)
    Keep in mind you can write your own basic filesystem cache class in about 10 lines:

    PHP Code:
    class Cache {
        public 
    $tmpDir '/tmp/';

        public function 
    isCached($id) {
            return 
    file_exists($this->tmpDir $id);
        }

        public function 
    save($id$data) {
            return 
    file_put_contents($this->tmpDir $idserialize($data));
        }

        public function 
    load($idd) {
            return 
    $this->isCached($id) ? unserialize(file_get_contents($this->tmpDir $id)) : null;
        }

    Add GC or a method to reload it and you're pretty much good to go...

  9. #9
    I solve practical problems. bronze trophy
    Michael Morris's Avatar
    Join Date
    Jan 2008
    Location
    Knoxville TN
    Posts
    2,011
    Mentioned
    56 Post(s)
    Tagged
    0 Thread(s)
    The most powerful cache possible is a static file the webserver reads without starting PHP at all.

  10. #10
    SitePoint Guru bronze trophy TomB's Avatar
    Join Date
    Oct 2005
    Location
    Milton Keynes, UK
    Posts
    988
    Mentioned
    9 Post(s)
    Tagged
    2 Thread(s)
    While that's true it's very limiting. It prevents anything dynamic happening on the page (e.g. greeting the logged in user, displaying a shopping basket, etc). In the real world it's almost always better to just cache parts of the output.

  11. #11
    I solve practical problems. bronze trophy
    Michael Morris's Avatar
    Join Date
    Jan 2008
    Location
    Knoxville TN
    Posts
    2,011
    Mentioned
    56 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by tomb View Post
    while that's true it's very limiting. It prevents anything dynamic happening on the page (e.g. Greeting the logged in user, displaying a shopping basket, etc). In the real world it's almost always better to just cache parts of the output.
    (We shall see.)

  12. #12
    SitePoint Wizard Ren's Avatar
    Join Date
    Aug 2003
    Location
    UK
    Posts
    1,060
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Caching complete pages, it seems to me, is a waste of time.
    There are a multitude of methods to do this (squid, varnish, nginx to name a few) without needing to write a single line.

    I think key to using them is learning how to purge stale pages from the cache, once something changes.


    Next would be fragment caching, where a fragment is a piece of HTML, and associated cache headers (Expires, Content-Control etc) which are combined into a final page.

    Things like ESI which Varnish as partial support, would enable different caching policies for static and dynamic sections of pages.

  13. #13
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Ren View Post
    Caching complete pages, it seems to me, is a waste of time.
    There are a multitude of methods to do this (squid, varnish, nginx to name a few) without needing to write a single line...
    Aye that is a good point, when possible let the web server or other devices further down the chain handle most of the caching. For IIS 7 you can use Output Caching that is built in. I'm still investigating solutions for Apache.

    http://learn.iis.net/page.aspx/380/i...utput-caching/
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  14. #14
    SitePoint Enthusiast
    Join Date
    Aug 2010
    Location
    Cape Town, South Africa
    Posts
    50
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by logic_earth View Post
    http://framework.zend.com/manual/en/...roduction.html

    Can later swap out the means of caching, file or memcache, without changing your caching logic.
    Zend's caching is awesome, but I am unable to make use of it as it will conflict with our naming convention and thus make autoloading of classes difficult.

    Regarding page caching, I do agree, allow the web server to handling caching of complete pages. Let your code cache results and database query results.

  15. #15
    I solve practical problems. bronze trophy
    Michael Morris's Avatar
    Join Date
    Jan 2008
    Location
    Knoxville TN
    Posts
    2,011
    Mentioned
    56 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Ren View Post
    Caching complete pages, it seems to me, is a waste of time.
    Usually yes, this is the case.

    I'm not going to elaborate on what I plan because I'm not sure it will work. If it does though I'll be sharing it. Conceptually though I think it has a great shot.

  16. #16
    SitePoint Wizard Ren's Avatar
    Join Date
    Aug 2003
    Location
    UK
    Posts
    1,060
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Also for MySQL data caching, http://blog.ulf-wendel.de/?p=295

  17. #17
    SitePoint Guru dbevfat's Avatar
    Join Date
    Dec 2004
    Location
    ljubljana, slovenia
    Posts
    684
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm developing a layered cache framework, that enables you to easily cache items in any number of layers with different TTLs. It also supports prefetch (fetching items that haven't expired yet, to avoid database slams upon expiration).

    We're using it in production for over a year now, usually with DB as the source, then a Memcache layer, then APC, and sometimes Local (basically just a PHP associative array, to avoid hitting even APC, if the same object was already retrieved during the same request). Handles lots of requests and works great.

    If you're interested, check out LayerCache (basic documentation here).

  18. #18
    SitePoint Enthusiast
    Join Date
    Aug 2010
    Location
    Cape Town, South Africa
    Posts
    50
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by dbevfat View Post
    I'm developing a layered cache framework, that enables you to easily cache items in any number of layers with different TTLs. It also supports prefetch (fetching items that haven't expired yet, to avoid database slams upon expiration).

    We're using it in production for over a year now, usually with DB as the source, then a Memcache layer, then APC, and sometimes Local (basically just a PHP associative array, to avoid hitting even APC, if the same object was already retrieved during the same request). Handles lots of requests and works great.

    If you're interested, check out LayerCache (basic documentation here).

    I will definately check it out. Thank you very much.

  19. #19
    SitePoint Zealot
    Join Date
    Aug 2006
    Location
    Poland
    Posts
    108
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I use Xcache (http://xcache.lighttpd.net) on my sites - it works nice in fastcgi env (sharing cache between workers) and you can stuff data into ram for later use. You can write a brain-dead wrapper class for its functions.
    APC is very similar, although APC used to crash for me a lot in the past (under apache mod-php) and so I decided to drop it. And I think it still does not support fastcgi, so it's not an option for me anymore.

    I also think about moving to memcache (along with session data) - gonna test it soon. But I think it won't make much difference (memcache or xcache), when you use just 1 machine.

  20. #20
    SitePoint Guru dbevfat's Avatar
    Join Date
    Dec 2004
    Location
    ljubljana, slovenia
    Posts
    684
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    @dan7 APC supports fastcgi, we've been using in with Lighttpd+FastCGI for at least two years now. It's also much more stable nowadays, and will become an integral in PHP in one of the future PHP versions. Or so they say.

    Also, the purpose of APC and XCache differ from that of Memcache. They're primarily opcode cachers that run on a PHP node, and aren't sharing their cache, while Memcache is a general-purpose cache, is accessible via the network and can share its data to several servers. It also doesn't do any opcode caching, and is somewhat slower, but has more memory available in a typical install.

    They are not interchangeable, but they work fine together, though. As I mentioned eariler, we're using LayerCache for layered caching of data from DB -> Memcache -> APC (could also be XCache) -> in-request local PHP array cache, where each layer is faster, but also has less memory available.

  21. #21
    PHP Guru lampcms.com's Avatar
    Join Date
    Jan 2009
    Posts
    921
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I like memcache, but if you don't have it installed then Cache_Lite with file system backend is your next choice. It's not as fast as memcache of cause. File system entries are actually also cached by hard drive's internal cache, but that cache size if pretty small, so there will be disc reads, unavoidable, which are about 100 times slower than memcache in-memory cache.
    My project: Open source Q&A
    (similar to StackOverflow)
    powered by php+MongoDB
    Source on github, collaborators welcome!


Tags for this Thread

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
  •