Fixed bug #18016
[Packages/TYPO3.CMS.git] / t3lib / cache / backend / class.t3lib_cache_backend_abstractbackend.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 2009-2011 Ingo Renner <ingo@typo3.org>
6 * All rights reserved
7 *
8 * This script is part of the TYPO3 project. The TYPO3 project is
9 * free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * The GNU General Public License can be found at
15 * http://www.gnu.org/copyleft/gpl.html.
16 *
17 * This script is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * This copyright notice MUST APPEAR in all copies of the script!
23 ***************************************************************/
24
25 /**
26 * An abstract caching backend
27 *
28 * This file is a backport from FLOW3
29 *
30 * @author Ingo Renner <ingo@typo3.org>
31 * @package TYPO3
32 * @subpackage t3lib_cache
33 * @api
34 */
35 abstract class t3lib_cache_backend_AbstractBackend implements t3lib_cache_backend_Backend {
36
37 const DATETIME_EXPIRYTIME_UNLIMITED = '9999-12-31T23:59:59+0000';
38 const UNLIMITED_LIFETIME = 0;
39
40 /**
41 * Reference to the cache which uses this backend
42 *
43 * @var t3lib_cache_frontend_Frontend
44 */
45 protected $cache;
46
47 /**
48 * @var string
49 */
50 protected $cacheIdentifier;
51
52 /**
53 * Default lifetime of a cache entry in seconds
54 *
55 * @var integer
56 */
57 protected $defaultLifetime = 3600;
58
59 /**
60 * Constructs this backend
61 *
62 * @param array $options Configuration options - depends on the actual backend
63 * @author Robert Lemke <robert@typo3.org>
64 */
65 public function __construct(array $options = array()) {
66 if (is_array($options) || $options instanceof ArrayAccess) {
67 foreach ($options as $optionKey => $optionValue) {
68 $methodName = 'set' . ucfirst($optionKey);
69 if (method_exists($this, $methodName)) {
70 $this->$methodName($optionValue);
71 } else {
72 throw new InvalidArgumentException('Invalid cache backend option "' . $optionKey . '" for backend of type "' . get_class($this) . '"', 1235837747);
73 }
74 }
75 }
76 }
77
78 /**
79 * Sets a reference to the cache frontend which uses this backend
80 *
81 * @param t3lib_cache_frontend_Frontend The frontend for this backend
82 * @return void
83 * @author Robert Lemke <robert@typo3.org>
84 */
85 public function setCache(t3lib_cache_frontend_Frontend $cache) {
86 $this->cache = $cache;
87 $this->cacheIdentifier = $this->cache->getIdentifier();
88 }
89
90 /**
91 * Sets the default lifetime for this cache backend
92 *
93 * @param integer $defaultLifetime Default lifetime of this cache backend in seconds. If NULL is specified, the default lifetime is used. "0" means unlimited liftime.
94 * @return void
95 * @author Karsten Dambekalns <karsten@typo3.org>
96 */
97 public function setDefaultLifetime($defaultLifetime) {
98 if (!is_int($defaultLifetime) || $defaultLifetime < 0) {
99 throw new InvalidArgumentException(
100 'The default lifetime must be given as a positive integer.',
101 1233072774
102 );
103 }
104
105 $this->defaultLifetime = $defaultLifetime;
106 }
107
108 /**
109 * Calculates the expiry time by the given lifetime. If no lifetime is
110 * specified, the default lifetime is used.
111 *
112 * @param integer $lifetime The lifetime in seconds
113 * @return \DateTime The expiry time
114 * @author Robert Lemke <robert@typo3.org>
115 * @internal
116 */
117 protected function calculateExpiryTime($lifetime = NULL) {
118 if ($lifetime === self::UNLIMITED_LIFETIME || ($lifetime === NULL && $this->defaultLifetime === self::UNLIMITED_LIFETIME)) {
119 $expiryTime = new DateTime(self::DATETIME_EXPIRYTIME_UNLIMITED, new DateTimeZone('UTC'));
120 } else {
121 if ($lifetime === NULL) {
122 $lifetime = $this->defaultLifetime;
123 }
124 $expiryTime = new DateTime('now +' . $lifetime . ' seconds', new DateTimeZone('UTC'));
125 }
126
127 return $expiryTime;
128 }
129
130
131 }
132
133
134 if (defined('TYPO3_MODE') && isset($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['XCLASS']['t3lib/cache/backend/class.t3lib_cache_backend_abstractbackend.php'])) {
135 include_once($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['XCLASS']['t3lib/cache/backend/class.t3lib_cache_backend_abstractbackend.php']);
136 }
137
138 ?>