diff --git a/.github/workflows/format-code.yml b/.github/workflows/format-code.yml index 4e34b1f..cf9cb26 100644 --- a/.github/workflows/format-code.yml +++ b/.github/workflows/format-code.yml @@ -14,6 +14,9 @@ jobs: contents: write steps: - uses: actions/checkout@v4 + with: + ref: ${{ github.event.pull_request.head.ref }} + fetch-depth: 0 - name: Validate composer.json and composer.lock run: composer validate @@ -32,15 +35,19 @@ jobs: - name: format code run: bash commands php-cs-fixer - - name: Commit and Push changes + - name: Commit and push changelog to PR branch + env: + GIT_AUTHOR_NAME: github-actions + GIT_AUTHOR_EMAIL: actions@github.com run: | - if [ -z "$(git status --porcelain)" ]; then + git config user.name "$GIT_AUTHOR_NAME" + git config user.email "$GIT_AUTHOR_EMAIL" + git add . + if git diff --staged --quiet; then echo "No changes to commit." - else - echo "Found changes, committing..." - git config --global user.name "GitHub Actions Bot" - git config --global user.email "github-actions-bot@users.noreply.github.com" - git add . - git commit -m "style: Fix PHP code style issues" - git push - fi \ No newline at end of file + exit 0 + fi + commit_msg="style: Fix PHP code style issues" + git commit -m "$commit_msg" + git push origin HEAD:${{ github.event.pull_request.head.ref }} + \ No newline at end of file diff --git a/CHANGELOG.md b/CHANGELOG.md index 13a2bfc..7ab71cc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,3 +14,7 @@ - [2025-12-22] DamImpr: Add tests directory to export-ignore in .gitattributes [#30](https://github.com/DamImpr/cache-multi-layer/pull/30) - [2026-01-05] DamImpr: add persistent connection redis [#31](https://github.com/DamImpr/cache-multi-layer/pull/31) + +- [2026-01-09] DamImpr: Redis and memcache cache, instance passed in construction [#32](https://github.com/DamImpr/cache-multi-layer/pull/32) + +- [2026-01-09] DamImpr: Instance configuration [#33](https://github.com/DamImpr/cache-multi-layer/pull/33) diff --git a/composer.lock b/composer.lock index 3c00181..7622f29 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "8310aaaf271b37ba5c613ac8d3130f08", + "content-hash": "05124d3fe3285b80707ceb8bdc9ab05b", "packages": [ { "name": "predis/predis", diff --git a/src/Service/MemcacheCache.php b/src/Service/MemcacheCache.php index 4dbee59..e14b9b7 100644 --- a/src/Service/MemcacheCache.php +++ b/src/Service/MemcacheCache.php @@ -3,7 +3,9 @@ namespace CacheMultiLayer\Service; use CacheMultiLayer\Enum\CacheEnum; +use CacheMultiLayer\Exception\CacheMissingConfigurationException; use CacheMultiLayer\Interface\Cacheable; +use Exception; use Memcache; use Override; @@ -165,25 +167,35 @@ public function set(string $key, int|float|string|Cacheable|array $val, ?int $tt protected function __construct(int $ttl, array $configuration = []) { parent::__construct($ttl, $configuration); - $this->memcache = new Memcache(); - $port = $configuration['port'] ?? 11211; - if (array_key_exists('persistent', $configuration)) { - $resultConnection = $this->memcache->pconnect($configuration['server_address'], $port); + if (array_key_exists('instance', $configuration)) { + $this->memcache = $configuration['instance']; } else { - $resultConnection = $this->memcache->connect($configuration['server_address'], $port); + $this->memcache = new Memcache(); + $port = $configuration['port'] ?? 11211; + if (array_key_exists('persistent', $configuration)) { + $resultConnection = $this->memcache->pconnect($configuration['server_address'], $port); + } else { + $resultConnection = $this->memcache->connect($configuration['server_address'], $port); + } + + if (!$resultConnection) { + throw new Exception("Connection not found"); + } } - - if (!$resultConnection) { - throw new \Exception("Connection not found"); - } - $this->compress = array_key_exists('compress', $configuration) && $configuration['compress']; } + #[\Override] + protected function assertConfig(array $configuration): void + { + if (!array_key_exists('instance', $configuration) || $configuration['instance'] instanceof Memcache) { + parent::assertConfig($configuration); + } elseif (array_key_exists('instance', $configuration)) { + throw new CacheMissingConfigurationException("instance must be " . Memcache::class . " class"); + } + } private readonly Memcache $memcache; - private readonly bool $compress; - private array $mandatoryKeys = [ 'server_address' ]; diff --git a/src/Service/RedisCache.php b/src/Service/RedisCache.php index d54adb5..a31635c 100644 --- a/src/Service/RedisCache.php +++ b/src/Service/RedisCache.php @@ -3,6 +3,7 @@ namespace CacheMultiLayer\Service; use CacheMultiLayer\Enum\CacheEnum; +use CacheMultiLayer\Exception\CacheMissingConfigurationException; use CacheMultiLayer\Interface\Cacheable; use Override; use Predis\Client as PredisClient; @@ -109,14 +110,18 @@ public function getRemainingTTL(string $key): ?int protected function __construct(int $ttl, array $configuration = []) { parent::__construct($ttl, $configuration); - $this->predisClient = new PredisClient([ - 'scheme' => $configuration['tcp'] ?? 'tcp', - 'host' => $configuration['server_address'], - 'port' => $configuration['port'] ?? 6379, - 'password' => $configuration['password'] ?? '', - 'database' => $configuration['database'] ?? 0, - 'persistent' => $configuration['persistent'] ?? false - ]); + if (array_key_exists('instance', $configuration)) { + $this->predisClient = $configuration['instance']; + } else { + $this->predisClient = new PredisClient([ + 'scheme' => $configuration['tcp'] ?? 'tcp', + 'host' => $configuration['server_address'], + 'port' => $configuration['port'] ?? 6379, + 'password' => $configuration['password'] ?? '', + 'database' => $configuration['database'] ?? 0, + 'persistent' => $configuration['persistent'] ?? false + ]); + } } /** @@ -149,8 +154,16 @@ protected function getMandatoryConfig(): array return $this->mandatoryKeys; } + #[\Override] + protected function assertConfig(array $configuration): void + { + if (!array_key_exists('instance', $configuration) || $configuration['instance'] instanceof PredisClient) { + parent::assertConfig($configuration); + } elseif (array_key_exists('instance', $configuration)) { + throw new CacheMissingConfigurationException("instance must be " . PredisClient::class . " class"); + } + } private readonly PredisClient $predisClient; - private array $mandatoryKeys = [ 'server_address' ]; diff --git a/tests/MemcacheCacheTest.php b/tests/MemcacheCacheTest.php index 48b690c..8864fc5 100644 --- a/tests/MemcacheCacheTest.php +++ b/tests/MemcacheCacheTest.php @@ -6,6 +6,7 @@ use CacheMultiLayer\Exception\CacheMissingConfigurationException; use CacheMultiLayer\Service\Cache; use Exception; +use Memcache; use Override; /** @@ -26,7 +27,7 @@ protected function setUp(): void #[Override] public static function setUpBeforeClass(): void { - parent::setUpBeforeClass(); + parent::setUpBeforeClass(); set_error_handler(function (int $errno, string $errstr, string $errfile, int $errline): bool { // error was suppressed with the @-operator if (0 === error_reporting()) { @@ -101,7 +102,7 @@ public function testIsConnected(): void { parent::testIsConnected(); } - + #[\Override] public function testEmptyDecrement(): void { @@ -132,6 +133,20 @@ public function testConnectionNotFound(): void Cache::factory(CacheEnum::MEMCACHE, 60, ['server_address' => 'ip-no-memcache'])->isConnected(); } + public function testInstance(): void + { + $memcache = new Memcache(); + $memcache->connect('memcache-server', 11211); + Cache::factory(CacheEnum::MEMCACHE, 60, ['instance' => $memcache]); + $this->assertTrue(true); //no exception throwns + } + + public function testMissingInstance(): void + { + $this->expectException(CacheMissingConfigurationException::class); + Cache::factory(CacheEnum::MEMCACHE, 60, ['instance' => 5]); + } + public function testEnum(): void { $this->doTestRealEnum(CacheEnum::MEMCACHE); @@ -140,7 +155,7 @@ public function testEnum(): void #[\Override] public static function tearDownAfterClass(): void { - restore_error_handler(); + restore_error_handler(); Cache::factory(CacheEnum::MEMCACHE, 60, ['server_address' => 'memcache-server'])->clearAllCache(); } -} +} \ No newline at end of file diff --git a/tests/RedisCacheTest.php b/tests/RedisCacheTest.php index 342569e..af9d235 100644 --- a/tests/RedisCacheTest.php +++ b/tests/RedisCacheTest.php @@ -7,6 +7,7 @@ use CacheMultiLayer\Service\Cache; use Exception; use Override; +use Predis\Client; /** * REDIS unit test class implementation @@ -139,10 +140,26 @@ public function testConnectionPersistent(): void $this->assertTrue(Cache::factory(CacheEnum::REDIS, 60, ['server_address' => 'redis-server', 'persistent' => true])->isConnected()); } + public function testInstance(): void + { + $redis = new Client([ + 'host' => 'redis-server', + 'port' => 6379 + ]); + Cache::factory(CacheEnum::REDIS, 60, ['instance' => $redis]); + $this->assertTrue(true); //no exception throwns + } + + public function testMissingInstance(): void + { + $this->expectException(CacheMissingConfigurationException::class); + Cache::factory(CacheEnum::REDIS, 60, ['instance' => 5]); + } + #[\Override] public static function tearDownAfterClass(): void { restore_error_handler(); Cache::factory(CacheEnum::REDIS, 60, ['server_address' => 'redis-server'])->clearAllCache(); } -} +} \ No newline at end of file