php use memcached to store query data ,something to discuss
function select(){
//format sql string
$sql = format($sql);
//a unique key to store this query
$encodeKey = md5($sql);
if( $cacheObj = $memcache->get( $encodeKey ) ){
// cache data exis开发者_如何学运维t ,to get it
$result = $cacheObj;
}
else{
//or create a cache data
$query = $this->exec($sql);
$result = mysql_fetch_array( $query );
$memcache->set( $encodeKey, $result );
}
return $result;
}
I use md5 method to store query string as memcached key, is it a good way? or something better than this .
and in 'format' function , select field from table where id='5' and select field from table where id = 5 is the same query , so I must write a lot of codes to format both of them . any better design will be appreciated
Using md5 to generate a key is fine, although it may have potential performance impacts. The downside of using that method is its more difficult to selectively expire data in the cache should the underlying data change.
精彩评论