ecshop源码分析——静态缓存static_caches

static_caches缓存文件存放在ecshop/temp/static_caches下面

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

该函数在ecshop/includes/lib_base.php

写缓存 

 

 

/**
 * 将结果写进缓存文件
 *
 * @params  string  $cache_name 缓存文件的名字
 * @params  string  $caches          缓存的内容
 *
 * @return
 */
function write_static_cache($cache_name, $caches)
{
    if ((DEBUG_MODE & 2) == 2)
    {
        return false;
    }
//缓存的路径
    $cache_file_path = ROOT_PATH . '/temp/static_caches/' . $cache_name . '.php';
//缓存内容$content
    $content = "<?php\r\n";
    $content .= "\$data = " . var_export($caches, true) . ";\r\n";
    $content .= "?>";
//将内容写进缓存
    file_put_contents($cache_file_path, $content, LOCK_EX);
}

 

 读缓存

/**
 * 读结果缓存文件
 *
 * @params  string  $cache_name//缓存文件的名字
 *
 * @return  array   $data
 */
function read_static_cache($cache_name)
{
    if ((DEBUG_MODE & 2) == 2)
    {
        return false;
    }
//注意这里的静态变量用法
    static $result = array();
//如果已经从缓存文件中读取了数据则直接返回结果
    if (!empty($result[$cache_name]))
    {
        return $result[$cache_name];
    }
//缓存文件的路径
    $cache_file_path = ROOT_PATH . '/temp/static_caches/' . $cache_name . '.php';
//如果缓存文件存在就读取缓存
    if (file_exists($cache_file_path))
    {
        include_once($cache_file_path);
        $result[$cache_name] = $data;
        return $result[$cache_name];
    }
    else
    {
        return false;
    }
}

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

 

ecshop/inlucdes/lib_goods.php


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

 

/**
 * 获得推荐商品
 *
 * @access  public
 * @param   string      $type       推荐类型,可以是 best, new, hot
 * @return  array
 */
//代码我省略了一些,主要说明与缓存相关的代码
 */
function get_recommend_goods($type = '', $cats = '')
{
  /* 省略的代码。。。。*/
  /*读取缓存文件*/
  $data = read_static_cache('recommend_goods');
  if($data==false) //如果缓存文件不存在
  {
      /*从数据库中获取需要的数据*/
     /*将从数据库中获取的数据写入到相应的缓存文件当中*/
      write_static_cache('recommend_goods', $goods_data);
  }else //如果缓存文件存在
  {
      /*直接使用缓存数据*/
      $good_data=$data;
  }
}
//缓存结果在这里     <?php
$data = array (
  'best' => 
  array (
    0 => 
    array (
      'goods_id' => '33',
      'sort_order' => '0',
    ),
    1 => 
    array (
      'goods_id' => '19',
      'sort_order' => '0',
    ),
    2 => 
    array (
      'goods_id' => '24',
      'sort_order' => '0',
    ),
    3 => 
    array (
      'goods_id' => '20',
      'sort_order' => '0',
    ),
    4 => 
    array (
      'goods_id' => '22',
      'sort_order' => '0',
    ),
    5 => 
    array (
      'goods_id' => '8',
      'sort_order' => '0',
    ),
    6 => 
    array (
      'goods_id' => '23',
      'sort_order' => '0',
    ),
    7 => 
    array (
      'goods_id' => '9',
      'sort_order' => '0',
    ),
    8 => 
    array (
      'goods_id' => '1',
      'sort_order' => '0',
    ),
    9 => 
    array (
      'goods_id' => '17',
      'sort_order' => '0',
    ),
    10 => 
    array (
      'goods_id' => '27',
      'sort_order' => '0',
    ),
    11 => 
    array (
      'goods_id' => '30',
      'sort_order' => '0',
    ),
    12 => 
    array (
      'goods_id' => '25',
      'sort_order' => '0',
    ),
    13 => 
    array (
      'goods_id' => '29',
      'sort_order' => '0',
    ),
    14 => 
    array (
      'goods_id' => '5',
      'sort_order' => '0',
    ),
  ),
  'new' => 
  array (
    0 => 
    array (
      'goods_id' => '33',
      'sort_order' => '0',
    ),
    1 => 
    array (
      'goods_id' => '19',
      'sort_order' => '0',
    ),
    2 => 
    array (
      'goods_id' => '32',
      'sort_order' => '0',
    ),
    3 => 
    array (
      'goods_id' => '24',
      'sort_order' => '0',
    ),
    4 => 
    array (
      'goods_id' => '20',
      'sort_order' => '0',
    ),
    5 => 
    array (
      'goods_id' => '12',
      'sort_order' => '0',
    ),
    6 => 
    array (
      'goods_id' => '22',
      'sort_order' => '0',
    ),
    7 => 
    array (
      'goods_id' => '8',
      'sort_order' => '0',
    ),
    8 => 
    array (
      'goods_id' => '23',
      'sort_order' => '0',
    ),
    9 => 
    array (
      'goods_id' => '9',
      'sort_order' => '0',
    ),
    10 => 
    array (
      'goods_id' => '1',
      'sort_order' => '0',
    ),
    11 => 
    array (
      'goods_id' => '27',
      'sort_order' => '0',
    ),
    12 => 
    array (
      'goods_id' => '5',
      'sort_order' => '0',
    ),
  ),
  'hot' => 
  array (
    0 => 
    array (
      'goods_id' => '33',
      'sort_order' => '0',
    ),
    1 => 
    array (
      'goods_id' => '19',
      'sort_order' => '0',
    ),
    2 => 
    array (
      'goods_id' => '32',
      'sort_order' => '0',
    ),
    3 => 
    array (
      'goods_id' => '24',
      'sort_order' => '0',
    ),
    4 => 
    array (
      'goods_id' => '20',
      'sort_order' => '0',
    ),
    5 => 
    array (
      'goods_id' => '8',
      'sort_order' => '0',
    ),
    6 => 
    array (
      'goods_id' => '9',
      'sort_order' => '0',
    ),
    7 => 
    array (
      'goods_id' => '13',
      'sort_order' => '0',
    ),
    8 => 
    array (
      'goods_id' => '10',
      'sort_order' => '0',
    ),
    9 => 
    array (
      'goods_id' => '1',
      'sort_order' => '0',
    ),
    10 => 
    array (
      'goods_id' => '14',
      'sort_order' => '0',
    ),
    11 => 
    array (
      'goods_id' => '17',
      'sort_order' => '0',
    ),
    12 => 
    array (
      'goods_id' => '27',
      'sort_order' => '0',
    ),
    13 => 
    array (
      'goods_id' => '30',
      'sort_order' => '0',
    ),
    14 => 
    array (
      'goods_id' => '25',
      'sort_order' => '0',
    ),
    15 => 
    array (
      'goods_id' => '29',
      'sort_order' => '0',
    ),
    16 => 
    array (
      'goods_id' => '28',
      'sort_order' => '0',
    ),
    17 => 
    array (
      'goods_id' => '26',
      'sort_order' => '0',
    ),
  ),
  'brand' => 
  array (
    33 => 'jk',
    19 => '三星',
    32 => '诺基亚',
    24 => '联想',
    20 => '三星',
    12 => '摩托罗拉',
    22 => '多普达',
    8 => '飞利浦',
    23 => '诺基亚',
    9 => '诺基亚',
    13 => '诺基亚',
    10 => '索爱',
    1 => 'LG',
    14 => '诺基亚',
    17 => '夏新',
    5 => '索爱',
  ),
);
?>

 

你可能感兴趣的:(PHP,工作,cache,Access,三星)