Learn how to a build a cloud-first strategyRegister Now

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

"global", "persistent" variables and objects


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)
1 Solution
Have you considered using database abstraction with ADOdb?

They have fantastic built-in caching functions:

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.
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.
Me personally, I use eAccelerator. It's just about as fast as I could ask a cache to be.
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.

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.

  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]) {

        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;}
                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.

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

Featured Post


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