"global", "persistent" variables and objects

Posted on 2005-04-09
Last Modified: 2008-02-01

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)
Question by:blowfly
    LVL 14

    Expert Comment

    Have you considered using database abstraction with ADOdb?

    They have fantastic built-in caching functions:

    They simply use PHP 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.
    LVL 25

    Accepted Solution

    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.
    LVL 7

    Expert Comment

    Me personally, I use eAccelerator. It's just about as fast as I could ask a cache to be.
    LVL 14

    Expert Comment

    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.
    LVL 7

    Expert Comment

    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:

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

    LVL 14

    Expert Comment

    My bad :) I don't know enough about eAccelerator to know that it does more than cache scripts.
    LVL 7

    Expert Comment

    It's no good without keys. It was a natural progression, like the 8088 from the 8086.
    LVL 17

    Expert Comment

    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.


    Author Comment

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

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
    Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
    Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
    This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

    759 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    10 Experts available now in Live!

    Get 1:1 Help Now