Skip to content

Instantly share code, notes, and snippets.

@fbrnc
Created May 13, 2011 21:02
Show Gist options
  • Save fbrnc/971318 to your computer and use it in GitHub Desktop.
Save fbrnc/971318 to your computer and use it in GitHub Desktop.
Patch for Magento's database backend
Index: lib/Varien/Cache/Backend/Database.php
===================================================================
--- lib/Varien/Cache/Backend/Database.php (revision 63515)
+++ lib/Varien/Cache/Backend/Database.php (working copy)
@@ -224,6 +224,7 @@
if ($this->_options['store_data']) {
$adapter = $this->_getAdapter();
$result = $adapter->delete($this->_getDataTable(), array('id=?'=>$id));
+ $tagResult = $adapter->delete($this->_getTagsTable(), array('cache_id=?' => $id));
return $result;
} else {
return false;
@@ -261,10 +262,7 @@
break;
case Zend_Cache::CLEANING_MODE_OLD:
if ($this->_options['store_data']) {
- $result = $adapter->delete($this->_getDataTable(), array(
- 'expire_time>' => 0,
- 'expire_time<=' => time()
- ));
+ $result = $this->_cleanOld();
} else {
$result = true;
}
@@ -281,7 +279,46 @@
return $result;
}
+
+ /**
+ * Clean old cache records and related cache tag records
+ *
+ * @return boolean true if no problem
+ */
+ protected function _cleanOld()
+ {
+ $time = time();
+ $adapter = $this->_getAdapter();
+
+ // get all expired cache records
+ $select = $adapter->select()
+ ->from($this->_getDataTable(), 'id')
+ ->where('expire_time>?', 0)
+ ->where('expire_time<=?', $time);
+ $stm = $adapter->query($select);
+ // delete attached cache tags
+ $ids = array();
+ while (($row = $stm->fetch()) == true) {
+ $ids[] = $row['id'];
+ if (count($ids) > 100) {
+ $adapter->delete($this->_getTagsTable(), array('cache_id IN (?)' => $ids));
+ $ids = array();
+ }
+ }
+ if (!empty($ids)) {
+ $adapter->delete($this->_getTagsTable(), array('cache_id IN (?)' => $ids));
+ }
+
+ // delete actual cache records
+ $result = $adapter->delete($this->_getDataTable(), array(
+ 'expire_time>?' => 0,
+ 'expire_time<=?' => $time
+ ));
+
+ return $result;
+ }
+
/**
* Return an array of stored cache ids
*
@@ -416,7 +453,7 @@
return $this->_getAdapter()->update(
$this->_getDataTable(),
array('expire_time'=>new Zend_Db_Expr('expire_time+'.$extraLifetime)),
- array('id=?'=>$id, 'expire_time = 0 OR expire_time>'=>time())
+ array('id=?'=>$id, 'expire_time = 0 OR expire_time>?'=>time())
);
} else {
return true;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment