Skip to content

Commit

Permalink
CacheTrait and CacheInterfaces
Browse files Browse the repository at this point in the history
  • Loading branch information
valzargaming committed Jan 22, 2025
1 parent 2e8f09b commit 2575754
Show file tree
Hide file tree
Showing 2 changed files with 153 additions and 1 deletion.
147 changes: 147 additions & 0 deletions src/Discord/Helpers/CacheTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
<?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;

trait CacheTrait
{
/**
* Fetches a value from the cache.
*
* @param string $key The unique key of this item in the cache.
* @param mixed $default Default value to return if the key does not exist.
*
* @return mixed The value of the item from the cache, or $default in case of cache miss.
*
* @throws InvalidArgumentException
*/
public function get(string $key, mixed $default = null): mixed
{
return $this->items[$key] ?? $default;
}

/**
* Persists data in the cache, uniquely referenced by a key with an optional expiration TTL time.
*
* @param string $key The key of the item to store.
* @param mixed $value The value of the item to store, must be serializable.
* @param null|int|\DateInterval $ttl Optional. The TTL value of this item.
*
* @return bool True on success and false on failure.
*
* @throws InvalidArgumentException
*/
public function set(string $key, mixed $value, null|int|\DateInterval $ttl = null): bool
{
$this->items[$key] = $value;
return true;
}

/**
* Delete an item from the cache by its unique key.
*
* @param string $key The unique cache key of the item to delete.
*
* @return bool True if the item was successfully removed. False if there was an error.
*
* @throws InvalidArgumentException
*/
public function delete(string $key): bool
{
if (isset($this->items[$key])) {
unset($this->items[$key]);
return true;
}
return false;
}

/**
* Wipes clean the entire cache's keys.
*
* @return bool True on success and false on failure.
*/
public function clear(): bool
{
$this->items = [];
return true;
}

/**
* Obtains multiple cache items by their unique keys.
*
* @param iterable<string> $keys A list of keys that can be obtained in a single operation.
* @param mixed $default Default value to return for keys that do not exist.
*
* @return iterable<string, mixed> A list of key => value pairs.
*
* @throws InvalidArgumentException
*/
public function getMultiple(iterable $keys, mixed $default = null): iterable
{
$results = [];
foreach ($keys as $key) {
$results[$key] = $this->get($key, $default);
}
return $results;
}

/**
* Persists a set of key => value pairs in the cache, with an optional TTL.
*
* @param iterable $values A list of key => value pairs for a multiple-set operation.
* @param null|int|\DateInterval $ttl Optional. The TTL value of this item.
*
* @return bool True on success and false on failure.
*
* @throws InvalidArgumentException
*/
public function setMultiple(iterable $values, null|int|\DateInterval $ttl = null): bool
{
foreach ($values as $key => $value) {
$this->set($key, $value, $ttl);
}
return true;
}

/**
* Deletes multiple cache items in a single operation.
*
* @param iterable<string> $keys A list of string-based keys to be deleted.
*
* @return bool True if the items were successfully removed. False if there was an error.
*
* @throws InvalidArgumentException
*/
public function deleteMultiple(iterable $keys): bool
{
$success = true;
foreach ($keys as $key) {
if (! $this->delete($key)) {
$success = false;
}
}
return $success;
}

/**
* Determines whether an item is present in the cache.
*
* @param string $key The cache item key.
*
* @return bool
*
* @throws InvalidArgumentException
*/
public function has(string $key): bool
{
return isset($this->items[$key]);
}
}
7 changes: 6 additions & 1 deletion src/Discord/Helpers/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,17 @@

namespace Discord\Helpers;

use React\Cache\CacheInterface as ReactCacheInterface;
use Psr\SimpleCache\CacheInterface as PsrCacheInterface;

/**
* Collection of items. Inspired by Laravel Collections.
*
* @since 5.0.0 No longer extends Laravel's BaseCollection
* @since 4.0.0
*
*/
class Collection implements CollectionInterface
class Collection implements CollectionInterface, PsrCacheInterface, ReactCacheInterface
{
/**
* The collection discriminator.
Expand All @@ -41,4 +45,5 @@ class Collection implements CollectionInterface
protected $class;

use CollectionTrait;
use CacheTrait;
}

0 comments on commit 2575754

Please sign in to comment.