Changeset 741

Show
Ignore:
Timestamp:
06/21/07 11:46:08 (1 year ago)
Author:
mikey
Message:

added unit tests for net.stubbles.util.cache

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/src/main/php/net/stubbles/util/cache/stubAbstractCacheContainer.php

    r738 r741  
    1818     * the strategy used for decisions about caching 
    1919     * 
    20      * @var  stubCacheStrage
     20     * @var  stubCacheStrateg
    2121     */ 
    2222    protected $strategy; 
     
    3535 
    3636    /** 
     37     * constructor 
     38     * 
     39     * @param  string             $id        id of the container 
     40     * @param  stubCacheStrategy  $strategy  strategy the container should use 
     41     */ 
     42    public function __construct($id, stubCacheStrategy $strategy) 
     43    { 
     44        $this->id       = $id; 
     45        $this->strategy = $strategy; 
     46    } 
     47 
     48    /** 
    3749     * sets the id of the container 
    3850     * 
     
    5769     * sets the strategy the container should use 
    5870     * 
    59      * @param  stubCacheStragey  $strategy 
    60      */ 
    61     public function setStrategy(stubCacheStragey $strategy) 
     71     * @param  stubCacheStrategy  $strategy 
     72     */ 
     73    public function setStrategy(stubCacheStrategy $strategy) 
    6274    { 
    6375        $this->strategy = $strategy; 
     
    7587    public function put($key, $data) 
    7688    { 
    77         if ($this->strategy->isCacheable($this, $key, $data) == false) { 
     89        if ($this->strategy->isCachable($this, $key, $data) == false) { 
    7890            return false; 
    7991        } 
  • trunk/src/main/php/net/stubbles/util/cache/stubCacheContainer.php

    r738 r741  
    77 * @subpackage  util_cache 
    88 */ 
    9 stubClassLoader::load('net.stubbles.util.cache.stubCacheStragey'); 
     9stubClassLoader::load('net.stubbles.util.cache.stubCacheStrategy'); 
    1010/** 
    1111 * Interface for cache containers. 
     
    3333     * sets the strategy the container should use 
    3434     * 
    35      * @param  stubCacheStragey  $strategy 
     35     * @param  stubCacheStrategy  $strategy 
    3636     */ 
    37     public function setStrategy(stubCacheStragey $strategy); 
     37    public function setStrategy(stubCacheStrategy $strategy); 
    3838 
    3939    /** 
     
    7575 
    7676    /** 
    77      * returns the unix timestamp when the data associated with $key was cached 
     77     * returns the time in seconds how long the data associated with $key is cached 
    7878     * 
    7979     * @param   string  $key 
  • trunk/src/main/php/net/stubbles/util/cache/stubCacheStrategy.php

    r738 r741  
    2424     * @return  bool 
    2525     */ 
    26     public function isCacheable(stubCacheContainer $container, $key, $data); 
     26    public function isCachable(stubCacheContainer $container, $key, $data); 
    2727 
    2828    /** 
  • trunk/src/main/php/net/stubbles/util/cache/stubDefaultCacheStrategy.php

    r739 r741  
    3131    protected $maxSize; 
    3232    /** 
    33      * probability of a garbace collection run 
     33     * probability of a garbage collection run 
    3434     *  
    3535     * Should be a value between 0 and 100 where 0 means never and 100 means always. 
     
    4343     * 
    4444     * @param  int     $ttl            time to live for single cached data 
    45      * @param  string  $maxSize        maximum size of cache 
     45     * @param  string  $maxSize        maximum size of cache, -1 for infinite size 
    4646     * @param  int     $gcProbability  probability of a garbace collection run 
    4747     */ 
     
    6161     * @return  bool 
    6262     */ 
    63     public function isCacheable(stubCacheContainer $container, $key, $data) 
     63    public function isCachable(stubCacheContainer $container, $key, $data) 
    6464    { 
    6565        if (-1 == $this->maxSize) { 
     
    8383    public function isExpired(stubCacheContainer $container, $key) 
    8484    { 
    85         $cacheTime = $container->getCacheTime($key); 
    86         return (($cacheTime + $this->ttl) < time()); 
     85        return ($container->getCacheTime($key) > $this->ttl); 
    8786    } 
    8887 
  • trunk/src/main/php/net/stubbles/util/cache/stubFileCacheContainer.php

    r739 r741  
    4444    public function setCacheDirectory($directory) 
    4545    { 
    46         $this->cacheDirectory = $directory; 
     46        $this->cacheDirectory = $directory . '/' . $this->id; 
     47        if (file_exists($this->cacheDirectory) == false) { 
     48            mkdir($this->cacheDirectory, 0700, true); 
     49        } 
    4750    } 
    4851 
     
    5962    { 
    6063        $bytes = file_put_contents($this->cacheDirectory . '/' . $key . '.cache', $data); 
     64        if (false === $bytes) { 
     65            return false; 
     66        } 
     67         
    6168        if (null !== $this->keys) { 
    6269            $this->keys[$key] = $key; 
     
    99106 
    100107    /** 
    101      * returns the unix timestamp when the data associated with $key was cached 
     108     * returns the time in seconds how long the data associated with $key is cached 
    102109     * 
    103110     * @param   string  $key 
     
    106113    public function getCacheTime($key) 
    107114    { 
    108         return filemtime($this->cacheDirectory . '/' . $key . '.cache'); 
     115        if ($this->doHas($key) == true) { 
     116            return (time() - filemtime($this->cacheDirectory . '/' . $key . '.cache')); 
     117        } 
     118         
     119        return 0; 
    109120    } 
    110121 
     
    117128    protected function doGetSize($key) 
    118129    { 
     130        if (null !== $this->size) { 
     131            return $this->size[$key]; 
     132        } 
     133         
    119134        return filesize($this->cacheDirectory . '/' . $key . '.cache'); 
    120135    } 
     
    158173                } 
    159174                 
     175                $key = str_replace('.cache', '', $file->getFilename()); 
    160176                if ($this->strategy->isExpired($this, $key) == true) { 
    161177                    continue; 
    162178                } 
    163179                 
    164                 $key              = str_replace('.cache', '', $file->getFilename()); 
    165180                $this->keys[$key] = $key; 
    166181            } 
     182        } else { 
     183            foreach ($this->keys as $key) { 
     184                if ($this->strategy->isExpired($this, $key) == true) { 
     185                    unset($this->keys[$key]); 
     186                    if (null !== $this->size) { 
     187                        unset($this->size[$key]); 
     188                    } 
     189                } 
     190            } 
    167191        } 
    168192         
     
    173197     * runs the garbage collection 
    174198     */ 
    175     protected function gc() 
     199    protected function doGc() 
    176200    { 
    177201        $dirIt = new DirectoryIterator($this->cacheDirectory); 
  • trunk/src/test/php/net/stubbles/util/UtilTestSuite.php

    r719 r741  
    2525        $this->addTestFile($dir . '/stubRegistryTestCase.php'); 
    2626        $this->addTestFile($dir . '/stubRegistryXJConfInitializerTestCase.php'); 
     27         
     28        // cache 
     29        $this->addTestFile($dir . '/cache/stubCacheTestCase.php'); 
     30        $this->addTestFile($dir . '/cache/stubDefaultCacheStrategyTestCase.php'); 
     31        $this->addTestFile($dir . '/cache/stubAbstractCacheContainerTestCase.php'); 
     32        $this->addTestFile($dir . '/cache/stubFileCacheContainerTestCase.php'); 
    2733 
    2834        // datespan