您的位置:首页 > 运维架构

ecshop源码分析——静态缓存

2010-07-13 18:03 239 查看
tatic_caches缓存文件存放在ecshop/temp/static_caches下面
先来看缓存工作的2个主要函数,写缓存和读缓存。
该函数在ecshop/includes/lib_base.php
写缓存

Php代码

1. /**
2. * 将结果写进缓存文件
3. *
4. * @params string $cache_name 缓存文件的名字
5. * @params string $caches 缓存的内容
6. *
7. * @return
8. */
9. function write_static_cache($cache_name, $caches)
10. {
11. if ((DEBUG_MODE & 2) == 2)
12. {
13. return false;
14. }
15. //缓存的路径
16. $cache_file_path = ROOT_PATH . '/temp/static_caches/' . $cache_name . '.php';
17. //缓存内容$content
18. $content = "<?php/r/n";
19. $content .= "/$data = " . var_export($caches, true) . ";/r/n";
20. $content .= "?>";
21. //将内容写进缓存
22. file_put_contents($cache_file_path, $content, LOCK_EX);
23. }

读缓存
Php代码

1. /**
2. * 读结果缓存文件
3. *
4. * @params string $cache_name//缓存文件的名字
5. *
6. * @return array $data
7. */
8. function read_static_cache($cache_name)
9. {
10. if ((DEBUG_MODE & 2) == 2)
11. {
12. return false;
13. }
14. //注意这里的静态变量用法
15. static $result = array();
16. //如果已经从缓存文件中读取了数据则直接返回结果
17. if (!emptyempty($result[$cache_name]))
18. {
19. return $result[$cache_name];
20. }
21. //缓存文件的路径
22. $cache_file_path = ROOT_PATH . '/temp/static_caches/' . $cache_name . '.php';
23. //如果缓存文件存在就读取缓存
24. if (file_exists($cache_file_path))
25. {
26. include_once($cache_file_path);
27. $result[$cache_name] = $data;
28. return $result[$cache_name];
29. }
30. else
31. {
32. return false;
33. }
34. }

举一个ecshop中具体应用的例子来说明

ecshop/inlucdes/lib_goods.php

function get_recommend_goods首页中展示的新品推荐、热卖商品、今日特价就是从这里来的

Php代码

1. /**
2. * 获得推荐商品
3. *
4. * @access public
5. * @param string $type 推荐类型,可以是 best, new, hot
6. * @return array
7. */
8. //代码我省略了一些,主要说明与缓存相关的代码
9. */
10. function get_recommend_goods($type = '', $cats = '')
11. {
12. /* 省略的代码。。。。*/
13. /*读取缓存文件*/
14. $data = read_static_cache('recommend_goods');
15. if($data==false) //如果缓存文件不存在
16. {
17. /*从数据库中获取需要的数据*/
18. /*将从数据库中获取的数据写入到相应的缓存文件当中*/
19. write_static_cache('recommend_goods', $goods_data);
20. }else //如果缓存文件存在
21. {
22. /*直接使用缓存数据*/
23. $good_data=$data;
24. }
25. }

Java代码

1. //缓存结果在这里 <?php
2. $data = array (
3. 'best' =>
4. array (
5. 0 =>
6. array (
7. 'goods_id' => '33',
8. 'sort_order' => '0',
9. ),
10. 1 =>
11. array (
12. 'goods_id' => '19',
13. 'sort_order' => '0',
14. ),
15. 2 =>
16. array (
17. 'goods_id' => '24',
18. 'sort_order' => '0',
19. ),
20. 3 =>
21. array (
22. 'goods_id' => '20',
23. 'sort_order' => '0',
24. ),
25. 4 =>
26. array (
27. 'goods_id' => '22',
28. 'sort_order' => '0',
29. ),
30. 5 =>
31. array (
32. 'goods_id' => '8',
33. 'sort_order' => '0',
34. ),
35. 6 =>
36. array (
37. 'goods_id' => '23',
38. 'sort_order' => '0',
39. ),
40. 7 =>
41. array (
42. 'goods_id' => '9',
43. 'sort_order' => '0',
44. ),
45. 8 =>
46. array (
47. 'goods_id' => '1',
48. 'sort_order' => '0',
49. ),
50. 9 =>
51. array (
52. 'goods_id' => '17',
53. 'sort_order' => '0',
54. ),
55. 10 =>
56. array (
57. 'goods_id' => '27',
58. 'sort_order' => '0',
59. ),
60. 11 =>
61. array (
62. 'goods_id' => '30',
63. 'sort_order' => '0',
64. ),
65. 12 =>
66. array (
67. 'goods_id' => '25',
68. 'sort_order' => '0',
69. ),
70. 13 =>
71. array (
72. 'goods_id' => '29',
73. 'sort_order' => '0',
74. ),
75. 14 =>
76. array (
77. 'goods_id' => '5',
78. 'sort_order' => '0',
79. ),
80. ),
81. 'new' =>
82. array (
83. 0 =>
84. array (
85. 'goods_id' => '33',
86. 'sort_order' => '0',
87. ),
88. 1 =>
89. array (
90. 'goods_id' => '19',
91. 'sort_order' => '0',
92. ),
93. 2 =>
94. array (
95. 'goods_id' => '32',
96. 'sort_order' => '0',
97. ),
98. 3 =>
99. array (
100. 'goods_id' => '24',
101. 'sort_order' => '0',
102. ),
103. 4 =>
104. array (
105. 'goods_id' => '20',
106. 'sort_order' => '0',
107. ),
108. 5 =>
109. array (
110. 'goods_id' => '12',
111. 'sort_order' => '0',
112. ),
113. 6 =>
114. array (
115. 'goods_id' => '22',
116. 'sort_order' => '0',
117. ),
118. 7 =>
119. array (
120. 'goods_id' => '8',
121. 'sort_order' => '0',
122. ),
123. 8 =>
124. array (
125. 'goods_id' => '23',
126. 'sort_order' => '0',
127. ),
128. 9 =>
129. array (
130. 'goods_id' => '9',
131. 'sort_order' => '0',
132. ),
133. 10 =>
134. array (
135. 'goods_id' => '1',
136. 'sort_order' => '0',
137. ),
138. 11 =>
139. array (
140. 'goods_id' => '27',
141. 'sort_order' => '0',
142. ),
143. 12 =>
144. array (
145. 'goods_id' => '5',
146. 'sort_order' => '0',
147. ),
148. ),
149. 'hot' =>
150. array (
151. 0 =>
152. array (
153. 'goods_id' => '33',
154. 'sort_order' => '0',
155. ),
156. 1 =>
157. array (
158. 'goods_id' => '19',
159. 'sort_order' => '0',
160. ),
161. 2 =>
162. array (
163. 'goods_id' => '32',
164. 'sort_order' => '0',
165. ),
166. 3 =>
167. array (
168. 'goods_id' => '24',
169. 'sort_order' => '0',
170. ),
171. 4 =>
172. array (
173. 'goods_id' => '20',
174. 'sort_order' => '0',
175. ),
176. 5 =>
177. array (
178. 'goods_id' => '8',
179. 'sort_order' => '0',
180. ),
181. 6 =>
182. array (
183. 'goods_id' => '9',
184. 'sort_order' => '0',
185. ),
186. 7 =>
187. array (
188. 'goods_id' => '13',
189. 'sort_order' => '0',
190. ),
191. 8 =>
192. array (
193. 'goods_id' => '10',
194. 'sort_order' => '0',
195. ),
196. 9 =>
197. array (
198. 'goods_id' => '1',
199. 'sort_order' => '0',
200. ),
201. 10 =>
202. array (
203. 'goods_id' => '14',
204. 'sort_order' => '0',
205. ),
206. 11 =>
207. array (
208. 'goods_id' => '17',
209. 'sort_order' => '0',
210. ),
211. 12 =>
212. array (
213. 'goods_id' => '27',
214. 'sort_order' => '0',
215. ),
216. 13 =>
217. array (
218. 'goods_id' => '30',
219. 'sort_order' => '0',
220. ),
221. 14 =>
222. array (
223. 'goods_id' => '25',
224. 'sort_order' => '0',
225. ),
226. 15 =>
227. array (
228. 'goods_id' => '29',
229. 'sort_order' => '0',
230. ),
231. 16 =>
232. array (
233. 'goods_id' => '28',
234. 'sort_order' => '0',
235. ),
236. 17 =>
237. array (
238. 'goods_id' => '26',
239. 'sort_order' => '0',
240. ),
241. ),
242. 'brand' =>
243. array (
244. 33 => 'jk',
245. 19 => '三星',
246. 32 => '诺基亚',
247. 24 => '联想',
248. 20 => '三星',
249. 12 => '摩托罗拉',
250. 22 => '多普达',
251. 8 => '飞利浦',
252. 23 => '诺基亚',
253. 9 => '诺基亚',
254. 13 => '诺基亚',
255. 10 => '索爱',
256. 1 => 'LG',
257. 14 => '诺基亚',
258. 17 => '夏新',
259. 5 => '索爱',
260. ),
261. );
262. ?>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: