Skip to content
Open
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
122 changes: 122 additions & 0 deletions src/Discord/Helpers/VoidCache.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
<?php

/*
* This file is a part of the DiscordPHP project.
*
* Copyright (c) 2015-present David Cole <david.cole1340@gmail.com>
*
* This file is subject to the MIT license that is bundled
* with this source code in the LICENSE.md file.
*/

namespace Discord\Helpers;

use Psr\SimpleCache\CacheInterface;

/**
* A no-op cache that never stores values and always returns the provided default.
*
* This class is conditionally defined to remain compatible with
* psr/simple-cache v1, v2 and v3 method signatures.
*
* @since v10.48.0
*/
if ((new \ReflectionMethod(CacheInterface::class, 'get'))->hasReturnType()) {
class VoidCache implements CacheInterface
{
public function get(string $key, mixed $default = null): mixed
{
return $default;
}

public function set(string $key, mixed $value, null|int|\DateInterval $ttl = null): bool
{
return true;
}

public function delete(string $key): bool
{
return true;
}

public function getMultiple(iterable $keys, mixed $default = null): iterable
{
$result = [];

foreach ($keys as $key) {
$result[$key] = $default;
}

return $result;
}

public function setMultiple(iterable $values, null|int|\DateInterval $ttl = null): bool
{
return true;
}

public function deleteMultiple(iterable $keys): bool
{
return true;
}

public function clear(): bool
{
return true;
}

public function has(string $key): bool
{
return false;
}
}
} else {
class VoidCache implements CacheInterface
{
public function get($key, $default = null)
{
return $default;
}

public function set($key, $value, $ttl = null)
{
return true;
}

public function delete($key)
{
return true;
}

public function getMultiple($keys, $default = null)
{
$result = [];

foreach ($keys as $key) {
$result[$key] = $default;
}

return $result;
}

public function setMultiple($values, $ttl = null)
{
return true;
}

public function deleteMultiple($keys)
{
return true;
}

public function clear()
{
return true;
}

public function has($key)
{
return false;
}
}
}
93 changes: 93 additions & 0 deletions tests/Helpers/VoidCacheTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
<?php

declare(strict_types=1);

/*
* This file is a part of the DiscordPHP project.
*
* Copyright (c) 2015-present David Cole <david.cole1340@gmail.com>
*
* This file is subject to the MIT license that is bundled
* with this source code in the LICENSE.md file.
*/

use Discord\Helpers\VoidCache;
use PHPUnit\Framework\TestCase;

final class VoidCacheTest extends TestCase
{
private VoidCache $cache;

protected function setUp(): void
{
$this->cache = new VoidCache();
}

public function testGetReturnsNullByDefault(): void
{
$this->assertNull($this->cache->get('key'));
}

public function testGetReturnsProvidedDefault(): void
{
$this->assertSame('default', $this->cache->get('key', 'default'));
$this->assertSame(42, $this->cache->get('key', 42));
}

public function testSetReturnsTrue(): void
{
$this->assertTrue($this->cache->set('key', 'value'));
}

public function testDeleteReturnsTrue(): void
{
$this->assertTrue($this->cache->delete('key'));
}

public function testClearReturnsTrue(): void
{
$this->assertTrue($this->cache->clear());
}

public function testHasReturnsFalse(): void
{
$this->assertFalse($this->cache->has('key'));
}

public function testGetMultipleReturnsDefaultForAllKeys(): void
{
$keys = ['a', 'b', 'c'];
$result = $this->cache->getMultiple($keys, 'default');

$this->assertSame([
'a' => 'default',
'b' => 'default',
'c' => 'default',
], $result);
}

public function testGetMultipleReturnsNullDefaultWhenNotSpecified(): void
{
$result = $this->cache->getMultiple(['x', 'y']);

$this->assertSame(['x' => null, 'y' => null], $result);
}

public function testSetMultipleReturnsTrue(): void
{
$this->assertTrue($this->cache->setMultiple(['a' => 1, 'b' => 2]));
}

public function testDeleteMultipleReturnsTrue(): void
{
$this->assertTrue($this->cache->deleteMultiple(['a', 'b']));
}

public function testSetDoesNotPersistValue(): void
{
$this->cache->set('key', 'value');

$this->assertNull($this->cache->get('key'));
$this->assertFalse($this->cache->has('key'));
}
}