On Thu, 11 Apr 2002, Steve Meyers wrote:

> Here's another idea that I've floated the last couple weeks (this would be 
> especially good in the SRM and msession extensions), how about a method to 
> cache values?  This is very similar to the "application variables", except 
> that there would be a limit to how much was stored, and if that limit was 
> reached, the most infrequently used data would be discarded.
> 
> Right now I've got some lookups I have to do fairly often for country, 
> state/province, city of users.  There are over 2 million cities in my 
> database, but the vast majority of them aren't used.  I would like to be 
> able to store the most frequently used data in a persistent manner, but 
> there's really not the option of storing everything.  So if it wasn't in 
> the persistent variable, I would look it up in the database, and then put 
> it into the persistent variable.  The caching mechanism would then delete a 
> value if necessary, e.g. the least recently used.

You can already do this with SRM, by writing a Banana like:

<?php
        class Cache extends Banana
        {
                function init ($db)
                {
                        $this->db = $db;
                        $this->cache = array();
                }

                function get_city ($city)
                {
                        if (in_array ($city, array_keys ($this->cache))) {
                                return $this->cache[$city];
                        } else {
                                $r = mysql_query ("select * from City where name = 
'$city'", $this->db);
                                $row = mysql_fetch_row ($r);
                                $this->cache[$city] = $row;
                                $this->cache[$city]['timestamp'] = date();
                                return $row;
                        }

                        /* purge old entries */
                        foreach ($this->cache as $key => $entry) {
                                if ($entry['timestamp'] > date() - 3600) {
                                        unset ($this->cache['timestamp']);
                                }
                        }
                }
        }

        $banana = new Cache();
        $banana->run();
?>

Then call from your 'client' script:

<?php
        $srm = new SRM ('/var/srm.socket', 7777);

        $cache = new SRMApp ($srm, 'Cache');
        $cache->init(mysql_connect ('localhost', 'user', 'password'));

        $cache->get_city ("Koln");
?>


regards,
Derick

-----------------------------------------------------------------------
                 Did I help you? Consider a gift:
      http://www.amazon.co.uk/exec/obidos/registry/SLCB276UZU8B
-----------------------------------------------------------------------
              PHP: Scripting the Web - [EMAIL PROTECTED]
                All your branches are belong to me!
            SRM: Script Running Machine - www.vl-srm.net
-----------------------------------------------------------------------


-- 
PHP Development Mailing List <http://www.php.net/>
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to