Ignore:
Timestamp:
08/23/2010 03:32:14 AM (21 months ago)
Author:
ofer
Message:

Fixed caching of APC elements to avoid arrays and save memory, negative key caching is now really working, many internal changes

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/WordPress/plugin/transposh/wp/transposh_db.php

    r507 r513  
    5959        if (!TP_ENABLE_CACHE) return false; 
    6060        $cached = false; 
    61         $key = $original . '___' . $lang; 
     61        $key = $lang . '_' . $original; 
    6262        if (function_exists('apc_fetch')) { 
    6363            $cached = apc_fetch($key, $rc); 
     
    7171        } elseif (function_exists('eaccelerator_get')) { 
    7272            $cached = eaccelerator_get($key); 
    73             if ($cached === null) 
    74                     return false; //TODO - unfortunantly null storing does not work here.. 
    75  logger('eaccelerator', 5); 
    76         } 
    77         logger("Cached: $original", 5); 
     73            if ($cached === null) return false; 
     74            //TODO - unfortunantly null storing does not work here.. 
     75            logger('eaccelerator', 5); 
     76        } 
     77        logger("Cache fetched: $original => $cached", 3); 
     78        if ($cached !== null) $cached = explode('_', $cached, 2); 
    7879        return $cached; 
    7980    } 
     
    8990    function cache_store($original, $lang, $translated, $ttl) { 
    9091        if (!TP_ENABLE_CACHE) return false; 
    91         $key = $original . '___' . $lang; 
    92         $cache_entry = $translated; 
     92        $key = $lang . '_' . $original; 
     93        if ($translated !== null) { 
     94            $translated = implode('_', $translated); 
     95        } 
    9396        //If we don't have translation still we want to have it in cache 
    9497        //update cache FIXME, do we really need this null to ""? 
    95         if ($cache_entry == NULL) { 
    96             $cache_entry = ""; 
    97         } 
    9898        if (function_exists('apc_store')) { 
    99             $rc = apc_store($key, $cache_entry, $ttl); 
     99            $rc = apc_store($key, $translated, $ttl); 
    100100        } elseif (function_exists('xcache_set')) { 
    101             $rc = xcache_set($key, $cache_entry, $ttl); 
     101            $rc = xcache_set($key, $translated, $ttl); 
    102102        } elseif (function_exists('eaccelerator_put')) { 
    103             $rc = eaccelerator_put($key, $cache_entry, $ttl); 
     103            $rc = eaccelerator_put($key, $translated, $ttl); 
    104104        } 
    105105 
    106106        if ($rc) { 
    107             logger("Stored in cache: $original => {$translated[0]},{$translated[1]}", 3); 
     107            logger("Stored in cache: $original => {$translated}", 3); 
     108        } else { 
     109            logger("Didn't cache: $original => {$translated}", 3); 
    108110        } 
    109111        return $rc; 
     
    164166        // we are saving in the array and not directly to cache, because cache might not exist... 
    165167        foreach ($rows as $row) { 
    166             $this->translations[$row['original']] = array(stripslashes($row['translated']), $row['source']); 
     168            $this->translations[$row['original']] = array($row['source'], stripslashes($row['translated'])); 
    167169        } 
    168170        logger($this->translations, 5); 
     
    175177     * @param string $original 
    176178     * @param string $lang 
    177      * @return array list(translation,source) 
     179     * @return array list(source,translation) 
    178180     */ 
    179181    function fetch_translation($original, $lang) { 
    180         $translated = NULL; 
    181         logger("Enter: $original", 4); 
     182        $translated = null; 
     183        logger("Fetching for: $original-$lang", 4); 
    182184        //The original is saved in db in its escaped form 
    183185        $original = $GLOBALS['wpdb']->escape(html_entity_decode($original, ENT_NOQUOTES, 'UTF-8')); 
     
    191193        if ($this->translations[$original]) { 
    192194            $translated = $this->translations[$original]; 
    193             logger("prefetch result for $original >>> {$this->translations[$original][0]} ({$this->translations[$original][1]})", 4); 
     195            logger("prefetch result for $original >>> {$this->translations[$original][0]} ({$this->translations[$original][1]})", 3); 
    194196        } else { 
    195197            $query = "SELECT * FROM {$this->translation_table} WHERE original = '$original' and lang = '$lang' "; 
    196198            $row = $GLOBALS['wpdb']->get_row($query); 
    197199 
    198             if ($row !== FALSE) { 
     200            if ($row !== null) { 
    199201                $translated_text = stripslashes($row->translated); 
    200                 $translated = array($translated_text, $row->source); 
    201  
    202                 logger("db result for $original >>> $translated_text ($lang) ({$row->source})", 4); 
     202                $translated = array($row->source, $translated_text); 
     203                logger("db result for $original >>> $translated_text ($lang) ({$row->source})", 3); 
    203204            } 
    204205        } 
     
    218219     */ 
    219220    function fetch_original($translation, $lang) { 
    220         $original = NULL; 
     221        $original = null; 
    221222        logger("Enter: $translation", 4); 
    222223 
     
    224225        $translation = $GLOBALS['wpdb']->escape(html_entity_decode($translation, ENT_NOQUOTES, 'UTF-8')); 
    225226        // The translation might be cached (notice the additional postfix) 
    226         $cached = $this->cache_fetch($translation . '_R_', $lang); 
     227        $cached = $this->cache_fetch('R_' . $translation, $lang); 
    227228        if ($cached !== false) { 
    228229            logger("Exit from cache: $cached", 4); 
     
    237238            $row = $GLOBALS['wpdb']->get_row($query); 
    238239 
    239             if ($row !== FALSE) { 
     240            if ($row !== null) { 
    240241                $original = stripslashes($row->original); 
    241242                logger("db result for $translation >>> $original ($lang) ({$row->source})", 4); 
     
    244245 
    245246        // we can store the result in the cache (or the fact we don't have one) 
    246         $this->cache_store($translation . '_R_', $lang, $original, TP_CACHE_TTL); 
     247        $this->cache_store('R_' . $translation, $lang, $original, TP_CACHE_TTL); 
    247248 
    248249        logger("Exit: $translation/$original", 4); 
     
    647648 
    648649} 
     650 
    649651?> 
Note: See TracChangeset for help on using the changeset viewer.