Skip to main content
Tweeted twitter.com/StackCodeReview/status/741469922434371588
added 109 characters in body
Source Link
JPJens
  • 133
  • 5

Save in memcache

// Store memcache data
function setCache($key,$value,$storage=18) {
    $memcache_obj = new Memcache;
    $memcache_obj = memcache_connect(memcache_host, 11211);

    if(is_array($key)) {
        $i = 0;
        foreach($key as $id => $storeName) {
            $memcache_obj->set($storeName, $value[$id], 0, $storage);   
        }
    }else{
        $memcache_obj->set($key, $value, 0, $storage);
    }
}
 

Get from memcache

// Get memcache data
function getCache($key) {
    $memcache_obj = new Memcache;
    $memcache_obj = memcache_connect(memcache_host, 11211);
    $res = $memcache_obj->get($key);
    if(empty($res)) {
        $res = false;   // Expired
    }
    return $res;
}
 

Get data from memcache (or store in memcache if not present)

// Get data from either memcache or mysql
function _getData($userid,$table,$fields,$server) {

    $toSelect = explode(",",$fields);
    $toPush = array();
    foreach($toSelect AS &$value) {
        // Check if data is available from cache
        $key = $userid."_".$table."_".$value;
        $res[$value] = getCache($key);
        if(empty($res[$value])) {
            // Not cached, so must be pushed
            $toPush[] = $value;
        }
    }

    if($toPush) {
        // Some or all data missing from cache, so we fetch and cache it
        $fieldsToSelect = implode(",",$toPush);
        $q = _query("SELECT ".$fieldsToSelect." FROM ".$table." WHERE id = '".$userid."'",$server);
        $row = _rows($q);
        $key = array();
        $cValue = array();
        foreach($toPush AS &$value) {
            $key[] = $userid."_".$table."_".$value;
            $cValue[] = $row[$value];
            $res[$value] = $row[$value];
        }
        setCache($key,$cValue);
    }
    return $res;

}
// Store memcache data
function setCache($key,$value,$storage=18) {
    $memcache_obj = new Memcache;
    $memcache_obj = memcache_connect(memcache_host, 11211);

    if(is_array($key)) {
        $i = 0;
        foreach($key as $id => $storeName) {
            $memcache_obj->set($storeName, $value[$id], 0, $storage);   
        }
    }else{
        $memcache_obj->set($key, $value, 0, $storage);
    }
}
 
// Get memcache data
function getCache($key) {
    $memcache_obj = new Memcache;
    $memcache_obj = memcache_connect(memcache_host, 11211);
    $res = $memcache_obj->get($key);
    if(empty($res)) {
        $res = false;   // Expired
    }
    return $res;
}
 
// Get data from either memcache or mysql
function _getData($userid,$table,$fields,$server) {

    $toSelect = explode(",",$fields);
    $toPush = array();
    foreach($toSelect AS &$value) {
        // Check if data is available from cache
        $key = $userid."_".$table."_".$value;
        $res[$value] = getCache($key);
        if(empty($res[$value])) {
            // Not cached, so must be pushed
            $toPush[] = $value;
        }
    }

    if($toPush) {
        // Some or all data missing from cache, so we fetch and cache it
        $fieldsToSelect = implode(",",$toPush);
        $q = _query("SELECT ".$fieldsToSelect." FROM ".$table." WHERE id = '".$userid."'",$server);
        $row = _rows($q);
        $key = array();
        $cValue = array();
        foreach($toPush AS &$value) {
            $key[] = $userid."_".$table."_".$value;
            $cValue[] = $row[$value];
            $res[$value] = $row[$value];
        }
        setCache($key,$cValue);
    }
    return $res;

}

Save in memcache

// Store memcache data
function setCache($key,$value,$storage=18) {
    $memcache_obj = new Memcache;
    $memcache_obj = memcache_connect(memcache_host, 11211);

    if(is_array($key)) {
        $i = 0;
        foreach($key as $id => $storeName) {
            $memcache_obj->set($storeName, $value[$id], 0, $storage);   
        }
    }else{
        $memcache_obj->set($key, $value, 0, $storage);
    }
}

Get from memcache

// Get memcache data
function getCache($key) {
    $memcache_obj = new Memcache;
    $memcache_obj = memcache_connect(memcache_host, 11211);
    $res = $memcache_obj->get($key);
    if(empty($res)) {
        $res = false;   // Expired
    }
    return $res;
}

Get data from memcache (or store in memcache if not present)

// Get data from either memcache or mysql
function _getData($userid,$table,$fields,$server) {

    $toSelect = explode(",",$fields);
    $toPush = array();
    foreach($toSelect AS &$value) {
        // Check if data is available from cache
        $key = $userid."_".$table."_".$value;
        $res[$value] = getCache($key);
        if(empty($res[$value])) {
            // Not cached, so must be pushed
            $toPush[] = $value;
        }
    }

    if($toPush) {
        // Some or all data missing from cache, so we fetch and cache it
        $fieldsToSelect = implode(",",$toPush);
        $q = _query("SELECT ".$fieldsToSelect." FROM ".$table." WHERE id = '".$userid."'",$server);
        $row = _rows($q);
        $key = array();
        $cValue = array();
        foreach($toPush AS &$value) {
            $key[] = $userid."_".$table."_".$value;
            $cValue[] = $row[$value];
            $res[$value] = $row[$value];
        }
        setCache($key,$cValue);
    }
    return $res;

}
Let's not judge who is a "real" developer
Source Link
200_success
  • 145.6k
  • 22
  • 191
  • 481

Ideal way of storing mysql Storing MySQL data in memcachedMemcache

The above works well, but how much does it hurt the eye of a real developer ?

Thank you

Ideal way of storing mysql data in memcached

The above works well, but how much does it hurt the eye of a real developer ?

Thank you

Storing MySQL data in Memcache

The above works well, but how much does it hurt the eye?

Source Link
JPJens
  • 133
  • 5

Ideal way of storing mysql data in memcached

I have written a script that checks if data is known in memcached, and if not it queries it from the mysql db and stores it.

I would appreciate any inputs if i have done this the "correct" way, since i'm trying to optimize the code as much as possible.

_query() & _rows() are simple mysqli functions

// Store memcache data
function setCache($key,$value,$storage=18) {
    $memcache_obj = new Memcache;
    $memcache_obj = memcache_connect(memcache_host, 11211);

    if(is_array($key)) {
        $i = 0;
        foreach($key as $id => $storeName) {
            $memcache_obj->set($storeName, $value[$id], 0, $storage);   
        }
    }else{
        $memcache_obj->set($key, $value, 0, $storage);
    }
}

// Get memcache data
function getCache($key) {
    $memcache_obj = new Memcache;
    $memcache_obj = memcache_connect(memcache_host, 11211);
    $res = $memcache_obj->get($key);
    if(empty($res)) {
        $res = false;   // Expired
    }
    return $res;
}

// Get data from either memcache or mysql
function _getData($userid,$table,$fields,$server) {

    $toSelect = explode(",",$fields);
    $toPush = array();
    foreach($toSelect AS &$value) {
        // Check if data is available from cache
        $key = $userid."_".$table."_".$value;
        $res[$value] = getCache($key);
        if(empty($res[$value])) {
            // Not cached, so must be pushed
            $toPush[] = $value;
        }
    }

    if($toPush) {
        // Some or all data missing from cache, so we fetch and cache it
        $fieldsToSelect = implode(",",$toPush);
        $q = _query("SELECT ".$fieldsToSelect." FROM ".$table." WHERE id = '".$userid."'",$server);
        $row = _rows($q);
        $key = array();
        $cValue = array();
        foreach($toPush AS &$value) {
            $key[] = $userid."_".$table."_".$value;
            $cValue[] = $row[$value];
            $res[$value] = $row[$value];
        }
        setCache($key,$cValue);
    }
    return $res;

}

The idea is to use the function as follows:

_getData($userid,"name_table","first_name,last_name",$dbConnection);

The above will return the data requested from memcahced first and if not present, then query it from mysql and store it in memcached.

The above works well, but how much does it hurt the eye of a real developer ?

Thank you