diff --git a/library/think/cache/driver/File.php b/library/think/cache/driver/File.php index 2d2fb5ca..04d8c4a6 100644 --- a/library/think/cache/driver/File.php +++ b/library/think/cache/driver/File.php @@ -127,9 +127,9 @@ class File extends Driver /** * 写入缓存 * @access public - * @param string $name 缓存变量名 - * @param mixed $value 存储数据 - * @param int $expire 有效时间 0为永久 + * @param string $name 缓存变量名 + * @param mixed $value 存储数据 + * @param integer|DateTime $expire 有效时间(秒) * @return boolean */ public function set($name, $value, $expire = null) @@ -137,6 +137,9 @@ class File extends Driver if (is_null($expire)) { $expire = $this->options['expire']; } + if ($expire instanceof \DateTime) { + $expire = $expire->getTimestamp() - time(); + } $filename = $this->getCacheKey($name); if ($this->tag && !is_file($filename)) { $first = true; diff --git a/library/think/cache/driver/Lite.php b/library/think/cache/driver/Lite.php index eeb96a19..ca69489f 100644 --- a/library/think/cache/driver/Lite.php +++ b/library/think/cache/driver/Lite.php @@ -77,7 +77,7 @@ class Lite extends Driver if (is_file($filename)) { // 判断是否过期 $mtime = filemtime($filename); - if ($mtime < $_SERVER['REQUEST_TIME']) { + if ($mtime < time()) { // 清除已经过期的文件 unlink($filename); return $default; @@ -91,9 +91,9 @@ class Lite extends Driver /** * 写入缓存 * @access public - * @param string $name 缓存变量名 - * @param mixed $value 存储数据 - * @param int $expire 有效时间 0为永久 + * @param string $name 缓存变量名 + * @param mixed $value 存储数据 + * @param integer|DateTime $expire 有效时间(秒) * @return bool */ public function set($name, $value, $expire = null) @@ -101,9 +101,11 @@ class Lite extends Driver if (is_null($expire)) { $expire = $this->options['expire']; } - // 模拟永久 - if (0 === $expire) { - $expire = 10 * 365 * 24 * 3600; + if ($expire instanceof \DateTime) { + $expire = $expire->getTimestamp(); + } else { + $expire = 0 === $expire ? 10 * 365 * 24 * 3600 : $expire; + $expire = time() + $expire; } $filename = $this->getCacheKey($name); if ($this->tag && !is_file($filename)) { @@ -113,7 +115,7 @@ class Lite extends Driver // 通过设置修改时间实现有效期 if ($ret) { isset($first) && $this->setTagItem($filename); - touch($filename, $_SERVER['REQUEST_TIME'] + $expire); + touch($filename, $expire); } return $ret; } diff --git a/library/think/cache/driver/Memcache.php b/library/think/cache/driver/Memcache.php index e464f38c..816e5a93 100644 --- a/library/think/cache/driver/Memcache.php +++ b/library/think/cache/driver/Memcache.php @@ -82,9 +82,9 @@ class Memcache extends Driver /** * 写入缓存 * @access public - * @param string $name 缓存变量名 - * @param mixed $value 存储数据 - * @param integer $expire 有效时间(秒) + * @param string $name 缓存变量名 + * @param mixed $value 存储数据 + * @param integer|DateTime $expire 有效时间(秒) * @return bool */ public function set($name, $value, $expire = null) @@ -92,6 +92,9 @@ class Memcache extends Driver if (is_null($expire)) { $expire = $this->options['expire']; } + if ($expire instanceof \DateTime) { + $expire = $expire->getTimestamp() - time(); + } if ($this->tag && !$this->has($name)) { $first = true; } diff --git a/library/think/cache/driver/Memcached.php b/library/think/cache/driver/Memcached.php index 4a9d898c..6232fd80 100644 --- a/library/think/cache/driver/Memcached.php +++ b/library/think/cache/driver/Memcached.php @@ -93,9 +93,9 @@ class Memcached extends Driver /** * 写入缓存 * @access public - * @param string $name 缓存变量名 - * @param mixed $value 存储数据 - * @param integer $expire 有效时间(秒) + * @param string $name 缓存变量名 + * @param mixed $value 存储数据 + * @param integer|DateTime $expire 有效时间(秒) * @return bool */ public function set($name, $value, $expire = null) @@ -103,6 +103,9 @@ class Memcached extends Driver if (is_null($expire)) { $expire = $this->options['expire']; } + if ($expire instanceof \DateTime) { + $expire = $expire->getTimestamp() - time(); + } if ($this->tag && !$this->has($name)) { $first = true; } diff --git a/library/think/cache/driver/Redis.php b/library/think/cache/driver/Redis.php index 4f618785..d2bba3ed 100644 --- a/library/think/cache/driver/Redis.php +++ b/library/think/cache/driver/Redis.php @@ -91,9 +91,9 @@ class Redis extends Driver /** * 写入缓存 * @access public - * @param string $name 缓存变量名 - * @param mixed $value 存储数据 - * @param integer $expire 有效时间(秒) + * @param string $name 缓存变量名 + * @param mixed $value 存储数据 + * @param integer|DateTime $expire 有效时间(秒) * @return boolean */ public function set($name, $value, $expire = null) @@ -101,6 +101,9 @@ class Redis extends Driver if (is_null($expire)) { $expire = $this->options['expire']; } + if ($expire instanceof \DateTime) { + $expire = $expire->getTimestamp() - time(); + } if ($this->tag && !$this->has($name)) { $first = true; } diff --git a/library/think/cache/driver/Sqlite.php b/library/think/cache/driver/Sqlite.php index 305fd9e8..74f0b66f 100644 --- a/library/think/cache/driver/Sqlite.php +++ b/library/think/cache/driver/Sqlite.php @@ -96,9 +96,9 @@ class Sqlite extends Driver /** * 写入缓存 * @access public - * @param string $name 缓存变量名 - * @param mixed $value 存储数据 - * @param integer $expire 有效时间(秒) + * @param string $name 缓存变量名 + * @param mixed $value 存储数据 + * @param integer|DateTime $expire 有效时间(秒) * @return boolean */ public function set($name, $value, $expire = null) @@ -108,7 +108,11 @@ class Sqlite extends Driver if (is_null($expire)) { $expire = $this->options['expire']; } - $expire = (0 == $expire) ? 0 : ($_SERVER['REQUEST_TIME'] + $expire); //缓存有效期为0表示永久缓存 + if ($expire instanceof \DateTime) { + $expire = $expire->getTimestamp(); + } else { + $expire = (0 == $expire) ? 0 : (time() + $expire); //缓存有效期为0表示永久缓存 + } if (function_exists('gzcompress')) { //数据压缩 $value = gzcompress($value, 3); diff --git a/library/think/cache/driver/Wincache.php b/library/think/cache/driver/Wincache.php index 3076fc14..aa56f6a7 100644 --- a/library/think/cache/driver/Wincache.php +++ b/library/think/cache/driver/Wincache.php @@ -68,9 +68,9 @@ class Wincache extends Driver /** * 写入缓存 * @access public - * @param string $name 缓存变量名 - * @param mixed $value 存储数据 - * @param integer $expire 有效时间(秒) + * @param string $name 缓存变量名 + * @param mixed $value 存储数据 + * @param integer|DateTime $expire 有效时间(秒) * @return boolean */ public function set($name, $value, $expire = null) @@ -78,6 +78,9 @@ class Wincache extends Driver if (is_null($expire)) { $expire = $this->options['expire']; } + if ($expire instanceof \DateTime) { + $expire = $expire->getTimestamp() - time(); + } $key = $this->getCacheKey($name); if ($this->tag && !$this->has($name)) { $first = true; diff --git a/library/think/cache/driver/Xcache.php b/library/think/cache/driver/Xcache.php index 2a0e07ad..c0b3a64c 100644 --- a/library/think/cache/driver/Xcache.php +++ b/library/think/cache/driver/Xcache.php @@ -68,9 +68,9 @@ class Xcache extends Driver /** * 写入缓存 * @access public - * @param string $name 缓存变量名 - * @param mixed $value 存储数据 - * @param integer $expire 有效时间(秒) + * @param string $name 缓存变量名 + * @param mixed $value 存储数据 + * @param integer|DateTime $expire 有效时间(秒) * @return boolean */ public function set($name, $value, $expire = null) @@ -78,6 +78,9 @@ class Xcache extends Driver if (is_null($expire)) { $expire = $this->options['expire']; } + if ($expire instanceof \DateTime) { + $expire = $expire->getTimestamp() - time(); + } if ($this->tag && !$this->has($name)) { $first = true; }