Skip to content

Commit

Permalink
Merge pull request #16607 from noone-silent/T16606-add-events-to-stor…
Browse files Browse the repository at this point in the history
…age-and-cache

[#16606] - feat: added events and events manager to storage and cache classes
  • Loading branch information
niden authored Jun 8, 2024
2 parents 8827949 + 09a6657 commit fbb8318
Show file tree
Hide file tree
Showing 27 changed files with 1,905 additions and 42 deletions.
7 changes: 7 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@ autom4te.cache/
.zephir/
.temp/

# JetBrains IDE
*.iml
.idea/

# Zephir compiler
zephir.phar
zephir-*.phar
Expand All @@ -75,3 +79,6 @@ tests/_output/*
.php_cs.cache
.env
zephir

# Docker
docker-compose.override.yml
14 changes: 14 additions & 0 deletions CHANGELOG-5.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,20 @@
- Changed `Phalcon\Mvc\View\Engine\Volt\Compiler::compileSource` to also return `array` [#16608](https://github.com/phalcon/cphalcon/issues/16608)

### Added

- Added events and `Phalcon\Events\Manager` for `Phalcon\Storage\Adapter\Apcu`,
`Phalcon\Storage\Adapter\Redis`,
`Phalcon\Storage\Adapter\Memory`,
`Phalcon\Storage\Adapter\Libmemcached`,
`Phalcon\Storage\Adapter\Stream`,
`Phalcon\Storage\Adapter\Weak`,
`Phalcon\Cache\Adapter\Apcu`,
`Phalcon\Cache\Adapter\Redis`,
`Phalcon\Cache\Adapter\Memory`,
`Phalcon\Cache\Adapter\Libmemcached`,
`Phalcon\Cache\Adapter\Stream`,
`Phalcon\Cache\Adapter\Weak`
`Phalcon\Cache\AbstractCache`. [#16606](https://github.com/phalcon/cphalcon/issues/16606)

### Fixed

Expand Down
21 changes: 18 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,23 @@
"test-db-mysql": "php -d extension=ext/modules/phalcon.so ./vendor/bin/codecept run --ext DotReporter database --env mysql -g mysql",
"test-db-pgsql": "php -d extension=ext/modules/phalcon.so ./vendor/bin/codecept run --ext DotReporter database --env pgsql -g pgsql",
"test-db-sqlite": "php -d extension=ext/modules/phalcon.so ./vendor/bin/codecept run --ext DotReporter database --env sqlite -g sqlite",
"test-db": "composer test-db-common && composer test-db-mysql && composer test-db-sqlite",
"test-all": "composer test-unit && composer test-cli && composer test-integration && composer test-db",
"analyze": "php -d extension=ext/modules/phalcon.so ./vendor/bin/psalm"
"test-db": [
"@test-db-common",
"@test-db-mysql",
"@test-db-sqlite"
],
"test-all": [
"@test-unit",
"@test-cli",
"@test-integration",
"@test-db"
],
"analyze": "php -d extension=ext/modules/phalcon.so ./vendor/bin/psalm --no-cache",
"clean-build-stubs": [
"./vendor/bin/zephir fullclean",
"Composer\\Config::disableProcessTimeout",
"./vendor/bin/zephir build",
"./vendor/bin/zephir stubs"
]
}
}
94 changes: 89 additions & 5 deletions phalcon/Cache/AbstractCache.zep
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,14 @@ namespace Phalcon\Cache;
use DateInterval;
use Phalcon\Cache\Adapter\AdapterInterface;
use Phalcon\Cache\Exception\InvalidArgumentException;
use Phalcon\Events\EventsAwareInterface;
use Phalcon\Events\ManagerInterface;
use Traversable;

/**
* This component offers caching capabilities for your application.
*/
abstract class AbstractCache implements CacheInterface
abstract class AbstractCache implements CacheInterface, EventsAwareInterface
{
/**
* The adapter
Expand All @@ -27,6 +29,13 @@ abstract class AbstractCache implements CacheInterface
*/
protected adapter;

/**
* Event Manager
*
* @var ManagerInterface|null
*/
protected eventsManager = null;

/**
* Constructor.
*
Expand All @@ -47,6 +56,22 @@ abstract class AbstractCache implements CacheInterface
return this->adapter;
}

/**
* Sets the event manager
*/
public function setEventsManager(<ManagerInterface> eventsManager) -> void
{
let this->eventsManager = eventsManager;
}

/**
* Get the event manager
*/
public function getEventsManager() -> <ManagerInterface> | null
{
return this->eventsManager;
}

/**
* Checks the key. If it contains invalid characters an exception is thrown
*
Expand Down Expand Up @@ -108,9 +133,17 @@ abstract class AbstractCache implements CacheInterface
*/
protected function doDelete(string key) -> bool
{
var result;

this->fire("cache:beforeDelete", key);

this->checkKey(key);

return this->adapter->delete(key);
let result = this->adapter->delete(key);

this->fire("cache:afterDelete", key);

return result;
}

/**
Expand All @@ -122,13 +155,17 @@ abstract class AbstractCache implements CacheInterface

this->checkKeys(keys);

this->fire("cache:beforeDeleteMultiple", keys);

let result = true;
for key in keys {
if (true !== this->adapter->delete(key)) {
let result = false;
}
}

this->fire("cache:afterDeleteMultiple", keys);

return result;
}

Expand All @@ -146,9 +183,17 @@ abstract class AbstractCache implements CacheInterface
*/
protected function doGet(string key, var defaultValue = null) -> var
{
var result;

this->checkKey(key);

return this->adapter->get(key, defaultValue);
this->fire("cache:beforeGet", key);

let result = this->adapter->get(key, defaultValue);

this->fire("cache:afterGet", key);

return result;
}

/**
Expand All @@ -160,11 +205,15 @@ abstract class AbstractCache implements CacheInterface

this->checkKeys(keys);

this->fire("cache:beforeGetMultiple", keys);

let results = [];
for element in keys {
let results[element] = this->get(element, defaultValue);
}

this->fire("cache:afterGetMultiple", keys);

return results;
}

Expand All @@ -180,9 +229,17 @@ abstract class AbstractCache implements CacheInterface
*/
protected function doHas(string key) -> bool
{
var result;

this->checkKey(key);

return this->adapter->has(key);
this->fire("cache:beforeHas", key);

let result = this->adapter->has(key);

this->fire("cache:afterHas", key);

return result;
}

/**
Expand All @@ -205,9 +262,17 @@ abstract class AbstractCache implements CacheInterface
*/
protected function doSet(string key, var value, var ttl = null) -> bool
{
var result;

this->checkKey(key);

return this->adapter->set(key, value, ttl);
this->fire("cache:beforeSet", key);

let result = this->adapter->set(key, value, ttl);

this->fire("cache:afterSet", key);

return result;
}

/**
Expand All @@ -219,16 +284,35 @@ abstract class AbstractCache implements CacheInterface

this->checkKeys(values);

this->fire("cache:beforeSetMultiple", array_keys(values));

let result = true;
for key, value in values {
if (true !== this->set(key, value, ttl)) {
let result = false;
}
}

this->fire("cache:afterSetMultiple", array_keys(values));

return result;
}

/**
* Trigger an event for the eventsManager.
*
* @var string $eventName
* @var mixed $keys
*/
protected function fire(string eventName, var keys) -> void
{
if (this->eventsManager === null) {
return;
}

this->eventsManager->fire(eventName, this, keys, false);
}

/**
* Returns the exception class that will be used for exceptions thrown
*
Expand Down
1 change: 1 addition & 0 deletions phalcon/Cache/Adapter/Apcu.zep
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,5 @@ use Phalcon\Storage\Adapter\Apcu as StorageApcu;
*/
class Apcu extends StorageApcu implements CacheAdapterInterface
{
protected eventType = "cache";
}
1 change: 1 addition & 0 deletions phalcon/Cache/Adapter/Libmemcached.zep
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,5 @@ use Phalcon\Storage\Adapter\Libmemcached as StorageLibmemcached;
*/
class Libmemcached extends StorageLibmemcached implements CacheAdapterInterface
{
protected eventType = "cache";
}
1 change: 1 addition & 0 deletions phalcon/Cache/Adapter/Memory.zep
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,5 @@ use Phalcon\Storage\Adapter\Memory as StorageMemory;
*/
class Memory extends StorageMemory implements CacheAdapterInterface
{
protected eventType = "cache";
}
1 change: 1 addition & 0 deletions phalcon/Cache/Adapter/Redis.zep
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,5 @@ use Phalcon\Storage\Adapter\Redis as StorageRedis;
*/
class Redis extends StorageRedis implements CacheAdapterInterface
{
protected eventType = "cache";
}
1 change: 1 addition & 0 deletions phalcon/Cache/Adapter/Stream.zep
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,5 @@ use Phalcon\Storage\Adapter\Stream as StorageStream;
*/
class Stream extends StorageStream implements CacheAdapterInterface
{
protected eventType = "cache";
}
1 change: 1 addition & 0 deletions phalcon/Cache/Adapter/Weak.zep
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,5 @@ use Phalcon\Storage\Adapter\Weak as StorageWeak;
*/
class Weak extends StorageWeak implements CacheAdapterInterface
{
protected eventType = "cache";
}
Loading

0 comments on commit fbb8318

Please sign in to comment.