ryxeo-glpi-git / lib / cache_lite / docs / examples @ b67d8923
Historique | Voir | Annoter | Télécharger (4,55 ko)
1 | b67d8923 | Eric Seigne | A few examples of Cache_Lite using : |
---|---|---|---|
2 | ------------------------------------ |
||
3 | |||
4 | >>> Basic one : |
||
5 | |||
6 | <?php |
||
7 | |||
8 | // Include the package |
||
9 | require_once('Cache/Lite.php'); |
||
10 | |||
11 | // Set a id for this cache |
||
12 | $id = '123'; |
||
13 | |||
14 | // Set a few options |
||
15 | $options = array( |
||
16 | 'cacheDir' => '/tmp/', |
||
17 | 'lifeTime' => 3600 |
||
18 | ); |
||
19 | |||
20 | // Create a Cache_Lite object |
||
21 | $Cache_Lite = new Cache_Lite($options); |
||
22 | |||
23 | // Test if thereis a valide cache for this id |
||
24 | if ($data = $Cache_Lite->get($id)) { |
||
25 | |||
26 | // Cache hit ! |
||
27 | // Content is in $data |
||
28 | // (...) |
||
29 | |||
30 | } else { // No valid cache found (you have to make the page) |
||
31 | |||
32 | // Cache miss ! |
||
33 | // Put in $data datas to put in cache |
||
34 | // (...) |
||
35 | $Cache_Lite->save($data); |
||
36 | |||
37 | } |
||
38 | |||
39 | ?> |
||
40 | |||
41 | |||
42 | >>> Usage with blocks |
||
43 | (You can use Cache_Lite for caching blocks and not the whole page) |
||
44 | |||
45 | <?php |
||
46 | |||
47 | require_once('Cache/Lite.php'); |
||
48 | |||
49 | $options = array( |
||
50 | 'cacheDir' => '/tmp/', |
||
51 | 'lifeTime' => 3600 |
||
52 | ); |
||
53 | |||
54 | // Create a Cache_Lite object |
||
55 | $Cache_Lite = new Cache_Lite($options); |
||
56 | |||
57 | if ($data = $Cache_Lite->get('block1')) { |
||
58 | echo($data); |
||
59 | } else { |
||
60 | $data = 'Data of the block 1'; |
||
61 | $Cache_Lite->save($data); |
||
62 | } |
||
63 | |||
64 | echo('<br><br>Non cached line !<br><br>'); |
||
65 | |||
66 | if ($data = $Cache_Lite->get('block2')) { |
||
67 | echo($data); |
||
68 | } else { |
||
69 | $data = 'Data of the block 2'; |
||
70 | $Cache_Lite->save($data); |
||
71 | } |
||
72 | |||
73 | ?> |
||
74 | |||
75 | |||
76 | A few examples of Cache_Lite_Output using : |
||
77 | ------------------------------------------- |
||
78 | |||
79 | >>> Basic one : |
||
80 | |||
81 | <?php |
||
82 | |||
83 | require_once('Cache/Lite/Output.php'); |
||
84 | |||
85 | $options = array( |
||
86 | 'cacheDir' => '/tmp/', |
||
87 | 'lifeTime' => 10 |
||
88 | ); |
||
89 | |||
90 | $cache = new Cache_Lite_Output($options); |
||
91 | |||
92 | if (!($cache->start('123'))) { |
||
93 | // Cache missed... |
||
94 | for($i=0;$i<1000;$i++) { // Making of the page... |
||
95 | echo('0123456789'); |
||
96 | } |
||
97 | $cache->end(); |
||
98 | } |
||
99 | |||
100 | ?> |
||
101 | |||
102 | >>> Usage with blocks : |
||
103 | (You can use Cache_Lite_Output for caching blocks and not the whole page) |
||
104 | |||
105 | <?php |
||
106 | |||
107 | require_once('Cache/Lite/Output.php'); |
||
108 | |||
109 | $options = array( |
||
110 | 'cacheDir' => '/tmp/', |
||
111 | 'lifeTime' => 10 |
||
112 | ); |
||
113 | |||
114 | $cache = new Cache_Lite_Output($options); |
||
115 | |||
116 | if (!($cache->start('block1'))) { |
||
117 | // Cache missed... |
||
118 | echo('Data of the block 1 !<br>'); |
||
119 | $cache->end(); |
||
120 | } |
||
121 | |||
122 | echo('<br><br>Non cached line !<br><br>'); |
||
123 | |||
124 | if (!($cache->start('block2'))) { |
||
125 | // Cache missed... |
||
126 | echo('Data of the block 2 !<br>'); |
||
127 | $cache->end(); |
||
128 | } |
||
129 | |||
130 | |||
131 | A few examples of Cache_Lite_Function using : |
||
132 | --------------------------------------------- |
||
133 | |||
134 | >>> With function : |
||
135 | |||
136 | <?php |
||
137 | |||
138 | require_once('Cache/Lite/Function.php'); |
||
139 | |||
140 | $options = array( |
||
141 | 'cacheDir' => '/tmp/', |
||
142 | 'lifeTime' => 10 |
||
143 | ); |
||
144 | |||
145 | $cache = new Cache_Lite_Function($options); |
||
146 | |||
147 | $cache->call('function_to_bench', 12, 45); |
||
148 | |||
149 | function function_to_bench($arg1, $arg2) |
||
150 | { |
||
151 | echo "This is the output of the function function_to_bench($arg1, $arg2) !<br>"; |
||
152 | return "This is the result of the function function_to_bench($arg1, $arg2) !<br>"; |
||
153 | } |
||
154 | |||
155 | ?> |
||
156 | |||
157 | >>> With method : |
||
158 | |||
159 | <?php |
||
160 | |||
161 | require_once('Cache/Lite/Function.php'); |
||
162 | |||
163 | $options = array( |
||
164 | 'cacheDir' => '/tmp/', |
||
165 | 'lifeTime' => 10 |
||
166 | ); |
||
167 | |||
168 | $cache = new Cache_Lite_Function($options); |
||
169 | |||
170 | $obj = new bench(); |
||
171 | $obj->test = 666; |
||
172 | |||
173 | $cache->call('obj->method_to_bench', 12, 45); |
||
174 | |||
175 | class bench |
||
176 | { |
||
177 | var $test; |
||
178 | |||
179 | function method_to_bench($arg1, $arg2) |
||
180 | { |
||
181 | echo "\$obj->test = $this->test and this is the output of the method \$obj->method_to_bench($arg1, $arg2) !<br>"; |
||
182 | return "\$obj->test = $this->test and this is the result of the method \$obj->method_to_bench($arg1, $arg2) !<br>"; |
||
183 | } |
||
184 | |||
185 | } |
||
186 | |||
187 | ?> |
||
188 | |||
189 | >>> With static method : |
||
190 | |||
191 | <?php |
||
192 | |||
193 | require_once('Cache/Lite/Function.php'); |
||
194 | |||
195 | $options = array( |
||
196 | 'cacheDir' => '/tmp/', |
||
197 | 'lifeTime' => 10 |
||
198 | ); |
||
199 | |||
200 | $cache = new Cache_Lite_Function($options); |
||
201 | |||
202 | $cache->call('bench::static_method_to_bench', 12, 45); |
||
203 | |||
204 | class bench |
||
205 | { |
||
206 | var $test; |
||
207 | |||
208 | function static_method_to_bench($arg1, $arg2) { |
||
209 | echo "This is the output of the function static_method_to_bench($arg1, $arg2) !<br>"; |
||
210 | return "This is the result of the function static_method_to_bench($arg1, $arg2) !<br>"; |
||
211 | } |
||
212 | } |
||
213 | |||
214 | ?> |
||
215 | |||
216 | >>> IMPORTANT : |
||
217 | |||
218 | If you try to use Cache_Lite_Function with $this object ($cache->call('this->method',...) |
||
219 | for example), have a look first at : |
||
220 | |||
221 | http://pear.php.net/bugs/bug.php?id=660 |
||
222 | |||
223 | |||
224 | A few examples of Cache_Lite_File using : |
||
225 | ----------------------------------------- |
||
226 | |||
227 | <?php |
||
228 | |||
229 | $options = array( |
||
230 | 'cacheDir' => '/tmp/', |
||
231 | 'masterFile' => '/home/web/config.xml' |
||
232 | ); |
||
233 | $cache = new Cache_Lite_File($options); |
||
234 | |||
235 | // Set a id for this cache |
||
236 | $id = '123'; |
||
237 | |||
238 | if ($data = $cache->get($id)) { |
||
239 | |||
240 | // Cache hit ! |
||
241 | // Content is in $data |
||
242 | // (...) |
||
243 | |||
244 | } else { // No valid cache found (you have to make the page) |
||
245 | |||
246 | // Cache miss ! |
||
247 | // Put in $data datas to put in cache |
||
248 | // (...) |
||
249 | $cache->save($data); |
||
250 | |||
251 | } |
||
252 | |||
253 | |||
254 | ?> |