Search.php
3.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
<?php
namespace Product;
use Configs\CacheConfig;
use LibModels\Wap\Product\SearchData;
use LibModels\Wap\Category\BrandData;
use LibModels\Wap\Category\ClassData;
use Plugin\DataProcess\ListProcess;
use Plugin\Cache;
/**
* 搜索相关的模板数据模型
*
* @name Search
* @package Product
* @copyright yoho.inc
* @version 1.0 (2015-11-1 14:35:52)
*/
class SearchModel
{
/**
* 获取搜索的数据
*
* @param array $condition 查询条件
* @return array
*/
public static function getSearchData($condition)
{
$result = array();
if (USE_CACHE) {
$key = CacheConfig::KEY_ACTION_SEARCH_SEARCH;
if (!empty($condition)) {
$key .= http_build_query($condition, null, '&');
}
// 先尝试获取一级缓存(master), 有数据则直接返回.
$result = Cache::get($key, 'master');
if (!empty($result)) {
return $result;
}
}
// 调用接口查询数据 (使用新的 Elastic Engine)
$listData = SearchData::searchElasticByCondition($condition);
// 调用接口查询数据 (使用老的 Sphinx Engine)
// $listData = SearchData::searchByCondition($condition);
// 处理返回的数据
if (isset($listData['code']) && $listData['code'] === 200) {
$result = ListProcess::getListData($listData['data'], false);
}
if (USE_CACHE) {
// 接口调用异常时, 不害怕,从我们的二级缓存(slave)里再取数据.
if (empty($result)) {
$result = Cache::get($key, 'slave');
}
// 接口调用正常,数据封装完成, 则设置一级(master)和二级(slave)数据缓存
else {
Cache::set($key, $result, 300); // 缓存5分钟
}
}
return $result;
}
/**
* 获取筛选的数据
*
* @param array $condition 查询条件
* @return array
*/
public static function getFilterData($condition)
{
$result = array();
if (USE_CACHE) {
$key = CacheConfig::KEY_ACTION_SEARCH_FILTER;
if (!empty($condition)) {
$key .= http_build_query($condition, null, '&');
}
// 先尝试获取一级缓存(master), 有数据则直接返回.
$result = Cache::get($key, 'master');
if (!empty($result)) {
return $result;
}
}
// 设置选定的gender
$gender = '1,2,3';
if (isset($condition['gender'])) {
$gender = $condition['gender'];
}
// 区别各种列表页面的筛选数据
if (isset($condition['shop_id'])) {
$condition['shop'] = $condition['shop_id'];
unset($condition['shop_id']);
$listData = SearchData::searchByCondition($condition);
$exclude = 'null';
}elseif (isset($condition['brand'])) {
$listData = BrandData::filterBrandData($condition);
$exclude = 'brand';
} else if (isset($condition['sort'])) {
$listData = ClassData::filterClassData($condition);
$exclude = 'group_sort';
} else {
$listData = SearchData::searchByCondition($condition);
$exclude = null;
}
if (isset($listData['data']['filter'])) {
$result['filter'] = ListProcess::getFilterData($listData['data']['filter'], $gender, $exclude);
}
if (USE_CACHE) {
// 接口调用异常时, 不害怕,从我们的二级缓存(slave)里再取数据.
if (empty($result)) {
$result = Cache::get($key, 'slave');
}
// 接口调用正常,数据封装完成, 则设置一级(master)和二级(slave)数据缓存
else {
Cache::set($key, $result, 300); // 缓存5分钟
}
}
return $result;
}
}