Updated the caching framework to the according FLOW3 revision 2616
[Packages/TYPO3.CMS.git] / t3lib / cache / backend / class.t3lib_cache_backend_abstractbackend.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 2009 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 * @version $Id$
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 * Default lifetime of a cache entry in seconds
49 *
50 * @var integer
51 */
52 protected $defaultLifetime = 3600;
53
54 /**
55 * Constructs this backend
56 *
57 * @param mixed Configuration options - depends on the actual backend
58 * @author Robert Lemke <robert@typo3.org>
59 */
60 public function __construct(array $options = array()) {
61 if (is_array($options) || $options instanceof ArrayAccess) {
62 foreach ($options as $optionKey => $optionValue) {
63 $methodName = 'set' . ucfirst($optionKey);
64 if (method_exists($this, $methodName)) {
65 $this->$methodName($optionValue);
66 } else {
67 throw new InvalidArgumentException('Invalid cache backend option "' . $optionKey . '" for backend of type "' . get_class($this) . '"', 1235837747);
68 }
69 }
70 }
71 }
72
73 /**
74 * Sets a reference to the cache frontend which uses this backend
75 *
76 * @param t3lib_cache_frontend_Frontend The frontend for this backend
77 * @return void
78 * @author Robert Lemke <robert@typo3.org>
79 */
80 public function setCache(t3lib_cache_frontend_Frontend $cache) {
81 $this->cache = $cache;
82 }
83
84 /**
85 * Sets the default lifetime for this cache backend
86 *
87 * @param integer $defaultLifeTime Default lifetime of this cache backend in seconds. If NULL is specified, the default lifetime is used. "0" means unlimited liftime.
88 * @return void
89 * @author Karsten Dambekalns <karsten@typo3.org>
90 */
91 public function setDefaultLifetime($defaultLifetime) {
92 if (!is_int($defaultLifetime) || $defaultLifetime < 0) {
93 throw new InvalidArgumentException(
94 'The default lifetime must be given as a positive integer.',
95 1233072774
96 );
97 }
98
99 $this->defaultLifetime = $defaultLifetime;
100 }
101
102 /**
103 * Calculates the expiry time by the given lifetime. If no lifetime is
104 * specified, the default lifetime is used.
105 *
106 * @param integer $lifetime The lifetime in seconds
107 * @return \DateTime The expiry time
108 * @author Robert Lemke <robert@typo3.org>
109 * @internal
110 */
111 protected function calculateExpiryTime($lifetime = NULL) {
112 if ($lifetime === self::UNLIMITED_LIFETIME || ($lifetime === NULL && $this->defaultLifetime === self::UNLIMITED_LIFETIME)) {
113 $expiryTime = new DateTime(self::DATETIME_EXPIRYTIME_UNLIMITED, new DateTimeZone('UTC'));
114 } else {
115 if ($lifetime === NULL) {
116 $lifetime = $this->defaultLifetime;
117 }
118 $expiryTime = new DateTime('now +' . $lifetime . ' seconds', new DateTimeZone('UTC'));
119 }
120
121 return $expiryTime;
122 }
123
124
125 }
126
127
128 if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['t3lib/cache/backend/class.t3lib_cache_backend_abstractbackend.php']) {
129 include_once($TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['t3lib/cache/backend/class.t3lib_cache_backend_abstractbackend.php']);
130 }
131
132 ?>