Link to home
Start Free TrialLog in
Avatar of blowfly
blowfly

asked on

"global", "persistent" variables and objects

Hi,

There's a table I download from the MySql database very frequently.

Rather than download it every single time, I was hoping I could some how declare a paricular object as "global" and "persistant". So it downloads it from MySql the first time, keeps it in memory, and subsequent calls to the page just use the local memory copy rather than make another database call.

Refreshing the memory resident copy could be manually forced from a sysadmin page as required.

Is this possible in PHP? (I don't really want to hard code the table into an include file, since this table is used on other SQL queries)
Avatar of cracky
cracky

Have you considered using database abstraction with ADOdb?
http://adodb.sourceforge.net

They have fantastic built-in caching functions:
http://phplens.com/adodb/caching.of.recordsets.html

They simply use PHP serialize() http://www.php.net/serialize to store a recordset object in a file and retrieve it from the same location until the time you specify expires. You can of course implement this strategy yourself, but why do this yourself when it's already been done so well already?

Let me know if you need any more help with this.
ASKER CERTIFIED SOLUTION
Avatar of Marcus Bointon
Marcus Bointon
Flag of France image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Me personally, I use eAccelerator. It's just about as fast as I could ask a cache to be.
Promethyl, isn't eAccelerator for caching PHP scripts and not database recordsets? Much like Zend Optimizer and Turck mmCache? I might be wrong, but I am not sure it will serve the purpose intended here.
Yes it will. Oh ye of narrow foresight. =) You forgot the API, sir. eAcc is Turch mmCache. It was forked and continued.

Not only will it cache his SQL queries, if properly programmatically addressed, it will cache his programs. It's a no-lose scenario.

From the top of the readme on API:
"
eaccelerator_put($key, $value, $ttl=0)
  puts the $value into shard memory for $ttl seconds.

eaccelerator_get($key)
  returns the value from shared memory which was stored by  eaccelerator_put()
  or null if it is not exists or was expired.
" src: http://cvs.sourceforge.net/viewcvs.py/eaccelerator/eaccelerator/README?rev=1.7

And an example:

        if (function_exists('eaccelerator_get') and !$_GET[refresh]) {
                $articles=eaccelerator_get(md5($where.$START.$LIMIT));
        }

        if (count($articles)<$LIMIT) {
                $result = sql_query( "select * from hk_posts $where order by post_id desc limit $START, $LIMIT" );
                $num_rows = @mysql_num_rows( $result );
                while ($line = mysql_fetch_assoc($result)) {$articles[]=$line;}
                @mysql_free_result($result);
                if (function_exists('eaccelerator_get')) {eaccelerator_put(md5($where.$START.$LIMIT),$articles, 60*60*1); }
        }

        if (!$articles) { thememainbox('Search','No results were found for your search. Would you like to refine it?<br/>'. i$


        foreach($articles as $line) {
                ....
        }

My bad :) I don't know enough about eAccelerator to know that it does more than cache scripts.
It's no good without keys. It was a natural progression, like the 8088 from the 8086.
You can also just write the caching code yourself. very easy to check the existance, and timestamp, of a file on disk, if it doesn't exist (or is stale) re-do the query and serialize/write to disk, if it does exist and isn't stale load it up and unserialize.

I do this all the time for newsfeeds, sql queries, etc.  It's just caching.  And doesn't require root access.  If you want more tricky stuff like true in-memory caching, automatic caching, but don't want to (or can) install something like eaccelerator, you can also look at enabling the query cache on mySQL, which will keep RESIDENT the results of a given query so it isn't actually doing a physical lookup, just returning you the results.  If apache/mysql are on the same box, this is effectively retained.  If they are different boxes, you may STILL see a win as typical SQL boxes have much more memory, and more available to keep things cached, than the avg webserver.

-d
Avatar of blowfly

ASKER

Thanks for the tips guys - for something quick and simple, serializing to a file sounds best.