ecshop源码分析——静态缓存static_c…

http://guozhiwei.javaeye.com/blog/673291

http://www.phpall.cn


static_caches缓存文件

存放在ecshop/temp/static_caches下面

先来看缓存工作的2个主要函数,写缓存和读缓存。

该函数在ecshop/includes/lib_base.php

写缓存



 


  1. function write_static_cache($cache_name, $caches)
  2. {
  3.     if ((DEBUG_MODE & 2) == 2)
  4.     {
  5.         return false;
  6.     }
  7. //缓存的路径
  8.     $cache_file_path = ROOT_PATH . '/temp/static_caches/' . $cache_name . '.php';
  9. //缓存内容$content
  10.     $content = "<?php\r\n";
  11.     $content .= "\$data = " . var_export($caches, true) . ";\r\n";
  12.     $content .= "?>";
  13. //将内容写进缓存
  14.     file_put_contents($cache_file_path, $content, LOCK_EX);
  15. }
复制代码


读缓存


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



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


ecshop/inlucdes/lib_goods.php


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


 


  1. //代码我省略了一些,主要说明与缓存相关的代码
  2. */
  3. function get_recommend_goods($type = '', $cats = '')
  4. {
  5.   
  6.   
  7.   $data = read_static_cache('recommend_goods');
  8.   if($data==false) //如果缓存文件不存在
  9.   {
  10.       
  11.      
  12.       write_static_cache('recommend_goods', $goods_data);
  13.   }else //如果缓存文件存在
  14.   {
  15.       
  16.       $good_data=$data;
  17.   }
  18. }
复制代码

  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. ?>
原文链接:http://bbs.phpchina.com/thread-183103-1-1.html
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值