root/trunk/src/main/php/net/stubbles/util/cache/stubDefaultCacheStrategy.php

Revision 1301, 2.7 kB (checked in by richi, 4 months ago)

coding standards: fixed coding standards issues (function docblock)

Line 
1 <?php
2 /**
3  * Default caching strategy.
4  *
5  * @author      Frank Kleine <mikey@stubbles.net>
6  * @package     stubbles
7  * @subpackage  util_cache
8  */
9 stubClassLoader::load('net::stubbles::util::cache::stubCacheContainer');
10 /**
11  * Default caching strategy.
12  *
13  * @package     stubbles
14  * @subpackage  util_cache
15  */
16 class stubDefaultCacheStrategy extends stubSerializableObject implements stubCacheStrategy
17 {
18     /**
19      * time to live for single cached data
20      *
21      * @var  int
22      */
23     protected $ttl;
24     /**
25      * maximum size of cache
26      *
27      * To allow an infinite size set this to -1.
28      *
29      * @var  string
30      */
31     protected $maxSize;
32     /**
33      * probability of a garbage collection run
34      *
35      * Should be a value between 0 and 100 where 0 means never and 100 means always.
36      *
37      * @var  int
38      */
39     protected $gcProbability;
40
41     /**
42      * constructor
43      *
44      * @param  int     $ttl            time to live for single cached data
45      * @param  string  $maxSize        maximum size of cache, -1 for infinite size
46      * @param  int     $gcProbability  probability of a garbace collection run
47      */
48     public function __construct($ttl, $maxSize, $gcProbability)
49     {
50         $this->ttl           = $ttl;
51         $this->maxSize       = $maxSize;
52         $this->gcProbability = $gcProbability;
53     }
54
55     /**
56      * checks whether an item is cacheable or not
57      *
58      * @param   stubCacheContainer  $container  the container to cache the data in
59      * @param   string              $key        the key to cache the data under
60      * @param   string              $data       data to cache
61      * @return  bool
62      */
63     public function isCachable(stubCacheContainer $container, $key, $data)
64     {
65         if (-1 == $this->maxSize) {
66             return true;
67         }
68         
69         if (($container->getUsedSpace() + strlen($data) - $container->getSize($key)) > $this->maxSize) {
70             return false;
71         }
72         
73         return true;
74     }
75
76     /**
77      * checks whether a cached item is expired
78      *
79      * @param   stubCacheContainer  $container  the container that contains the cached data
80      * @param   string              $key        the key where the data is cached under
81      * @return  bool
82      */
83     public function isExpired(stubCacheContainer $container, $key)
84     {
85         return ($container->getLifeTime($key) > $this->ttl);
86     }
87
88     /**
89      * checks whether the garbage collection should be run
90      *
91      * @param stubCacheContainer $container
92      * @return  bool
93      */
94     public function shouldRunGc(stubCacheContainer $container)
95     {
96         if (rand(1, 100) < $this->gcProbability) {
97             return true;
98         }
99         
100         return false;
101     }
102 }
103 ?>
Note: See TracBrowser for help on using the browser.