lfsworldsqlcache.php

Go to the documentation of this file.
00001 <?php
00016 class LFSWorldSQLCache
00017 {
00022     var $m_aDefaults = array(   'lifeTime' => 60,
00023                                 'noValidityCheck' => false,
00024                                 'queries' => array( 'count' => 'SELECT COUNT(lastUpdate) AS rowCount FROM {{table}} WHERE lastUpdate >= {{threshold}}',
00025                                                     'get' => 'SELECT * FROM {{table}} WHERE lastUpdate >= {{threshold}} {{where}} {{limit}} {{order}}',
00026                                                     'save' => 'REPLACE INTO {{table}} {{fields}} VALUES {{values}}',
00027                                                     'oldThreshold' => 'SELECT DISTINCT lastUpdate FROM {{table}} ORDER BY lastUpdate DESC LIMIT 1, 1'),
00028                                 'resultsPerPage' => 0,
00029                                 'page' => 1,
00030                                 'getTransformer' => '',
00031                                 'saveTransformer' => '',
00032                                 'where' => '',
00033                                 'order' => '');
00034 
00042     function LFSWorldSQLCache($params)
00043     {
00044         $this->m_aDefaults = array_merge_replace($this->m_aDefaults, $params);
00045     }
00046     
00054     function get($params)
00055     {
00056         $params = array_merge_replace($this->m_aDefaults, $params);
00057         $threshold = ($params['noValidityCheck'] == true) ? ($this->_getLastThreshold($params)) : (time() - $params['lifeTime']);
00058         
00059         $totalRows = $this->_countRows($params, $threshold); 
00060         if($totalRows == 0)
00061         {
00062             $GLOBALS['PPFLog'][] = 'Zero rows available from cache!';
00063             return false;
00064         }
00065         
00066         $pageData = $this->_createPaginationData($params, $totalRows);
00067 
00068         if($params['where'] != '')
00069             $params['where'] = "AND ({$params['where']})";
00070             
00071         $q = str_replace(   array('{{table}}', '{{threshold}}', '{{where}}', '{{limit}}', '{{order}}'),
00072                             array($params['table'], $threshold, $params['where'], $pageData['limitString'], $params['order']),
00073                             $params['queries']['get']); 
00074         
00075         $return = array('totalCount' => $totalRows,
00076                         'page' => $pageData['page'],
00077                         'resultsPerPage' => $pageData['resultsPerPage'],
00078                         'pages' => $pageData['pages']);
00079                         
00080         $result = mysql_db_query($params['dbName'], $q, $params['dbResource']);
00081 
00082         if(!$result || mysql_num_rows($result) == 0)
00083         {
00084             $GLOBALS['PPFLog'][] = 'Cache query has zero rows or is invalid!';
00085             return false;
00086         }
00087         else
00088         {
00089             $data = array();
00090             while(($row = mysql_fetch_assoc($result)) != false)
00091                 $data[] = $row;
00092 
00093             if(function_exists($params['getTransformer']))
00094                 $data = call_user_func($params['getTransformer'], &$this, $data, $params);
00095         }
00096         
00097         $return['data'] = $data;
00098         $return['lastUpdate'] = $data[0]['lastUpdate'];
00099         
00100         return $return;        
00101     }
00102 
00111     function save($data, $params)
00112     {
00113         $params = array_merge_recursive($this->m_aDefaults, $params);
00114 
00115         if(function_exists($params['saveTransformer']))
00116             $data = call_user_func($params['saveTransformer'], &$this, $data, $params);
00117         
00118         reset($data);
00119         list($k, $v) = each($data);
00120         $fields = '('.implode(', ', array_keys($data[$k])).')';
00121         $values = array_map(array($this, '_stringify'), $data);
00122         $values = implode(', ', $values); 
00123         
00124         $q = str_replace(   array('{{table}}', '{{fields}}', '{{values}}'),
00125                             array($params['table'], $fields, $values),
00126                             $params['queries']['save']);
00127 
00128         $result = mysql_db_query($params['dbName'], $q, $params['dbResource']);
00129  
00130         if(!$result || mysql_affected_rows($params['dbResource']) == 0)
00131         {
00132             $GLOBALS['PPFLog'][] = 'Failed to save cache for reasons unknown!';
00133             return false;
00134         }
00135 
00136         return true; 
00137     }
00138             
00145     function requiresDataReload()
00146     {
00147         return true;
00148     }
00149 
00158     function purgeOld($params)
00159     {
00160         $threshold = $this->getLastUpdate($params);
00161           
00162         //delete all data older than threshold
00163     }
00164 
00172     function createKey()
00173     {
00174         return ' ';
00175     }
00176     
00184     function getLastUpdate($params)
00185     {
00186         $params = array_merge_replace($this->m_aDefaults, $params);
00187         
00188         $q = str_replace(   array('{{table}}'),
00189                             array($params['table']),
00190                             $params['queries']['oldThreshold']);
00191 
00192         $results = mysql_db_query($params['dbName'], $q, $params['dbResource']);
00193         if($results && mysql_num_rows($results) == 1)
00194         {
00195             $row = mysql_fetch_assoc($results);
00196             return $row['lastUpdate'];
00197         }
00198 
00199         return 0;
00200     }
00201     
00209     function _countRows($params, $threshold)
00210     {
00211         $q = str_replace(   array('{{table}}', '{{threshold}}'),
00212                             array($params['table'], $threshold),
00213                             $params['queries']['count']);
00214 
00215         $result = mysql_db_query($params['dbName'], $q, $params['dbResource']);
00216         if(!$result || !mysql_num_rows($result))
00217             return 0;
00218 
00219         $result = mysql_fetch_assoc($result);
00220         return $result['rowCount'];
00221     }
00222 
00231     function _createPaginationData($params, $totalRows)
00232     {
00233         //Validate page num
00234         $page = (int)abs($params['page']);
00235         $page = ($page > 0) ? ($page) : (1);
00236         
00237         //Validate Results per page
00238         $rpp = ($params['resultsPerPage'] <= 0) ? ($totalRows) : ((int)abs($params['resultsPerPage']));
00239         $rpp = ($rpp > 0) ? ($rpp) : (1);
00240         
00241         //Calc page count
00242         $pages = ceil($totalRows / $rpp);
00243         
00244         //Calc start row based on page count and results per page
00245         $start = ($page-1) * $rpp;
00246         $limit = "LIMIT $start, $rpp";
00247 
00248         return array(   'page' => $page,
00249                         'resultsPerPage' => $rpp,
00250                         'pages' => $pages,
00251                         'start' => $start,
00252                         'limitString' => $limit);
00253     }
00254 
00262     function _stringify($array)
00263     {
00264         $array = array_map(array($this, '_escape'), $array);
00265         return '(' . implode(', ', $array) . ')';
00266     }
00267 
00275     function _escape($string)
00276     {
00277         return '\'' . str_replace('\'', '\'\'', $string) . '\'';
00278     }
00279 }
00280 
00281 function hostSaveTransformer(&$cache, $data, $params)
00282 {
00283     $lastUpdate = time();
00284     while(list($key, $val) = each($data))
00285     {
00286         $data[$key]['strippedHostName'] = $data[$key]['hostName'];
00287         $data[$key]['racers'] = implode(', ', $data[$key]['racers']);
00288         $data[$key]['lastUpdate'] = $lastUpdate;
00289     }
00290 
00291     return $data;
00292 }

Generated on Wed Oct 25 03:13:32 2006 for LFSWorldParsingProviderFramework by  doxygen 1.4.6