[BUGFIX] Caching framework: Reduce code duplication in db backend
authorChristian Kuhn <lolli@schwarzbu.ch>
Thu, 23 Jun 2011 20:41:14 +0000 (22:41 +0200)
committerChristian Kuhn <lolli@schwarzbu.ch>
Thu, 23 Jun 2011 20:45:10 +0000 (22:45 +0200)
Change-Id: I791e7171adedf2737f38adecac52e428ed63da90
Resolves: #27671
Reviewed-on: http://review.typo3.org/2901
Reviewed-by: Christian Kuhn
Tested-by: Christian Kuhn
t3lib/cache/backend/class.t3lib_cache_backend_dbbackend.php

index 35178da..5e6658b 100644 (file)
@@ -136,12 +136,7 @@ class t3lib_cache_backend_DbBackend extends t3lib_cache_backend_AbstractBackend
         * @author Ingo Renner <ingo@typo3.org>
         */
        public function set($entryIdentifier, $data, array $tags = array(), $lifetime = NULL) {
-               if (!$this->cache instanceof t3lib_cache_frontend_Frontend) {
-                       throw new t3lib_cache_Exception(
-                               'No cache frontend has been set via setCache() yet.',
-                               1236518288
-                       );
-               }
+               $this->throwExceptionIfFrontendDoesNotExist();
 
                if (!is_string($data)) {
                        throw new t3lib_cache_exception_InvalidData(
@@ -202,12 +197,7 @@ class t3lib_cache_backend_DbBackend extends t3lib_cache_backend_AbstractBackend
         * @author Ingo Renner <ingo@typo3.org>
         */
        public function get($entryIdentifier) {
-               if (!$this->cache instanceof t3lib_cache_frontend_Frontend) {
-                       throw new t3lib_cache_Exception(
-                               'No cache frontend has been set via setCache() yet.',
-                               1308435810
-                       );
-               }
+               $this->throwExceptionIfFrontendDoesNotExist();
 
                $cacheEntry = FALSE;
 
@@ -237,12 +227,7 @@ class t3lib_cache_backend_DbBackend extends t3lib_cache_backend_AbstractBackend
         * @author Ingo Renner <ingo@typo3.org>
         */
        public function has($entryIdentifier) {
-               if (!$this->cache instanceof t3lib_cache_frontend_Frontend) {
-                       throw new t3lib_cache_Exception(
-                               'No cache frontend has been set via setCache() yet.',
-                               1308435811
-                       );
-               }
+               $this->throwExceptionIfFrontendDoesNotExist();
 
                $hasEntry = FALSE;
 
@@ -268,12 +253,7 @@ class t3lib_cache_backend_DbBackend extends t3lib_cache_backend_AbstractBackend
         * @author Ingo Renner <ingo@typo3.org>
         */
        public function remove($entryIdentifier) {
-               if (!$this->cache instanceof t3lib_cache_frontend_Frontend) {
-                       throw new t3lib_cache_Exception(
-                               'No cache frontend has been set via setCache() yet.',
-                               1308435812
-                       );
-               }
+               $this->throwExceptionIfFrontendDoesNotExist();
 
                $entryRemoved = FALSE;
 
@@ -302,12 +282,7 @@ class t3lib_cache_backend_DbBackend extends t3lib_cache_backend_AbstractBackend
         * @author Ingo Renner <ingo@typo3.org>
         */
        public function findIdentifiersByTag($tag) {
-               if (!$this->cache instanceof t3lib_cache_frontend_Frontend) {
-                       throw new t3lib_cache_Exception(
-                               'No cache frontend has been set via setCache() yet.',
-                               1308435813
-                       );
-               }
+               $this->throwExceptionIfFrontendDoesNotExist();
 
                $cacheEntryIdentifiers = array();
 
@@ -334,12 +309,7 @@ class t3lib_cache_backend_DbBackend extends t3lib_cache_backend_AbstractBackend
         * @author Ingo Renner <ingo@typo3.org>
         */
        public function flush() {
-               if (!$this->cache instanceof t3lib_cache_frontend_Frontend) {
-                       throw new t3lib_cache_Exception(
-                               'No cache frontend has been set via setCache() yet.',
-                               1308435814
-                       );
-               }
+               $this->throwExceptionIfFrontendDoesNotExist();
 
                $GLOBALS['TYPO3_DB']->exec_TRUNCATEquery($this->cacheTable);
                $GLOBALS['TYPO3_DB']->exec_TRUNCATEquery($this->tagsTable);
@@ -356,12 +326,7 @@ class t3lib_cache_backend_DbBackend extends t3lib_cache_backend_AbstractBackend
         * @return void
         */
        public function flushByTag($tag) {
-               if (!$this->cache instanceof t3lib_cache_frontend_Frontend) {
-                       throw new t3lib_cache_Exception(
-                               'No cache frontend has been set via setCache() yet.',
-                               1308435815
-                       );
-               }
+               $this->throwExceptionIfFrontendDoesNotExist();
 
                $tagsTableWhereClause = $this->tagsTable . '.tag = ' . $GLOBALS['TYPO3_DB']->fullQuoteStr($tag, $this->tagsTable);
 
@@ -380,12 +345,7 @@ class t3lib_cache_backend_DbBackend extends t3lib_cache_backend_AbstractBackend
         * @author Ingo Renner <ingo@typo3.org>
         */
        public function collectGarbage() {
-               if (!$this->cache instanceof t3lib_cache_frontend_Frontend) {
-                       throw new t3lib_cache_Exception(
-                               'No cache frontend has been set via setCache() yet.',
-                               1308435816
-                       );
-               }
+               $this->throwExceptionIfFrontendDoesNotExist();
 
                        // Get identifiers of expired cache entries
                $tagsEntryIdentifierRowsResource = $GLOBALS['TYPO3_DB']->exec_SELECTquery(
@@ -438,12 +398,7 @@ class t3lib_cache_backend_DbBackend extends t3lib_cache_backend_AbstractBackend
         * @author Ingo Renner <ingo@typo3.org>
         */
        public function getCacheTable() {
-               if (!$this->cache instanceof t3lib_cache_frontend_Frontend) {
-                       throw new t3lib_cache_Exception(
-                               'No cache frontend has been set via setCache() yet.',
-                               1308435817
-                       );
-               }
+               $this->throwExceptionIfFrontendDoesNotExist();
 
                return $this->cacheTable;
        }
@@ -466,12 +421,7 @@ class t3lib_cache_backend_DbBackend extends t3lib_cache_backend_AbstractBackend
         * @return      string          Name of the table storing tags
         */
        public function getTagsTable() {
-               if (!$this->cache instanceof t3lib_cache_frontend_Frontend) {
-                       throw new t3lib_cache_Exception(
-                               'No cache frontend has been set via setCache() yet.',
-                               1308435818
-                       );
-               }
+               $this->throwExceptionIfFrontendDoesNotExist();
 
                return $this->tagsTable;
        }
@@ -499,6 +449,21 @@ class t3lib_cache_backend_DbBackend extends t3lib_cache_backend_AbstractBackend
        }
 
        /**
+        * Check if required frontend instance exists
+        *
+        * @throws t3lib_cache_Exception If there is no frontend instance in $this->cache
+        * @return void
+        */
+       protected function throwExceptionIfFrontendDoesNotExist() {
+               if (!$this->cache instanceof t3lib_cache_frontend_Frontend) {
+                       throw new t3lib_cache_Exception(
+                               'No cache frontend has been set via setCache() yet.',
+                               1236518288
+                       );
+               }
+       }
+
+       /**
         * Create data table of cache
         *
         * @return void