Question

I'm implementing a memcache to cache mysql query results.

It loops thru the mysql results using

while($rowP3= mysql_fetch_array($result3)) { 

or it loops thru the memcached results (if they are saved in memcache, and not expired) via

foreach($cch_active_users_final as $rowP3) {

This is probably something thats really obvious... but Im not getting it.

How can I get it to show the right looping method based on if the memcached value exists or not. I just want it to pick the right looping method. I could just duplicate the entire while { } function with all its contents, but I dont wanna repeat that huge chunk of code, just so I can change while to foreach

Any tips?

Was it helpful?

Solution

The logic should be something like:

(partial pseudocode)

$data = get_memcached_data('cch_active_users_final');

if (!$data) {
  $query = mysql_query(..);
  $data = array();
  while($row = mysql_fetch_array()) {
    $data[] = $row;
  }
  store_in_memcache('cch_active_users_final', $data);
}

// do whatever you want with $data

OTHER TIPS

If I understand your question, this code doesn't make sense. After querying database comparing queried results to cached values doesn't make sense.

If the $row and $rowP3 values contain the same data, you could have whatever happends in the loops in a function, and pass the row as an argument.

You don't, you use a unified method using PDO.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top