Assembla home | Assembla project page
 

Changeset 180

Show
Ignore:
Timestamp:
03/21/08 22:42:02 (8 months ago)
Author:
Slynderdale
Message:

Added caching to acronym that flushes daily to improve its speed and performance.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/Phergie/Plugin/Acronym.php

    r158 r180  
    4040 
    4141    /** 
     42    * Caches the returned acronyms 
     43    * 
     44    * @var array 
     45    */ 
     46    protected $cache; 
     47    protected $flushed; 
     48 
     49    /** 
    4250    * Possible reactions to return when no result is returned 
    4351    * 
     
    122130        $target = $this->event->getSource(); 
    123131        $message = $this->event->getArgument(1); 
     132        $today = date('md'); 
    124133 
    125134                // Matches an optional "Nick: " followed by an acronym formatted as A.B.C. or ABC 
     
    139148        } 
    140149 
    141         $opts = array('http' => 
    142             array( 
    143                 'timeout' => 5, 
    144                 'method' => 'GET', 
    145                 'user_agent' => 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.12) Gecko/20080201 Firefox/2.0.0.12', 
    146                 'content' => http_build_query(array('terms' => $acronym, 'andor' => 'or', 'acronym' => 'on')) 
    147             ) 
    148         ); 
    149         $context = stream_context_create($opts); 
    150         $url = 'http://www.acronymfinder.com/af-query.asp?Acronym=' . urlencode($acronym) . '&Find=find'; 
    151         $contents = @file_get_contents($url, false, $context); 
    152  
    153         if (empty ($contents) 
    154             || strpos($contents, 'no abbreviation matches') !== false 
    155             || strpos($contents, 'has exceeded the daily query limit') !== false) { 
    156             $this->randomAction($target); 
    157         } else { 
    158             $matches = array(); 
    159             $offset = 0; 
    160  
    161             do { 
    162                 $count = preg_match( 
    163                     '/<td width="65%"[^>]+>([^<]+)</i', 
    164                     $contents, 
    165                     $match, 
    166                     PREG_OFFSET_CAPTURE, 
    167                     $offset 
    168                 ); 
    169                 if ($count == 1) { 
    170                     $matches[] = html_entity_decode($match[1][0]); 
    171                     $offset = $match[1][1]; 
    172                 } 
    173             } while (($this->limit == 0 || count($matches) < $this->limit) && $count == 1); 
    174  
    175                         if (count($matches) > 0) { 
    176                                 $text = 'Possible matches for ' . $acronym . ': ' . implode(', ', $matches); 
    177                                 $this->doPrivmsg($target, $text); 
    178                         } 
     150        if ($this->flushed != $today) { 
     151            unset($this->cache); 
     152            $this->flushed = $today; 
     153            $this->cache = array(); 
     154        } 
     155 
     156        if (!isset($this->cache[$acronym])) { 
     157            $opts = array('http' => 
     158                array( 
     159                    'timeout' => 5, 
     160                    'method' => 'GET', 
     161                    'user_agent' => 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.12) Gecko/20080201 Firefox/2.0.0.12', 
     162                    'content' => http_build_query(array('terms' => $acronym, 'andor' => 'or', 'acronym' => 'on')) 
     163                ) 
     164            ); 
     165            $context = stream_context_create($opts); 
     166            $url = 'http://www.acronymfinder.com/af-query.asp?Acronym=' . urlencode($acronym) . '&Find=find'; 
     167            $contents = @file_get_contents($url, false, $context); 
     168 
     169            if (empty ($contents) 
     170                || strpos($contents, 'no abbreviation matches') !== false 
     171                || strpos($contents, 'has exceeded the daily query limit') !== false) { 
     172                $this->randomAction($target); 
     173                return; 
     174            } else { 
     175                $matches = array(); 
     176                $offset = 0; 
     177 
     178                do { 
     179                    $count = preg_match( 
     180                        '/<td width="65%"[^>]+>([^<]+)</i', 
     181                        $contents, 
     182                        $match, 
     183                        PREG_OFFSET_CAPTURE, 
     184                        $offset 
     185                    ); 
     186                    if ($count == 1) { 
     187                        $matches[] = html_entity_decode($match[1][0]); 
     188                        $offset = $match[1][1]; 
     189                    } 
     190                } while (($this->limit == 0 || count($matches) < $this->limit) && $count == 1); 
     191            } 
     192        } 
     193 
     194        if (isset($this->cache[$acronym])) { 
     195                $matches = $this->cache[$acronym]; 
     196        } 
     197 
     198        if (count($matches) > 0) { 
     199                $this->cache[$acronym] = $matches; 
     200 
     201            $text = 'Possible matches for ' . $acronym . ': ' . implode('; ', $matches); 
     202            $this->doPrivmsg($target, $text); 
    179203        } 
    180204    }