This repository has been archived by the owner on May 22, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 89
/
Copy pathDataCache.php
155 lines (142 loc) · 4.44 KB
/
DataCache.php
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
<?php
namespace Douyasi\Cache;
use Cache;
use DB;
use Config;
/**
* Class DataCache
*
* 数据缓存
* 用于缓存比较通用的内容Content/元Meta数据
* 注意:这里存取数据未使用仓库,而是直接DB操作
*
* @package Douyasi\Cache
* @author raoyc <raoyc2009@gmail.com>
*/
class DataCache
{
/**
* 缓存内容 (暂未完成 TODO)
*
* @return boolean true
*/
public static function cacheContent($category)
{
return true;
}
/**
* 缓存分类
*
* @return boolean true
*/
public static function cacheCategories()
{
$categories = array();
//这里变量$cats含义不是猫(复数),而是分类复数的缩写(categories)
$cats = DB::table('metas')
->select('id', 'name', 'slug')
->where('type', '=', 'CATEGORY')
->get();
if (!empty($cats)) {
foreach ($cats as $key=>$cat) {
$count = DB::table('contents')
->where('category_id', '=', $cat->id)
->where('type', '=', 'article')
->count();
$categories[$key]['category'] = $cat;
$categories[$key]['count'] = $count;
}
}
Cache::remember('categories', 120, function () use ($categories) {
return $categories;
});
return true;
}
/**
* 缓存标签 (暂未完成 TODO)
*
* @return boolean true
*/
public static function cacheTags()
{
return true;
}
/**
* 缓存推荐位
*
* @return boolean true
*/
public static function cacheFlags()
{
$cache_flags = array();
$flags = DB::table('flags')
->select('id', 'attr', 'attr_full_name', 'display_name')
->take(5)
->get();
if(!empty($flags)) {
foreach($flags as $flag) {
$cache_flags[$flag->attr] = $flag->display_name.'('.$flag->attr_full_name.')';
}
}
Cache::remember('flags', 120, function () use ($cache_flags) {
return $cache_flags;
});
return true;
}
/**
* 缓存存档,数据量大时可加快访问速度
*
* @return boolean true
*/
public static function cacheArchive()
{
$archives = array();
$yms = DB::table('contents')
->select(DB::raw('DATE_FORMAT (created_at, "%Y-%m") AS post_year_month'))
->where('type', '=', 'article')
->distinct()
->orderBy('post_year_month', 'desc')
->get();
if (!empty($yms)) {
foreach ($yms as $key=>$ym) {
$articles = DB::table('contents')
->join('metas', function ($join) {
$join->on('metas.id', '=', 'category_id')
->where('metas.type', '=', 'CATEGORY');
})
->select('metas.id as c_id', 'metas.slug as c_slug', 'contents.title', 'contents.id', 'contents.slug', 'contents.category_id')
->where('contents.created_at', 'LIKE', ''.$ym->post_year_month.'%')
->where('contents.type', '=', 'article')
->get();
if (!empty($articles)) {
$archives[$key]['year_month'] = $ym->post_year_month;
$archives[$key]['articles'] = $articles;
$archives[$key]['count'] = count($articles);
} else {
$archives[$key]['year_month'] = $ym->post_year_month;
$archives[$key]['articles'] = array();
$archives[$key]['count'] = 0;
}
}
}
Cache::remember('archives', 120, function () use ($archives) {
return $archives;
});
return true;
}
/**
* 重建数据缓存
*
* @return boolean true
*/
public static function rebuildDataCache()
{
Cache::forget('categories'); //清理掉分类缓存
Cache::forget('archives'); //清理掉存档缓存
Cache::forget('flags'); //清理掉推荐位缓存
static::cacheCategories();
static::cacheArchive();
static::cacheFlags();
return true;
}
}