Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 17 additions & 10 deletions .github/workflows/format-code.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
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 }}

4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
2 changes: 1 addition & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

36 changes: 24 additions & 12 deletions src/Service/MemcacheCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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'
];
Expand Down
31 changes: 22 additions & 9 deletions src/Service/RedisCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
]);
}
}

/**
Expand Down Expand Up @@ -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'
];
Expand Down
23 changes: 19 additions & 4 deletions tests/MemcacheCacheTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use CacheMultiLayer\Exception\CacheMissingConfigurationException;
use CacheMultiLayer\Service\Cache;
use Exception;
use Memcache;
use Override;

/**
Expand All @@ -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()) {
Expand Down Expand Up @@ -101,7 +102,7 @@ public function testIsConnected(): void
{
parent::testIsConnected();
}

#[\Override]
public function testEmptyDecrement(): void
{
Expand Down Expand Up @@ -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);
Expand All @@ -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();
}
}
}
19 changes: 18 additions & 1 deletion tests/RedisCacheTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use CacheMultiLayer\Service\Cache;
use Exception;
use Override;
use Predis\Client;

/**
* REDIS unit test class implementation
Expand Down Expand Up @@ -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();
}
}
}