mirror of
https://github.com/Karaka-Management/phpOMS.git
synced 2026-01-22 06:18:41 +00:00
Fix file cache implementation
This commit is contained in:
parent
7d3620c4f4
commit
8c1e601bfd
|
|
@ -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;
|
||||
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
|
||||
|
||||
/**
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user