Skip to content

Commit

Permalink
Merge pull request #15751 from niden/T15485-cache-ttl
Browse files Browse the repository at this point in the history
T15485 cache ttl
  • Loading branch information
niden authored Nov 3, 2021
2 parents 18a5848 + e485ed7 commit bdb1504
Show file tree
Hide file tree
Showing 56 changed files with 1,559 additions and 1,181 deletions.
25 changes: 14 additions & 11 deletions CHANGELOG-5.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,6 @@
- Changes to `Phalcon\Crypt`
- Moved `Phalcon\Crypt\Exception` to `Phalcon\Crypt\Exception\Exception`
- Moved `Phalcon\Crypt\Mismatch` to `Phalcon\Crypt\Exception\Mismatch`
- Added `Phalcon\Crypt\Padding\PadInteface` and padding adapters
- `Phalcon\Crypt\Padding\Ansi`
- `Phalcon\Crypt\Padding\Iso10126`
- `Phalcon\Crypt\Padding\IsoIek`
- `Phalcon\Crypt\Padding\Noop`
- `Phalcon\Crypt\Padding\PadInterface`
- `Phalcon\Crypt\Padding\Pkcs7`
- `Phalcon\Crypt\Padding\Space`
- `Phalcon\Crypt\Padding\Zero`
- Added `Phalcon\Crypt\PadFactory` to easily create padding adapters
- Added more tests increasing coverage
- Changed the ccm/gcm modes to store the `authTag` with the encryption string and process it with the decryption string [#15717](https://github.com/phalcon/cphalcon/issues/15717)
- Created new namespace `Phalcon\Encryption`
- Moved `Phalcon\Crypt` to `Phalcon\Encryption\Crypt`
Expand Down Expand Up @@ -86,6 +75,19 @@
- Added more tests in the suite for additional code coverage [#15691](https://github.com/phalcon/cphalcon/issues/15691)
- Added `Phalcon\Events\AbstractEventsAware` class to handle the Events Manager when necessary [#15691](https://github.com/phalcon/cphalcon/issues/15691)
- Added `Phalcon\Acl\Adapter\AdapterInterface::getInheritedRoles()` and `Phalcon\Acl\Adapter\Memory::getInheritedRoles()` that returns the inherited roles based on a passed role name (or all if no parameter supplied) [#15154](https://github.com/phalcon/cphalcon/issues/15154)
- Changes to `Phalcon\Crypt`
- Added `Phalcon\Crypt\Padding\PadInteface` and padding adapters
- `Phalcon\Crypt\Padding\Ansi`
- `Phalcon\Crypt\Padding\Iso10126`
- `Phalcon\Crypt\Padding\IsoIek`
- `Phalcon\Crypt\Padding\Noop`
- `Phalcon\Crypt\Padding\PadInterface`
- `Phalcon\Crypt\Padding\Pkcs7`
- `Phalcon\Crypt\Padding\Space`
- `Phalcon\Crypt\Padding\Zero`
- Added `Phalcon\Crypt\PadFactory` to easily create padding adapters
- Added more tests increasing coverage [#15717](https://github.com/phalcon/cphalcon/issues/15717)
- Added `Phalcon\Cache\Adapter\*::setForever()` and `Phalcon\Storage\Adapter\*::setForever()` to allow storing a key forever [#15485](https://github.com/phalcon/cphalcon/issues/15485)

## Fixed
- Fixed `Query::getExpression()` return type [#15553](https://github.com/phalcon/cphalcon/issues/15553)
Expand All @@ -97,6 +99,7 @@
- Fixed globals (Zephir change) to correctly display string values for global settings in `phpinfo()` [#15269](https://github.com/phalcon/cphalcon/issues/15269)
- Fixed `Phalcon\Storage\Adapter\Redis::getAdapter()` and `Phalcon\Cache\Adapter\Redis::getAdapter()` to accept the connection timeout in the constructor `options` [#15744](https://github.com/phalcon/cphalcon/issues/15744)
- Fixed `Phalcon\Db\Adapter\AbstractAdapter::getSQLVariables()` to return an empty array when initialized [#15637](https://github.com/phalcon/cphalcon/issues/15637)
- Fixed `Phalcon\Cache\Adapter\*` and `Phalcon\Storage\Adapter\*` to delete a key when `set()` is called with a zero or negative TTL [#15485](https://github.com/phalcon/cphalcon/issues/15485)

# [5.0.0alpha6](https://github.com/phalcon/cphalcon/releases/tag/v5.0.0alpha6) (2021-09-16)

Expand Down
25 changes: 21 additions & 4 deletions phalcon/Storage/Adapter/AdapterInterface.zep
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,28 @@ interface AdapterInterface
public function increment(string! key, int value = 1) -> int | bool;

/**
* Stores data in the adapter
* Stores data in the adapter. If the TTL is `null` (default) or not defined
* then the default TTL will be used, as set in this adapter. If the TTL
* is `0` or a negative number, a `delete()` will be issued, since this
* item has expired. If you need to set this key forever, you should use
* the `setForever()` method.
*
* @param string key
* @param mixed value
* @param \DateInterval|int|null ttl
* @param string $key
* @param mixed $value
* @param \DateInterval|int|null $ttl
*
* @return bool
*/
public function set(string! key, var value, var ttl = null) -> bool;

/**
* Stores data in the adapter forever. The key needs to manually deleted
* from the adapter.
*
* @param string $key
* @param mixed $value
*
* @return bool
*/
public function setForever(string key, value) -> bool;
}
31 changes: 28 additions & 3 deletions phalcon/Storage/Adapter/Apcu.zep
Original file line number Diff line number Diff line change
Expand Up @@ -169,9 +169,9 @@ class Apcu extends AbstractAdapter
/**
* Stores data in the adapter
*
* @param string $key
* @param mixed $value
* @param DateInterval|int|null $ttl
* @param string $key
* @param mixed $value
* @param \DateInterval|int|null $ttl
*
* @return bool
* @throws Exception
Expand All @@ -180,6 +180,10 @@ class Apcu extends AbstractAdapter
{
var result;

if (typeof ttl === "integer" && ttl < 1) {
return this->delete(key);
}

let result = apcu_store(
this->getPrefixedKey(key),
this->getSerializedData(value),
Expand All @@ -188,4 +192,25 @@ class Apcu extends AbstractAdapter

return typeof result === "bool" ? result : false;
}

/**
* Stores data in the adapter forever. The key needs to manually deleted
* from the adapter.
*
* @param string $key
* @param mixed $value
*
* @return bool
*/
public function setForever(string! key, var value) -> bool
{
var result;

let result = apcu_store(
this->getPrefixedKey(key),
this->getSerializedData(value)
);

return typeof result === "bool" ? result : false;
}
}
45 changes: 36 additions & 9 deletions phalcon/Storage/Adapter/Libmemcached.zep
Original file line number Diff line number Diff line change
Expand Up @@ -205,23 +205,50 @@ class Libmemcached extends AbstractAdapter
/**
* Stores data in the adapter
*
* @param string $key
* @param mixed $value
* @param DateInterval|int|null $ttl
* @param string $key
* @param mixed $value
* @param \DateInterval|int|null $ttl
*
* @return bool
* @throws BaseException
* @throws StorageException
*/
public function set(string key, var value, var ttl = null) -> bool
{
return this->getAdapter()
->set(
key,
this->getSerializedData(value),
this->getTtl(ttl)
)
var result;

if (typeof ttl === "integer" && ttl < 1) {
return this->delete(key);
}

let result = this->getAdapter()
->set(
key,
this->getSerializedData(value),
this->getTtl(ttl)
)
;

return typeof result === "bool" ? result : false;
}

/**
* Stores data in the adapter forever. The key needs to manually deleted
* from the adapter.
*
* @param string $key
* @param mixed $value
*
* @return bool
*/
public function setForever(string key, var value) -> bool
{
var result;

let result = this->getAdapter()
->set(key, this->getSerializedData(value), 0);

return typeof result === "bool" ? result : false;
}

/**
Expand Down
27 changes: 22 additions & 5 deletions phalcon/Storage/Adapter/Memory.zep
Original file line number Diff line number Diff line change
Expand Up @@ -169,23 +169,40 @@ class Memory extends AbstractAdapter
/**
* Stores data in the adapter
*
* @param string $key
* @param mixed $value
* @param DateInterval|int|null $ttl
* @param string $key
* @param mixed $value
* @param \DateInterval|int|null $ttl
*
* @return bool
* @throws BaseException
*/
public function set(string! key, var value, var ttl = null) -> bool
{
var content, lifetime, prefixedKey;
var content, prefixedKey;

if (typeof ttl === "integer" && ttl < 1) {
return this->delete(key);
}

let content = this->getSerializedData(value),
lifetime = this->getTtl(ttl),
prefixedKey = this->getPrefixedKey(key);

let this->data[prefixedKey] = content;

return true;
}

/**
* Stores data in the adapter forever. The key needs to manually deleted
* from the adapter.
*
* @param string $key
* @param mixed $value
*
* @return bool
*/
public function setForever(string! key, var value) -> bool
{
return this->set(key, value);
}
}
45 changes: 36 additions & 9 deletions phalcon/Storage/Adapter/Redis.zep
Original file line number Diff line number Diff line change
Expand Up @@ -186,22 +186,49 @@ class Redis extends AbstractAdapter
/**
* Stores data in the adapter
*
* @param string $key
* @param mixed $value
* @param DateInterval|int|null $ttl
* @param string $key
* @param mixed $value
* @param \DateInterval|int|null $ttl
*
* @return bool
* @throws BaseException
*/
public function set(string! key, var value, var ttl = null) -> bool
{
return this->getAdapter()
->set(
key,
this->getSerializedData(value),
this->getTtl(ttl)
)
var result;

if (typeof ttl === "integer" && ttl < 1) {
return this->delete(key);
}

let result = this->getAdapter()
->set(
key,
this->getSerializedData(value),
this->getTtl(ttl)
)
;

return typeof result === "bool" ? result : false;
}

/**
* Stores data in the adapter forever. The key needs to manually deleted
* from the adapter.
*
* @param string $key
* @param mixed $value
*
* @return bool
*/
public function setForever(string! key, var value) -> bool
{
var result;

let result = this->getAdapter()
->set(key, this->getSerializedData(value));

return typeof result === "bool" ? result : false;
}

/**
Expand Down
74 changes: 60 additions & 14 deletions phalcon/Storage/Adapter/Stream.zep
Original file line number Diff line number Diff line change
Expand Up @@ -246,30 +246,49 @@ class Stream extends AbstractAdapter
/**
* Stores data in the adapter
*
* @param string $key
* @param mixed $value
* @param DateInterval|int|null $ttl
* @param string $key
* @param mixed $value
* @param \DateInterval|int|null $ttl
*
* @return bool
*/
public function set(string! key, var value, var ttl = null) -> bool
{
var directory;
array payload;

if (typeof ttl === "integer" && ttl < 1) {
return this->delete(key);
}

let payload = [
"created" : time(),
"ttl" : this->getTtl(ttl),
"content" : this->getSerializedData(value)
],
payload = serialize(payload),
directory = this->getDir(key);
"created" : time(),
"ttl" : this->getTtl(ttl),
"content" : this->getSerializedData(value)
];

if !is_dir(directory) {
mkdir(directory, 0777, true);
}
return this->storePayload(payload, key);
}

return false !== file_put_contents(directory . key, payload, LOCK_EX);
/**
* Stores data in the adapter forever. The key needs to manually deleted
* from the adapter.
*
* @param string $key
* @param mixed $value
*
* @return bool
*/
public function setForever(string! key, var value) -> bool
{
array payload;

let payload = [
"created" : time(),
"ttl" : "forever",
"content" : this->getSerializedData(value)
];

return this->storePayload(payload, key);
}

/**
Expand Down Expand Up @@ -400,9 +419,36 @@ class Stream extends AbstractAdapter
let created = this->getArrVal(payload, "created", time()),
ttl = this->getArrVal(payload, "ttl", 3600);

if ("forever" === ttl) {
return false;
}

return (created + ttl) < time();
}


/**
* Stores an array payload on the file system
*
* @param array $payload
* @param string $key
*
* @return bool
*/
private function storePayload(array payload, string key) -> bool
{
var directory, payload;

let payload = serialize(payload),
directory = this->getDir(key);

if !is_dir(directory) {
mkdir(directory, 0777, true);
}

return false !== file_put_contents(directory . key, payload, LOCK_EX);
}

/**
* @todo Remove this when we get traits
*/
Expand Down
Loading

0 comments on commit bdb1504

Please sign in to comment.