memcached 细究(三)

今天研究ecshop的cls_mysql类库,

涉及到mysql 和memcache 的整合 category.php

商品属性帅选时使用memcache缓存

mysql

public function getColCached($sql, $cached = 'FILEFIRST')
{
$cachefirst = ($cached == 'FILEFIRST' || ($cached == 'MYSQLFIRST' && $this->platform != 'WINDOWS')) && $this->max_cache_time;
if (!$cachefirst)
{
return $this->getCol($sql);
}
else
{
$result = $this->getSqlCacheData($sql, $cached);
if (empty($result['storecache']) == true)
{
return $result['data'];
}
}
$arr = $this->getCol($sql);
if ($arr !== false && $cachefirst)
{
$this->setSqlCacheData($result, $arr);
}
return $arr;
}

function getSqlCacheData($sql, $cached = '')
{
$sql = trim($sql);
$result = array();
$result['filename'] = $this->root_path . $this->cache_data_dir . 'sqlcache_' . abs(crc32($this->dbhash . $sql)) . '_' . md5($this->dbhash . $sql) . '.php';
$mem=Instance_Memcached();
if($mem===false)
{
$data = @file_get_contents($result['filename']);
}
else{
$key='SqlCacheData_'.substr(md5($result['filename']),0,8);
$data=$mem->get($key);
}
if (isset($data{23}))
{
$filetime = substr($data, 13, 10);
$data = substr($data, 23);
if (($cached == 'FILEFIRST' && time() > $filetime + $this->max_cache_time) || ($cached == 'MYSQLFIRST' && $this->table_lastupdate($this->get_table_name($sql)) > $filetime))
{
$result['storecache'] = true;
}
else
{
$result['data'] = @unserialize($data);
if ($result['data'] === false)
{
$result['storecache'] = true;
}
else
{
$result['storecache'] = false;
}
}
}
else
{
$result['storecache'] = true;
}
return $result;
}
function setSqlCacheData($result, $data)
{
$mem=Instance_Memcached();
if($mem===false)
{
if ($result['storecache'] === true && $result['filename'])
{
@file_put_contents($result['filename'], '<?php exit;?>' . time() . serialize($data));
clearstatcache();
}
}
else{
$key='SqlCacheData_'.substr(md5($result['filename']),0,8);
$mem->set($key,'<?php exit;?>' . time() . serialize($data),0,$this->max_cache_time);
}
}

/* 属性筛选 */
if ($cat['filter_attr'] > 0)
{
$cat_filter_attr = explode(',', $cat['filter_attr']); //提取出此分类的筛选属性
$all_attr_list = array();
$attr_selected = array();
$ext_goods_ids=get_extension_goods($children,true);
$attr_filter=get_category_attr_filter($cat_id,$cat,$cat_id_array,$cat_filter_attr,$filter_attr,$brand,$shipin24hrs,$onsales,$ext_goods_ids);
$smarty->assign('attr_selected', $attr_filter['attr_selected']);
$smarty->assign('filter_attr_list', $attr_filter['filter_attr_list']);

/* 扩展商品查询条件 */
$ext = ''; //商品查询条件扩展
$ext_group_goods = array();
$ext_group_goods_intersect = array();
if (!empty($filter_attr))
{
$ext_sql = "SELECT DISTINCT(b.goods_id) FROM " . $ecs->table('goods_attr') . " AS a, " . $ecs->table('goods_attr') . " AS b " . "WHERE ";
foreach ($filter_attr AS $k => $v) // 查出符合所有筛选属性条件的商品id */
{
if ($v != 0)
{
$sql = $ext_sql . "b.attr_value = a.attr_value AND b.attr_id = '" . $cat_filter_attr[$k] ."' AND a.goods_attr_id = " . $v;
$ext_group_goods = $db->getColCached($sql);
$ext .= ' AND ' . db_create_in($ext_group_goods, 'g.goods_id');
$ext_group_goods_intersect=empty($ext_group_goods_intersect)? $ext_group_goods : array_intersect($ext_group_goods_intersect,$ext_group_goods);
}
}
$sql="select attr_value from ".$ecs->table('goods_attr')." where goods_attr_id in (".implode(',',$filter_attr).")";
$attr_values=$db->getCol($sql);
if($attr_values)
{
foreach($attr_values as $key => $value)
{
$value=explode('_',$value);
$attr_values[$key]=$value[0];
}
}
$attr_values=implode(' ',$attr_values);
$smarty->assign('attr_values', $attr_values);
}
unset($ext_group_goods);
if (!empty($filter_attr) && empty($ext_group_goods_intersect))
{
$ext_group_goods_intersect=false;
}
}

posted @ 2017-07-20 00:51  JasonXu_徐晓峰的博客  阅读(285)  评论(0编辑  收藏  举报