@@ -77,24 +77,65 @@ public function getConditionsInstance()
77
77
return Mage::getModel ('smile_virtualcategories/rule_condition_combine ' );
78
78
}
79
79
80
+ /**
81
+ * @param string $cacheKey The cache key
82
+ * @param array $data The data to cache
83
+ *
84
+ * @return $this
85
+ */
86
+ public function addToStaticCacheInstance ($ cacheKey , $ data )
87
+ {
88
+ $ this ->_queryCache [$ cacheKey ] = $ data ;
89
+
90
+ return $ this ;
91
+ }
92
+
93
+ /**
94
+ * @param string $cacheKey The cache key
95
+ *
96
+ * @return bool|array
97
+ */
98
+ public function getFromStaticCacheInstance ($ cacheKey )
99
+ {
100
+ $ data = false ;
101
+
102
+ if (isset ($ this ->_queryCache [$ cacheKey ]) && ($ this ->_queryCache [$ cacheKey ] !== null )) {
103
+ $ data = $ this ->_queryCache [$ cacheKey ];
104
+ }
105
+
106
+ return $ data ;
107
+ }
108
+
80
109
/**
81
110
* Local caching of queries. Used when a category query is retrieved several times during the same request.
82
111
*
83
- * @param int $categoryId Id of the category.
112
+ * @param int $categoryId Id of the category.
113
+ * @param array $excludedIds Excluded category ids, if any.
84
114
*
85
115
* @return NULL|string
86
116
*/
87
- public function getQueryFromCache ($ categoryId )
117
+ public function getQueryFromCache ($ categoryId, $ excludedIds = array () )
88
118
{
119
+ /** @var Smile_VirtualCategories_Model_Rule $cacheInstance */
89
120
$ cacheInstance = Mage::getSingleton ('smile_virtualcategories/rule ' );
90
- $ data = false ;
91
-
92
- if (isset ($ cacheInstance ->_queryCache [$ categoryId ])) {
93
- $ data = $ cacheInstance ->_queryCache [$ categoryId ];
121
+ $ cacheKey = $ this ->forgeCacheKey ($ categoryId , $ excludedIds );
122
+ $ data = false ;
123
+
124
+ Mage::log ("CACHE READ : " . $ cacheKey , null , "rule-cache.log " );
125
+ if ($ cacheInstance ->getFromStaticCacheInstance ($ cacheKey )) {
126
+ $ data = $ cacheInstance ->getFromStaticCacheInstance ($ cacheKey );
127
+ $ this ->cacheQuery ($ categoryId , $ data , $ excludedIds );
128
+ Mage::log ("CACHE FOUND [STATIC] " , null , "rule-cache.log " );
94
129
}
95
130
96
- if ($ data === false && $ cacheData = Mage::app ()->loadCache (self :: CACHE_KEY_PREFIX . ' _ ' . $ categoryId )) {
131
+ if ($ data === false && $ cacheData = Mage::app ()->loadCache ($ cacheKey )) {
97
132
$ data = unserialize ($ cacheData );
133
+ $ cacheInstance ->addToStaticCacheInstance ($ cacheKey , $ data );
134
+ Mage::log ("CACHE FOUND [BACKEND] " , null , "rule-cache.log " );
135
+ }
136
+
137
+ if ($ data == false ) {
138
+ Mage::log ("CACHE NOT FOUND " , null , "rule-cache.log " );
98
139
}
99
140
100
141
return $ data ;
@@ -103,24 +144,27 @@ public function getQueryFromCache($categoryId)
103
144
/**
104
145
* Store category query into the local cache.
105
146
*
106
- * @param int $categoryId Id of the category.
107
- * @param string $data Data to cache [query, used_categories].
147
+ * @param int $categoryId Id of the category.
148
+ * @param string $data Data to cache [query, used_categories].
149
+ * @param array $excludedIds Excluded category ids, if any.
108
150
*
109
151
* @return Smile_VirtualCategories_Model_Rule
110
152
*/
111
- public function cacheQuery ($ categoryId , $ data )
153
+ public function cacheQuery ($ categoryId , $ data, $ excludedIds = array () )
112
154
{
155
+ $ cacheKey = $ this ->forgeCacheKey ($ categoryId , $ excludedIds );
156
+
157
+ /** @var Smile_VirtualCategories_Model_Rule $cacheInstance */
113
158
$ cacheInstance = Mage::getSingleton ('smile_virtualcategories/rule ' );
114
- $ cacheInstance ->_queryCache [ $ categoryId ] = $ data ;
159
+ $ cacheInstance ->addToStaticCacheInstance ( $ cacheKey , $ data) ;
115
160
116
161
$ cacheTags = array ();
117
162
foreach ($ data [1 ] as $ usedCategoryId ) {
118
163
$ cacheTags [] = Mage_Catalog_Model_Category::CACHE_TAG . '_ ' . $ usedCategoryId ;
119
164
}
120
165
121
- $ cacheId = self ::CACHE_KEY_PREFIX . '_ ' .$ categoryId ;
122
-
123
- Mage::app ()->saveCache (serialize ($ data ), $ cacheId , $ cacheTags , Mage_Core_Model_Cache::DEFAULT_LIFETIME );
166
+ Mage::log ("CACHE WRITE : " . $ cacheKey , null , "rule-cache.log " );
167
+ Mage::app ()->saveCache (serialize ($ data ), $ cacheKey , $ cacheTags , Mage_Core_Model_Cache::DEFAULT_LIFETIME );
124
168
125
169
return $ this ;
126
170
}
@@ -136,12 +180,13 @@ public function getSearchQuery($excludedCategories = array())
136
180
{
137
181
$ category = $ this ->getCategory ();
138
182
139
- $ cacheData = $ this ->getQueryFromCache ($ category ->getId () . $ category ->getStoreId ());
183
+ $ cacheData = $ this ->getQueryFromCache ($ category ->getId () . " _ " . $ category ->getStoreId (), $ excludedCategories );
140
184
$ query = '' ;
141
185
142
- if (!$ cacheData || (! empty ( $ excludedCategories )) ) {
143
- $ this -> _usedCategories = array ();
186
+ if (!$ cacheData ) {
187
+
144
188
$ this ->addUsedCategoryIds ($ category ->getId ());
189
+
145
190
if ($ category ->getIsVirtual ()) {
146
191
$ this ->getConditions ()->setRule ($ this );
147
192
$ query = $ this ->getConditions ()->getSearchQuery ($ excludedCategories );
@@ -151,18 +196,12 @@ public function getSearchQuery($excludedCategories = array())
151
196
$ query = implode (' OR ' , array_merge (array ($ query ), $ childrenQueries ));
152
197
}
153
198
154
- // Append the root category query string if needed
155
- if ($ rootCategory = Mage::helper ('smile_virtualcategories ' )->getVirtualRootCategory ($ category )) {
156
- $ rootCategoryQuery = $ this ->_getVirtualRule ($ rootCategory )->getSearchQuery ($ category ->getId ());
157
- $ query = implode (' AND ' , array_filter (array_merge (array ($ query ), array ("( " . $ rootCategoryQuery . ") " ))));
158
- }
199
+ $ this ->cacheQuery ($ category ->getId () . "_ " . $ category ->getStoreId (), array ($ query , $ this ->_usedCategories ), $ excludedCategories );
159
200
160
- if (empty ($ excludedCategories )) {
161
- $ this ->cacheQuery ($ category ->getId () . $ category ->getStoreId (), array ($ query , $ this ->_usedCategories ));
162
- }
163
201
} else {
164
202
list ($ query , $ this ->_usedCategories ) = $ cacheData ;
165
203
}
204
+
166
205
return $ query ;
167
206
}
168
207
@@ -188,7 +227,7 @@ public function getChildrenCategoryQueries($excludedCategories = array(), $onlyV
188
227
->setStore ($ rootCategory ->getStoreId ())
189
228
->addIsActiveFilter ()
190
229
->addFieldToFilter ('path ' , array ('like ' => $ rootCategory ->getPath () . '/% ' ))
191
- ->addAttributeToSelect ('virtual_category ' );
230
+ ->addAttributeToSelect (array ( 'virtual_category ' , ' name ' ) );
192
231
193
232
if (!empty ($ excludedCategories )) {
194
233
$ categories ->addFieldToFilter ('entity_id ' , array ('nin ' => $ excludedCategories ));
@@ -200,17 +239,12 @@ public function getChildrenCategoryQueries($excludedCategories = array(), $onlyV
200
239
201
240
foreach ($ categories as $ currentCategory ) {
202
241
if ($ currentCategory ->getIsVirtual () || ($ onlyVirtual == false )) {
242
+
203
243
$ virtualRule = $ currentCategory ->getVirtualRule ();
204
244
$ virtualRule ->setStoreId ($ this ->getCategory ()->getStoreId ());
205
245
$ query = $ virtualRule ->getSearchQuery ($ excludedCategories );
206
- if ($ query ) {
207
-
208
- // Append the root category query string if needed
209
- if ($ rootCategory = Mage::helper ('smile_virtualcategories ' )->getVirtualRootCategory ($ currentCategory )) {
210
- $ rootCategoryQuery = $ this ->_getVirtualRule ($ rootCategory )->getSearchQuery ($ currentCategory ->getId ());
211
- $ query = implode (' AND ' , array_filter (array_merge (array ($ query ), array ("( " . $ rootCategoryQuery . ") " ))));
212
- }
213
246
247
+ if ($ query ) {
214
248
$ queries [$ currentCategory ->getId ()] = '( ' . $ query . ') ' ;
215
249
$ this ->addUsedCategoryIds ($ virtualRule ->getUsedCategoryIds ());
216
250
}
@@ -291,4 +325,32 @@ protected function _getVirtualRule($category)
291
325
{
292
326
return Mage::helper ('smile_virtualcategories ' )->getVirtualRule ($ category );
293
327
}
328
+
329
+ /**
330
+ * Retrieve proper cache key for a given string
331
+ *
332
+ * @param string $identifier The object identifier to forge cache key for
333
+ * @param array $additional An additional array of object ids to add to the cache key.
334
+ *
335
+ * @return string
336
+ */
337
+ private function forgeCacheKey ($ identifier , $ additional = array ())
338
+ {
339
+ $ cacheKey = (string ) $ identifier ;
340
+ $ cacheKey = self ::CACHE_KEY_PREFIX . "_ " . $ cacheKey ;
341
+
342
+ if (!is_array ($ additional )) {
343
+ $ additional = array ($ additional );
344
+ }
345
+
346
+ $ additional = array_filter ($ additional );
347
+
348
+ if (!empty ($ additional )) {
349
+ $ additional = array_unique ($ additional );
350
+ sort ($ additional );
351
+ $ cacheKey .= "_ " . implode ("| " , $ additional );
352
+ }
353
+
354
+ return $ cacheKey ;
355
+ }
294
356
}
0 commit comments