/
Caching(3).php
179 lines (155 loc) · 5.14 KB
/
Caching(3).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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
<?php
/**
* http://blog.astrumfutura.com/archives/381-Zend-Framework-Page-Caching-Part-2-Controller-Based-Cache-Management.html
* http://www.survivethedeepend.com/zendframeworkbook/en/1.0/performance.optimisation.for.zend.framework.applications
* http://nqdung.plus.vn/?p=278
* http://devzone.zend.com/article/3372
* http://www.maycode.com/index.php/hotspot/32-web20/1110-zend.html
Our cache criteria will be as follows:
- Cache configuration will be passed to the constructor
- Only GET requests will be cached
- Redirects will not be cached
- Any given action can tell the plugin to skip caching
*/
class Digitalus_Controller_Plugin_Caching extends Zend_Controller_Plugin_Abstract
{
/**
* @var bool Whether or not to disable caching
*/
public static $doNotCache = false;
/**
* @var Zend_Cache_Frontend
*/
public $_cache;
/**
* @var string Cache key
*/
public $_key;
public $_keyTags;
/**
* Constructor: initialize cache
*
* @param array|Zend_Config $options
* @return void
* @throws Exception
*/
public function __construct()
{
// Khoi tao cache
// $this->_cache = Zend_Registry::get('cache');
// Cache options
// $frontendOptions = array(
// 'lifetime' => 1200, // Cache lifetime of 20 minutes
// 'automatic_serialization' => true,
// );
// $backendOptions = array(
// 'lifetime' => 3600, // Cache lifetime of 1 hour
// 'cache_dir' => BASE_PATH . '/cache/', // Directory where to put the cache files
// );
// // Get a Zend_Cache_Core object
// $this->_cache = Zend_Cache::factory('Core', 'File', $frontendOptions, $backendOptions);
// Zend_Registry::set('cache', $cache);
// Return it, so that it can be stored by the bootstrap
// return $cache;
// if ($options instanceof Zend_Config) {
// $options = $options->toArray();
// }
// if (!is_array($options)) {
// throw new Exception('Invalid cache options; must be array or Zend_Config object');
// }
//
// if (array('frontend', 'backend', 'frontendOptions', 'backendOptions') != array_keys($options)) {
// throw new Exception('Invalid cache options provided');
// }
//
// $options['frontendOptions']['automatic_serialization'] = true;
//
// $this->cache = Zend_Cache::factory(
// $options['frontend'],
// $options['backend'],
// $options['frontendOptions'],
// $options['backendOptions']
// );
}
/**
* Start caching
*
* Determine if we have a cache hit. If so, return the response; else,
* start caching.
*
* @param Zend_Controller_Request_Abstract $request
* @return void
*/
// public function dispatchLoopStartup(Zend_Controller_Request_Abstract $request)
// {
//
//// echo "<pre>";
//// print_r($request->getRequestUri());
//// echo "</pre>";
//// exit();
//
// if (!$request->isGet())
// {
// self::$doNotCache = true;
// return;
// }
//
// $path = $request->getPathInfo();
//
//
// // co loi o day , xem link de biet cach sua
// $this->_key = md5($path);
// if (false !== ($response = $this->getCache()))
// {
// $response->sendResponse();
// exit;
// }
//
// }
public function preDispatch(Zend_Controller_Request_Abstract $request)
{
$this->_cache = Zend_Registry::get('cache');
// La la cac phuong thuc khac get() no se khong lay tu content tu cache ra
if (!$request->isGet())
{
self::$doNotCache = true;
return;
}
$module = $request->getModuleName();
$controller = $request->getControllerName();
$action = $request->getActionName();
$path = $request->getPathInfo();
// co loi o day , xem link de biet cach sua
$this->_key = md5($path);
$this->_keyTags = array($module,"{$module}_{$controller}","{$module}_{$controller}_{$action}");
if (false !== ($data = $this->getCache()))
{
$response = $this->getResponse();
$response->setBody($data['default']);
$response->sendResponse();
exit;
}
}
/**
* Store cache
*
* @return void
*/
public function dispatchLoopShutdown()
{
if (self::$doNotCache || $this->getResponse()->isRedirect() || (null === $this->_key))
{
return;
}
$response = $this->getResponse();
$data = $response->getBody(true);
$this->_cache->save($data, $this->_key,$this->_keyTags);
}
private function getCache()
{
if( ($data = $this->_cache->load($this->_key)) != false) {
return $data;
}
return false;
}
}