From 8c1e601bfd3be140a1cf5d86438c874627fe73f2 Mon Sep 17 00:00:00 2001 From: Dennis Eichhorn Date: Mon, 31 Oct 2016 09:33:08 +0100 Subject: [PATCH 1/4] Fix file cache implementation --- DataStorage/Cache/CacheFactory.php | 3 ++- DataStorage/Cache/CachePool.php | 2 +- DataStorage/Cache/FileCache.php | 26 ++++++++++++++++---------- System/File/Local/Directory.php | 13 ++++++++++++- System/File/Local/File.php | 2 +- 5 files changed, 32 insertions(+), 14 deletions(-) diff --git a/DataStorage/Cache/CacheFactory.php b/DataStorage/Cache/CacheFactory.php index 6760ba8fb..eb126955e 100644 --- a/DataStorage/Cache/CacheFactory.php +++ b/DataStorage/Cache/CacheFactory.php @@ -13,7 +13,8 @@ * @version 1.0.0 * @link http://orange-management.com */ -namespace phpOMS\DataStorage\Database\Connection; +namespace phpOMS\DataStorage\Cache; + use phpOMS\DataStorage\Cache\CacheInterface; use phpOMS\DataStorage\Cache\FileCache; diff --git a/DataStorage/Cache/CachePool.php b/DataStorage/Cache/CachePool.php index 915432811..3f578bfef 100644 --- a/DataStorage/Cache/CachePool.php +++ b/DataStorage/Cache/CachePool.php @@ -17,7 +17,7 @@ namespace phpOMS\DataStorage\Cache; use phpOMS\Config\OptionsInterface; use phpOMS\Config\OptionsTrait; -use phpOMS\DataStorage\Database\Connection\CacheFactory; +use phpOMS\DataStorage\Cache\CacheFactory; /** diff --git a/DataStorage/Cache/FileCache.php b/DataStorage/Cache/FileCache.php index 582d20115..f1afed11e 100644 --- a/DataStorage/Cache/FileCache.php +++ b/DataStorage/Cache/FileCache.php @@ -260,9 +260,9 @@ class FileCache implements CacheInterface private function getExpire(string $raw) : int { $expireStart = strpos($raw, self::DELIM); - $expireEnd = strpos($raw, self::DELIM, $expireStart); + $expireEnd = strpos($raw, self::DELIM, $expireStart+1); - return (int) substr($raw, $expireStart, $expireEnd); + return (int) substr($raw, $expireStart+1, $expireEnd - ($expireStart+1)); } /** @@ -274,8 +274,14 @@ class FileCache implements CacheInterface return null; } - $name = Directory::sanitize($key); - $created = Directory::created($path = $this->cachePath . '/' . $name)->getTimestamp(); + $name = File::sanitize($key); + $path = $this->cachePath . '/' . $name; + + if(!file_exists($path)) { + return null; + } + + $created = Directory::created($path)->getTimestamp(); $now = time(); if ($expire >= 0 && $created + $expire > $now) { @@ -286,10 +292,10 @@ class FileCache implements CacheInterface $type = $raw[0]; $expireStart = strpos($raw, self::DELIM); - $expireEnd = strpos($raw, self::DELIM, $expireStart); - $cacheExpire = substr($raw, $expireStart, $expireEnd); + $expireEnd = strpos($raw, self::DELIM, $expireStart+1); + $cacheExpire = substr($raw, $expireStart+1, $expireEnd - ($expireStart+1)); - if ($cacheExpire >= 0 && $created + $cacheExpire > $now) { + if ($cacheExpire >= 0 && $created + $cacheExpire < $now) { $this->delete($key); return null; @@ -316,7 +322,7 @@ class FileCache implements CacheInterface case CacheType::_SERIALIZABLE: case CacheType::_JSONSERIALIZABLE: $namespaceStart = strpos($raw, self::DELIM, $expireEnd); - $namespaceEnd = strpos($raw, self::DELIM, $namespaceStart); + $namespaceEnd = strpos($raw, self::DELIM, $namespaceStart+1); $namespace = substr($raw, $namespaceStart, $namespaceEnd); $value = $namespace::unserialize(substr($raw, $namespaceEnd + 1)); @@ -349,8 +355,8 @@ class FileCache implements CacheInterface $now = time(); $raw = file_get_contents($path); $expireStart = strpos($raw, self::DELIM); - $expireEnd = strpos($raw, self::DELIM, $expireStart); - $cacheExpire = substr($raw, $expireStart, $expireEnd); + $expireEnd = strpos($raw, self::DELIM, $expireStart+1); + $cacheExpire = substr($raw, $expireStart+1, $expireEnd - ($expireStart+1)); if ($cacheExpire >= 0 && $created + $cacheExpire > $now) { unlink($path); diff --git a/System/File/Local/Directory.php b/System/File/Local/Directory.php index 140094950..c39db39ca 100644 --- a/System/File/Local/Directory.php +++ b/System/File/Local/Directory.php @@ -192,10 +192,21 @@ class Directory extends FileAbstract implements DirectoryInterface /** * {@inheritdoc} + * todo: move to fileAbastract since it should be the same for file and directory? */ public static function created(string $path) : \DateTime { - // TODO: Implement created() method. + if(!file_exists($path)) { + $created = new \DateTime(); + $created->setTimestamp(0); + + return $created; + } + + $created = new \DateTime('now'); + $created->setTimestamp(filemtime($path)); + + return $created; } /** diff --git a/System/File/Local/File.php b/System/File/Local/File.php index 38a3737bf..a4aaa6088 100644 --- a/System/File/Local/File.php +++ b/System/File/Local/File.php @@ -145,7 +145,7 @@ class File extends FileAbstract implements FileInterface */ public static function sanitize(string $path) : string { - return preg_replace('[^\w\s\d\.\-_~,;:\[\]\(\]]', '', $path); + return preg_replace('/[^\w\s\d\.\-_~,;\[\]\(\]]/', '', $path); } /** From dd04acdb59bbbbddf1943e78d21e6848784a9424 Mon Sep 17 00:00:00 2001 From: Dennis Eichhorn Date: Mon, 31 Oct 2016 10:14:25 +0100 Subject: [PATCH 2/4] Fixes #80 --- DataStorage/Cache/FileCache.php | 10 +++++----- System/File/ContainerInterface.php | 3 ++- System/File/Local/Directory.php | 4 ++-- System/File/Local/File.php | 4 ++-- 4 files changed, 11 insertions(+), 10 deletions(-) diff --git a/DataStorage/Cache/FileCache.php b/DataStorage/Cache/FileCache.php index f1afed11e..67efe4ac1 100644 --- a/DataStorage/Cache/FileCache.php +++ b/DataStorage/Cache/FileCache.php @@ -142,7 +142,7 @@ class FileCache implements CacheInterface return false; } - $path = File::sanitize($key); + $path = File::sanitize($key, '~'); file_put_contents($this->cachePath . '/' . $path, $this->build($value, $expire)); @@ -158,7 +158,7 @@ class FileCache implements CacheInterface return false; } - $path = File::sanitize($key); + $path = File::sanitize($key, '~'); if (!file_exists($this->cachePath . '/' . $path)) { file_put_contents($this->cachePath . '/' . $path, $this->build($value, $expire)); @@ -274,7 +274,7 @@ class FileCache implements CacheInterface return null; } - $name = File::sanitize($key); + $name = File::sanitize($key, '~'); $path = $this->cachePath . '/' . $name; if(!file_exists($path)) { @@ -341,7 +341,7 @@ class FileCache implements CacheInterface return false; } - $name = File::sanitize($key); + $name = File::sanitize($key, '~'); $path = $this->cachePath . '/' . $name; if ($expire < 0 && file_exists($path)) { @@ -404,7 +404,7 @@ class FileCache implements CacheInterface return false; } - $path = File::sanitize($key); + $path = File::sanitize($key, '~'); if (file_exists($this->cachePath . '/' . $path)) { file_put_contents($this->cachePath . '/' . $path, $this->build($value, $expire)); diff --git a/System/File/ContainerInterface.php b/System/File/ContainerInterface.php index 640665651..a95e9da62 100644 --- a/System/File/ContainerInterface.php +++ b/System/File/ContainerInterface.php @@ -197,13 +197,14 @@ interface ContainerInterface * Make name/path operating system safe. * * @param string $path Path of the resource + * @param string $replace Replace invalid chars with * * @return string * * @since 1.0.0 * @author Dennis Eichhorn */ - public static function sanitize(string $path) : string; + public static function sanitize(string $path, string $replace = '') : string; /** * Get amount of sub-resources. diff --git a/System/File/Local/Directory.php b/System/File/Local/Directory.php index c39db39ca..65d116a8d 100644 --- a/System/File/Local/Directory.php +++ b/System/File/Local/Directory.php @@ -260,9 +260,9 @@ class Directory extends FileAbstract implements DirectoryInterface /** * {@inheritdoc} */ - public static function sanitize(string $path) : string + public static function sanitize(string $path, string $replace = '') : string { - return preg_replace('[^\w\s\d\.\-_~,;:\[\]\(\]\/]', '', $path); + return preg_replace('[^\w\s\d\.\-_~,;:\[\]\(\]\/]', $replace, $path); } /** diff --git a/System/File/Local/File.php b/System/File/Local/File.php index a4aaa6088..62d0199fa 100644 --- a/System/File/Local/File.php +++ b/System/File/Local/File.php @@ -143,9 +143,9 @@ class File extends FileAbstract implements FileInterface /** * {@inheritdoc} */ - public static function sanitize(string $path) : string + public static function sanitize(string $path, string $replace = '') : string { - return preg_replace('/[^\w\s\d\.\-_~,;\[\]\(\]]/', '', $path); + return preg_replace('/[^\w\s\d\.\-_~,;\[\]\(\]]/', $replace, $path); } /** From f051043421f51f95fc3ee173519a083c3ce8bee8 Mon Sep 17 00:00:00 2001 From: Dennis Eichhorn Date: Mon, 31 Oct 2016 11:49:04 +0100 Subject: [PATCH 3/4] Fix expire bug --- DataStorage/Cache/FileCache.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/DataStorage/Cache/FileCache.php b/DataStorage/Cache/FileCache.php index 67efe4ac1..cade7db92 100644 --- a/DataStorage/Cache/FileCache.php +++ b/DataStorage/Cache/FileCache.php @@ -284,7 +284,7 @@ class FileCache implements CacheInterface $created = Directory::created($path)->getTimestamp(); $now = time(); - if ($expire >= 0 && $created + $expire > $now) { + if ($expire >= 0 && $created + $expire < $now) { return null; } From d783d682559bdebecad3954cea204578e637bfef Mon Sep 17 00:00:00 2001 From: Dennis Eichhorn Date: Mon, 31 Oct 2016 12:03:01 +0100 Subject: [PATCH 4/4] Adding cache extension This makes it easier to identify the purpose and specify an editor for modifications. --- DataStorage/Cache/FileCache.php | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/DataStorage/Cache/FileCache.php b/DataStorage/Cache/FileCache.php index cade7db92..44657bda5 100644 --- a/DataStorage/Cache/FileCache.php +++ b/DataStorage/Cache/FileCache.php @@ -144,7 +144,7 @@ class FileCache implements CacheInterface $path = File::sanitize($key, '~'); - file_put_contents($this->cachePath . '/' . $path, $this->build($value, $expire)); + file_put_contents($this->cachePath . '/' . $path . '.cache', $this->build($value, $expire)); return false; } @@ -158,10 +158,11 @@ class FileCache implements CacheInterface return false; } - $path = File::sanitize($key, '~'); + $name = File::sanitize($key, '~'); + $path = $this->cachePath . '/' . $path . '.cache'; - if (!file_exists($this->cachePath . '/' . $path)) { - file_put_contents($this->cachePath . '/' . $path, $this->build($value, $expire)); + if (!file_exists($path)) { + file_put_contents($path, $this->build($value, $expire)); return true; } @@ -275,7 +276,7 @@ class FileCache implements CacheInterface } $name = File::sanitize($key, '~'); - $path = $this->cachePath . '/' . $name; + $path = $this->cachePath . '/' . $name . '.cache'; if(!file_exists($path)) { return null; @@ -342,7 +343,7 @@ class FileCache implements CacheInterface } $name = File::sanitize($key, '~'); - $path = $this->cachePath . '/' . $name; + $path = $this->cachePath . '/' . $name . '.cache'; if ($expire < 0 && file_exists($path)) { unlink($path); @@ -404,10 +405,11 @@ class FileCache implements CacheInterface return false; } - $path = File::sanitize($key, '~'); + $name = File::sanitize($key, '~'); + $path = $this->cachePath . '/' . $path . '.cache'; - if (file_exists($this->cachePath . '/' . $path)) { - file_put_contents($this->cachePath . '/' . $path, $this->build($value, $expire)); + if (file_exists($path)) { + file_put_contents($path, $this->build($value, $expire)); return true; }