Avip_Cache - Version 0.1

Version Notes

Tested only with Magento 1.3, could work with above.

Download this release

Release Info

Developer Magento Core Team
Extension Avip_Cache
Version 0.1
Comparing to
See all releases


Version 0.1

app/code/community/Avip/Cache/Model/Product/Observer.php ADDED
@@ -0,0 +1,130 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ class Avip_Cache_Model_Product_Observer
3
+ {
4
+ protected $cache;
5
+ private $stores = array();
6
+
7
+ public function __construct()
8
+ {
9
+ $this->cache = Avip_Cache::getInstance();
10
+ }
11
+
12
+ private function getSession()
13
+ {
14
+ return Mage::getSingleton('customer/session');
15
+ }
16
+
17
+ /**
18
+ * Launch Page Caching
19
+ * @param Varien_Event_Observer $observer
20
+ * @return Avip_Cache_Model_Product_Observer
21
+ */
22
+ public function start($observer)
23
+ {
24
+ $cache_update = Mage::app()->getRequest()->getParam('cache_update', false);
25
+ $this->cache->start($cache_update);
26
+ return $this;
27
+ }
28
+
29
+ /**
30
+ * Clean before Add product
31
+ * @param Varien_Event_Observer $observer
32
+ * @return Avip_Cache_Model_Product_Observer
33
+ */
34
+ public function add_clean($observer)
35
+ {
36
+ $event = $observer->getEvent();
37
+ $product = $event->getProduct();
38
+
39
+ if($this->cache && $this->cache->isStarted()){
40
+ $this->cache->clean($product->getId());
41
+
42
+ $session = $this->getSession();
43
+ if($session->isLoggedIn())
44
+ {
45
+ $this->cache->clean('customer_' . $session->getId());
46
+ }
47
+ }
48
+
49
+ return $this;
50
+ }
51
+
52
+ /**
53
+ * Add product id to current tags
54
+ * @param Varien_Event_Observer $observer
55
+ * @return Avip_Cache_Model_Product_Observer
56
+ */
57
+ public function add($observer)
58
+ {
59
+ $event = $observer->getEvent();
60
+ $products = $event->getCollection();
61
+
62
+ if($this->cache && $this->cache->isStarted()){
63
+ $tags = array();
64
+ foreach ($products as $product){
65
+ $tags[] = $product->getId();
66
+ }
67
+ if(!empty($tags))
68
+ {
69
+ $this->cache->addTags($tags);
70
+ }
71
+ }
72
+
73
+ return $this;
74
+ }
75
+
76
+ /**
77
+ * remove cache page for given product
78
+ * @param Varien_Event_Observer $observer
79
+ * @return Avip_Cache_Model_Product_Observer
80
+ */
81
+ public function clean($observer)
82
+ {
83
+ $event = $observer->getEvent();
84
+ $item = $event->getQuoteItem();
85
+
86
+ if($this->cache && $this->cache->isStarted()){
87
+ $this->cache->clean($item->getData('product_id'));
88
+
89
+ $session = $this->getSession();
90
+ if($session->isLoggedIn())
91
+ {
92
+ $this->cache->clean('customer_' . $session->getId());
93
+ }
94
+ }
95
+
96
+ return $this;
97
+ }
98
+
99
+ /**
100
+ * Generate cache based upon url
101
+ */
102
+ public function dailyUrlCacheUpdate()
103
+ {
104
+ $collection = Mage::getResourceModel('core/url_rewrite_collection');
105
+ foreach($collection as $url)
106
+ {
107
+ $store = $this->getStore($url->getStoreId());
108
+ if($store)
109
+ {
110
+ $url = Mage::getBaseUrl('web') . $store->getCode() . '/' . $url->getRequestPath() . '?cache_update=true';
111
+ Mage::log($url);
112
+ get_headers($url);
113
+ }
114
+ }
115
+ }
116
+
117
+ private function getStore($id)
118
+ {
119
+ if(!in_array($id, array_keys($stores)))
120
+ {
121
+ $store = Mage::getModel('store')->load($id);
122
+ $this->stores[$id] = $store;
123
+ }else{
124
+ $store = $this->stores[$id];
125
+ }
126
+
127
+ return $store;
128
+ }
129
+ }
130
+ ?>
app/code/community/Avip/Cache/etc/config.xml ADDED
@@ -0,0 +1,72 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <config>
3
+ <modules>
4
+ <Avip_Cache>
5
+ <version>0.1</version>
6
+ </Avip_Cache>
7
+ </modules>
8
+ <global>
9
+ <models>
10
+ <product>
11
+ <class>Avip_Cache_Model_Product</class>
12
+ </product>
13
+ </models>
14
+ <events>
15
+ <checkout_cart_product_add_after>
16
+ <observers>
17
+ <avip>
18
+ <type>singleton</type>
19
+ <class>product/observer</class>
20
+ <method>add_clean</method>
21
+ </avip>
22
+ </observers>
23
+ </checkout_cart_product_add_after>
24
+ <catalog_product_collection_load_after>
25
+ <observers>
26
+ <avip>
27
+ <type>singleton</type>
28
+ <class>product/observer</class>
29
+ <method>add</method>
30
+ </avip>
31
+ </observers>
32
+ </catalog_product_collection_load_after>
33
+ <wishlist_add_product>
34
+ <observers>
35
+ <avip>
36
+ <type>singleton</type>
37
+ <class>product/observer</class>
38
+ <method>add_clean</method>
39
+ </avip>
40
+ </observers>
41
+ </wishlist_add_product>
42
+ <sales_quote_remove_item>
43
+ <observers>
44
+ <avip>
45
+ <type>singleton</type>
46
+ <class>product/observer</class>
47
+ <method>clean</method>
48
+ </avip>
49
+ </observers>
50
+ </sales_quote_remove_item>
51
+ <controller_front_init_before>
52
+ <observers>
53
+ <avip>
54
+ <type>singleton</type>
55
+ <class>product/observer</class>
56
+ <method>start</method>
57
+ </avip>
58
+ </observers>
59
+ </controller_front_init_before>
60
+ </events>
61
+ </global>
62
+ <crontab>
63
+ <jobs>
64
+ <catalogrule_apply_all>
65
+ <schedule><cron_expr>0 1 * * *</cron_expr></schedule>
66
+ <run>
67
+ <model>product/dailyUrlCacheUpdate</model>
68
+ </run>
69
+ </catalogrule_apply_all>
70
+ </jobs>
71
+ </crontab>
72
+ </config>
lib/Avip/Cache.php ADDED
@@ -0,0 +1,114 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ class Avip_Cache
3
+ {
4
+ // Hold an instance of the class
5
+ private static $instance;
6
+ private $cache = null;
7
+ private $started = false;
8
+
9
+ // A private constructor; prevents direct creation of object
10
+ private function __construct()
11
+ {
12
+
13
+ }
14
+
15
+ // Prevent users to clone the instance
16
+ public function __clone()
17
+ {
18
+ trigger_error('Clone is not allowed.', E_USER_ERROR);
19
+ }
20
+
21
+ // The singleton method
22
+ public static function getInstance()
23
+ {
24
+ if (!isset(self::$instance)) {
25
+ $c = __CLASS__;
26
+ self::$instance = new $c;
27
+ self::$instance->init();
28
+ }
29
+
30
+ return self::$instance;
31
+ }
32
+
33
+ /**
34
+ * Use Page cache
35
+ */
36
+ private function init()
37
+ {
38
+ if(!isset($_SESSION))session_start() ;
39
+
40
+ $this->loadCache();
41
+ }
42
+
43
+ public function start()
44
+ {
45
+ if(!is_null($this->cache))
46
+ {
47
+ if(!$this->started){
48
+ $this->cache->start();
49
+ $this->started = true;
50
+ }
51
+ }
52
+ else
53
+ {
54
+ throw new Exception("Aucun moteur de cache n'est chargé");
55
+ }
56
+ }
57
+
58
+ public function addTags($tags)
59
+ {
60
+ if($this->started){
61
+ $this->cache->addTags($tags);
62
+ }
63
+ }
64
+
65
+ public function clean($tags)
66
+ {
67
+ if($this->started){
68
+ if(!is_array($tags))
69
+ {
70
+ $tags = array(0=>$tags);
71
+ }
72
+ Mage::log('ici_clean_step_0');
73
+ $this->cache->clean('matchingTag', $tags);
74
+ $this->cache->cancel();
75
+ }
76
+ }
77
+
78
+ public function isStarted()
79
+ {
80
+ return $this->started;
81
+ }
82
+
83
+ protected function loadCache()
84
+ {
85
+ $frontendOptions = array(
86
+ 'lifetime' => 3600,
87
+ 'default_options' => array(
88
+ 'cache' => false
89
+ ),
90
+ 'debug_header' => false,
91
+ 'regexps' => array(
92
+ '^/$' => array('cache' => true),
93
+ '^/fr_bp/customer' => array('cache' => false),
94
+ '^/fr_bp/boutiques' => array('cache' => true),
95
+ '^/catalog/category' => array('cache' => true),
96
+ '^/catalog/vente' => array('cache' => true),
97
+ )
98
+ );
99
+
100
+ $backendOptions = array(
101
+ 'cache_dir' => Mage::getBaseDir('var') . DIRECTORY_SEPARATOR . 'tmp',
102
+ );
103
+
104
+ $frontend = new Avip_Cache_Frontend_Page($frontendOptions);
105
+
106
+ $this->cache = Zend_Cache::factory(
107
+ $frontend,
108
+ 'File',
109
+ $frontendOptions,
110
+ $backendOptions
111
+ );
112
+ }
113
+
114
+ }
lib/Avip/Cache/Frontend/Page.php ADDED
@@ -0,0 +1,249 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * @category Avip
4
+ * @package Avip_Cache
5
+ * @subpackage Avip_Cache_Frontend
6
+ * @copyright Copyright (c) 2010
7
+ * @license http://framework.zend.com/license/new-bsd New BSD License
8
+ */
9
+
10
+
11
+ /**
12
+ * @see Zend_Cache_Core
13
+ */
14
+ #require_once 'Zend/Cache/Core.php';
15
+
16
+
17
+ /**
18
+ * @package Avip_Cache
19
+ * @subpackage Avip_Cache_Frontend
20
+ * @copyright Copyright (c) 2010
21
+ * @license http://framework.zend.com/license/new-bsd New BSD License
22
+ */
23
+ class Avip_Cache_Frontend_Page extends Zend_Cache_Frontend_Page
24
+ {
25
+ /**
26
+ * @var array options
27
+ */
28
+ protected $_specificOptions = array(
29
+ 'http_conditional' => false,
30
+ 'debug_header' => false,
31
+ 'content_type_memorization' => true,
32
+ 'memorize_headers' => array(),
33
+ 'default_options' => array(
34
+ 'cache_with_get_variables' => true,
35
+ 'cache_with_post_variables' => true,
36
+ 'cache_with_session_variables' => true,
37
+ 'cache_with_files_variables' => false,
38
+ 'cache_with_cookie_variables' => false,
39
+ 'make_id_with_get_variables' => true,
40
+ 'make_id_with_post_variables' => true,
41
+ 'make_id_with_session_variables' => true,
42
+ 'make_id_with_files_variables' => true,
43
+ 'make_id_with_cookie_variables' => true,
44
+ 'cache' => true,
45
+ 'specific_lifetime' => false,
46
+ 'tags' => array(),
47
+ 'priority' => null
48
+ ),
49
+ 'regexps' => array()
50
+ );
51
+
52
+ /**
53
+ * Start the cache
54
+ *
55
+ * @param string $id (optional) A cache id (if you set a value here, maybe you have to use Output frontend instead)
56
+ * @param boolean $doNotDie For unit testing only !
57
+ * @return boolean True if the cache is hit (false else)
58
+ */
59
+ public function start($id = false, $doNotDie = false)
60
+ {
61
+
62
+ $this->_cancel = false;
63
+ $lastMatchingRegexp = null;
64
+ foreach ($this->_specificOptions['regexps'] as $regexp => $conf) {
65
+ if (preg_match("`$regexp`", $_SERVER['REQUEST_URI'])) {
66
+ $lastMatchingRegexp = $regexp;
67
+ }
68
+ }
69
+
70
+ $this->_activeOptions = $this->_specificOptions['default_options'];
71
+ if (!is_null($lastMatchingRegexp)) {
72
+ $conf = $this->_specificOptions['regexps'][$lastMatchingRegexp];
73
+ foreach ($conf as $key=>$value) {
74
+ $this->_activeOptions[$key] = $value;
75
+ }
76
+ }
77
+ if (!($this->_activeOptions['cache'])) {
78
+ return false;
79
+ }
80
+
81
+ if (!$id) {
82
+ $id = $this->_makeId();
83
+ if (!$id) {
84
+ return false;
85
+ }
86
+ }
87
+
88
+ if($this->getSession()->isLoggedIn())
89
+ {
90
+ $this->_activeOptions['tags'][] = 'customer_' . $this->getSession()->getId();
91
+ }
92
+
93
+ $array = $this->load($id);
94
+ if ($array !== false) {
95
+ $data = $array['data'];
96
+ $headers = $array['headers'];
97
+ if ($this->_specificOptions['debug_header']) {
98
+ echo "DEBUG HEADER : This is a cached page $id!";
99
+ }
100
+ if (!headers_sent()) {
101
+ foreach ($headers as $key=>$headerCouple) {
102
+ $name = $headerCouple[0];
103
+ $value = $headerCouple[1];
104
+ header("$name: $value");
105
+ }
106
+ }
107
+ echo $data;
108
+ if ($doNotDie) {
109
+ return true;
110
+ }
111
+ die();
112
+ }
113
+ ob_start(array($this, '_flush'));
114
+ ob_implicit_flush(false);
115
+ return false;
116
+ }
117
+
118
+ private function getSession()
119
+ {
120
+ return Mage::getSingleton('customer/session');
121
+ }
122
+
123
+ public function addTags($tags)
124
+ {
125
+ if(!is_array($tags) && !in_array($tags, $this->_activeOptions['tags']))
126
+ {
127
+ $this->_activeOptions['tags'][] = $tags;
128
+ }
129
+
130
+ if(is_array($tags))
131
+ {
132
+ foreach($tags as $tag)
133
+ {
134
+ if(!in_array($tag, $this->_activeOptions['tags']))
135
+ {
136
+ $this->_activeOptions['tags'][] = $tag;
137
+ }
138
+ }
139
+ }
140
+ }
141
+
142
+ /**
143
+ * callback for output buffering
144
+ * (shouldn't really be called manually)
145
+ *
146
+ * @param string $data Buffered output
147
+ * @return string Data to send to browser
148
+ */
149
+ public function _flush($data)
150
+ {
151
+ if ($this->_cancel) {
152
+ return $data;
153
+ }
154
+ $contentType = null;
155
+ $storedHeaders = array();
156
+ $headersList = headers_list();
157
+ foreach($this->_specificOptions['memorize_headers'] as $key=>$headerName) {
158
+ foreach ($headersList as $headerSent) {
159
+ $tmp = split(':', $headerSent);
160
+ $headerSentName = trim(array_shift($tmp));
161
+ if (strtolower($headerName) == strtolower($headerSentName)) {
162
+ $headerSentValue = trim(implode(':', $tmp));
163
+ $storedHeaders[] = array($headerSentName, $headerSentValue);
164
+ }
165
+ }
166
+ }
167
+ $array = array(
168
+ 'data' => $data,
169
+ 'headers' => $storedHeaders
170
+ );
171
+ $this->save($array, null, $this->_activeOptions['tags'], $this->_activeOptions['specific_lifetime'], $this->_activeOptions['priority']);
172
+ return $data;
173
+ }
174
+
175
+ /**
176
+ * Make an id depending on REQUEST_URI and superglobal arrays (depending on options)
177
+ *
178
+ * @return mixed|false a cache id (string), false if the cache should have not to be used
179
+ */
180
+ protected function _makeId()
181
+ {
182
+ $tmp = $_SERVER['REQUEST_URI'];
183
+ foreach (array('Get', 'Post', 'Session', 'Files', 'Cookie') as $arrayName) {
184
+ $tmp2 = $this->_makePartialId($arrayName, $this->_activeOptions['cache_with_' . strtolower($arrayName) . '_variables'], $this->_activeOptions['make_id_with_' . strtolower($arrayName) . '_variables']);
185
+ $tmp = $tmp . $tmp2;
186
+ }
187
+ return md5($tmp);
188
+ }
189
+
190
+ /**
191
+ * Make a partial id depending on options
192
+ *
193
+ * @param string $arrayName Superglobal array name
194
+ * @param bool $bool1 If true, cache is still on even if there are some variables in the superglobal array
195
+ * @param bool $bool2 If true, we have to use the content of the superglobal array to make a partial id
196
+ * @return mixed|false Partial id (string) or false if the cache should have not to be used
197
+ */
198
+ protected function _makePartialId($arrayName, $bool1, $bool2)
199
+ {
200
+ switch ($arrayName) {
201
+ case 'Get':
202
+ $var = $_GET;
203
+ break;
204
+ case 'Post':
205
+ $var = $_POST;
206
+ break;
207
+ case 'Session':
208
+ if ($this->getSession()->isLoggedIn()) {
209
+ $var = $this->getSession()->getId();
210
+ } else {
211
+ $var = null;
212
+ }
213
+ break;
214
+ case 'Cookie':
215
+ if (isset($_COOKIE) && isset($_COOKIE['PHPSESSID'])) {
216
+ $var = $_COOKIE['PHPSESSID'];
217
+ } else {
218
+ $var = null;
219
+ }
220
+ break;
221
+ case 'Files':
222
+ $var = $_FILES;
223
+ break;
224
+ default:
225
+ return false;
226
+ }
227
+ if ($bool1) {
228
+ if ($bool2) {
229
+ return serialize($var);
230
+ }
231
+ return '';
232
+ }
233
+ if (count($var) > 0) {
234
+ return false;
235
+ }
236
+ return '';
237
+ }
238
+
239
+ public function clean($mode = 'all', $tags = array())
240
+ {
241
+ if($this->getSession()->isLoggedIn())
242
+ {
243
+ $tags[] = $this->getSession()->getId();
244
+ }
245
+
246
+ Mage::log('ici_clean_step_1');
247
+ parent::clean($mode, $tags);
248
+ }
249
+ }
package.xml ADDED
@@ -0,0 +1,19 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?xml version="1.0"?>
2
+ <package>
3
+ <name>Avip_Cache</name>
4
+ <version>0.1</version>
5
+ <stability>stable</stability>
6
+ <license uri="http://www.opensource.org/licenses/gpl-license.php">GPL</license>
7
+ <channel>community</channel>
8
+ <extends/>
9
+ <summary>Fast caching solution for your stores. Based uppon Zend Cache Page.</summary>
10
+ <description>Fast caching solution for your stores.
11
+ Based on Zend Cache Page.</description>
12
+ <notes>Tested only with Magento 1.3, could work with above.</notes>
13
+ <authors><author><name>MOUALI Rabii</name><user>auto-converted</user><email>fincom@free.fr</email></author></authors>
14
+ <date>2010-04-18</date>
15
+ <time>10:36:36</time>
16
+ <contents><target name="magecommunity"><dir name="Avip"><dir name="Cache"><dir name="etc"><file name="config.xml" hash="c5118ff2d1d8d273e8563152381aea46"/></dir><dir name="Model"><dir name="Product"><file name="Observer.php" hash="9b9525c753fd0ea8725091f052b65d73"/></dir></dir></dir></dir></target><target name="magelib"><dir name="Avip"><dir name="Cache"><dir name="Frontend"><file name="Page.php" hash="c1bda4d74d4402a545af048618a7395f"/></dir></dir><file name="Cache.php" hash="4d9abebd7a8b6455a947e5013ca2ffeb"/></dir></target></contents>
17
+ <compatible/>
18
+ <dependencies/>
19
+ </package>