Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add helper for PSR-16/void-cache #1190

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
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
66 changes: 66 additions & 0 deletions src/Discord/Helpers/VoidCache.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php

/*
* This file is a part of the DiscordPHP project.
*
* Copyright (c) 2015-present David Cole <[email protected]>
*
* 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;

/**
* The cache that always be null/void
*/
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;
Comment on lines +38 to +44
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
$result = [];
foreach ($keys as $key) {
$result[$key] = $default;
}
return $result;
return array_fill_keys($keys, $default);

Though not sure if it accepts iterable, so maybe this instead?

Suggested change
$result = [];
foreach ($keys as $key) {
$result[$key] = $default;
}
return $result;
return array_fill_keys(iterator_to_array($keys), $default);

Copy link
Member Author

@SQKo SQKo May 8, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought of this before, but i changed my mind

Now i forgot why i changed my mind, it was probably something with the object's getIterator() implementation and the use of Generator, but not sure which

}

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;
}
}