-
-
Notifications
You must be signed in to change notification settings - Fork 241
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2e8f09b
commit 2575754
Showing
2 changed files
with
153 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters