LRU cache implementation

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Flavio Ribeiro

    LRU cache implementation

    Hi,

    Most of my application's operations (including SQL queries and numerical
    computation) can be cached. An LRU (least recently used) cache would fit
    the bill perfectly.

    This LRU cache should be preserved over multiple requests from the same
    session. If possible, it should be seen across multiple sessions so that
    all users can benefit from it.

    I can't implement either solution. In principle I could preserve the
    cache across multiple requests by using $_SESSION, but I don't think
    this would be wise. The whole structure would weigh on the order of a
    MB, so access would probably be really, really slow.

    Also consider that access to the cache must be serialized.

    I could in principle write the C code to do the caching. But this would
    be extremely ugly since I want to cache PHP objects, not C structures.
    There would also be the overhead of converting to and from PHP's
    representation.

    Does anyone have any suggestions? Thanks for your attention.

    Best regards,

    Flavio Ribeiro
  • Manuel Lemos

    #2
    Re: LRU cache implementation

    Hello,

    On 07/14/2004 08:26 PM, Flavio Ribeiro wrote:[color=blue]
    > Most of my application's operations (including SQL queries and numerical
    > computation) can be cached. An LRU (least recently used) cache would fit
    > the bill perfectly.
    >
    > This LRU cache should be preserved over multiple requests from the same
    > session. If possible, it should be seen across multiple sessions so that
    > all users can benefit from it.
    >
    > I can't implement either solution. In principle I could preserve the
    > cache across multiple requests by using $_SESSION, but I don't think
    > this would be wise. The whole structure would weigh on the order of a
    > MB, so access would probably be really, really slow.
    >
    > Also consider that access to the cache must be serialized.
    >
    > I could in principle write the C code to do the caching. But this would
    > be extremely ugly since I want to cache PHP objects, not C structures.
    > There would also be the overhead of converting to and from PHP's
    > representation.[/color]

    You may want to try this class for caching arbitrary information in disk
    files so you do not have to mess up with sessions.




    --

    Regards,
    Manuel Lemos

    PHP Classes - Free ready to use OOP components written in PHP
    Free PHP Classes and Objects 2025 Versions with PHP Example Scripts, PHP Tutorials, Download PHP Scripts, PHP articles, Remote PHP Jobs, Hire PHP Developers, PHP Book Reviews, PHP Language OOP Materials


    PHP Reviews - Reviews of PHP books and other products


    Metastorage - Data object relational mapping layer generator

    Comment

    • steve

      #3
      Re: LRU cache implementation

      > Hi,[color=blue]
      >
      > Most of my application’s operations (including SQL queries and[/color]
      numerical[color=blue]
      > computation) can be cached. An LRU (least recently used) cache[/color]
      would fit[color=blue]
      > the bill perfectly.
      >
      > This LRU cache should be preserved over multiple requests from the[/color]
      same[color=blue]
      > session. If possible, it should be seen across multiple sessions so[/color]
      that[color=blue]
      > all users can benefit from it.
      >
      > I can’t implement either solution. In principle I could preserve[/color]
      the[color=blue]
      > cache across multiple requests by using $_SESSION, but I don’t[/color]
      think[color=blue]
      > this would be wise. The whole structure would weigh on the order of[/color]
      a[color=blue]
      > MB, so access would probably be really, really slow.
      >
      > Also consider that access to the cache must be serialized.
      >
      > I could in principle write the C code to do the caching. But this[/color]
      would[color=blue]
      > be extremely ugly since I want to cache PHP objects, not C[/color]
      structures.[color=blue]
      > There would also be the overhead of converting to and from PHP’s
      > representation.
      >
      > Does anyone have any suggestions? Thanks for your attention.
      >
      > Best regards,
      >
      > Flavio Ribeiro[/color]

      You could save the info to mysql. Mysql has caching, so if the data
      does not change often, access will be from ram. This way, you have
      fast random access to your data.

      --
      http://www.dbForumz.com/ This article was posted by author's request
      Articles individually checked for conformance to usenet standards
      Topic URL: http://www.dbForumz.com/PHP-LRU-cach...ict129590.html
      Visit Topic URL to contact author (reg. req'd). Report abuse: http://www.dbForumz.com/eform.php?p=432410

      Comment

      • Flavio Ribeiro

        #4
        Re: LRU cache implementation

        steve wrote:[color=blue]
        >
        > You could save the info to mysql. Mysql has caching, so if the data
        > does not change often, access will be from ram. This way, you have
        > fast random access to your data.
        >[/color]

        Saving to MySQL (or PostgreSQL in my case) would force me to serialize
        the data. I'd really like to avoid the serialization/SQL overhead.

        Flavio

        Comment

        • steve

          #5
          Re: Re: LRU cache implementation

          > steve wrote:[color=blue][color=green]
          > >
          > > You could save the info to mysql. Mysql has caching, so if the[/color][/color]
          data[color=blue][color=green]
          > > does not change often, access will be from ram. This way, you[/color][/color]
          have[color=blue][color=green]
          > > fast random access to your data.
          > >[/color]
          >
          > Saving to MySQL (or PostgreSQL in my case) would force me to[/color]
          serialize[color=blue]
          > the data. I’d really like to avoid the serialization/SQL overhead.
          >
          > Flavio[/color]
          Yes, Flavio. You have two problems:
          (a) how to avoid serialization
          (b) how to preserve large amounts of data across sessions

          I don’t have the answer to (a). But as far as (b), I believe the best
          approach is to save that to shared memory. For the most efficient
          approach IMHO, you can consider something like mmcache which can write
          variables to shared memory. Also you can use something like Cache
          Lite which writes the data to disk... but Linux would most likely
          cache the disk file in memory anyways.

          Never heard of a way to avoid serialization. But others may have a
          solution I hope.

          In 90% of applications, db access is the time-consuming factor. Since
          mysql is already caching, are your computations so intensive as to
          warrant writing extra code for caching this way. Also serialization
          is a php internal function and presumably very fast. I suggest
          running benchmarks to see what is being implemented is worth the
          effort.

          --
          http://www.dbForumz.com/ This article was posted by author's request
          Articles individually checked for conformance to usenet standards
          Topic URL: http://www.dbForumz.com/PHP-LRU-cach...ict129590.html
          Visit Topic URL to contact author (reg. req'd). Report abuse: http://www.dbForumz.com/eform.php?p=432600

          Comment

          • Flavio Ribeiro

            #6
            Re: LRU cache implementation

            > Yes, Flavio. You have two problems:[color=blue]
            > (a) how to avoid serialization
            > (b) how to preserve large amounts of data across sessions
            >
            > I don’t have the answer to (a). But as far as (b), I believe the best
            > approach is to save that to shared memory. For the most efficient
            > approach IMHO, you can consider something like mmcache which can write
            > variables to shared memory. Also you can use something like Cache
            > Lite which writes the data to disk... but Linux would most likely
            > cache the disk file in memory anyways.
            >
            > Never heard of a way to avoid serialization. But others may have a
            > solution I hope.
            >
            > In 90% of applications, db access is the time-consuming factor. Since
            > mysql is already caching, are your computations so intensive as to
            > warrant writing extra code for caching this way. Also serialization
            > is a php internal function and presumably very fast. I suggest
            > running benchmarks to see what is being implemented is worth the
            > effort.
            >[/color]

            I've checked mmcache out, and it completely solves my problem :)
            Suddenly it feels like I have application variables in PHP.

            I don't know if it serializes the objects or if it just dumps their
            binary forms to shared memory (I'll look at the source later), but it's
            fast even for large cache sizes.

            Cache_Lite and Cache are okay for what they were designed to do, but
            mmcache seems like a much nicer solution.

            Many thanks for your help, Steve.

            Best regards,

            Flavio

            Comment

            Working...