Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1192
  • Last Modified:

"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)
0
blowfly
Asked:
blowfly
1 Solution
 
crackyCommented:
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.
0
 
Marcus BointonCommented:
ADODB is very good. An alternative solution (effectively emulating what ADO does internally) could be to download the table on the first hit, stick it into an array and serialize it into your session. Then unserialize it each on subsequent hit.
0
 
PromethylCommented:
Me personally, I use eAccelerator. It's just about as fast as I could ask a cache to be.
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
crackyCommented:
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.
0
 
PromethylCommented:
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) {
                ....
        }

0
 
crackyCommented:
My bad :) I don't know enough about eAccelerator to know that it does more than cache scripts.
0
 
PromethylCommented:
It's no good without keys. It was a natural progression, like the 8088 from the 8086.
0
 
davebytesCommented:
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
0
 
blowflyAuthor Commented:
Thanks for the tips guys - for something quick and simple, serializing to a file sounds best.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now