Skip to content

Commit

Permalink
feat: add in-memory caching
Browse files Browse the repository at this point in the history
  • Loading branch information
winternet-studio committed Jun 4, 2024
1 parent d9ce622 commit 37a326b
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions src/cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ class cache {
public $file_path;
public $file_prefix;

protected static $in_memory = [];

public function __construct($file_path, $file_prefix = '', $options = []) {
$this->file_path = rtrim($file_path, '/');
$this->file_prefix = $file_prefix;
Expand Down Expand Up @@ -57,4 +59,20 @@ public function get_full_filepath($key) {
return $this->file_path .'/'. $this->file_prefix .'_'. filesystem::make_valid_filename($key) .'.phpser';
}

/**
* Cache just in memory (in an array)
*
* @param string|integer|array $key
* @param callable $callable : Function that returns the value to be cached if it wasn't cached already
*/
public static function get_or_set_in_memory($key, $callable) {
if (is_array($key)) {
$key = json_encode($key, JSON_THROW_ON_ERROR);
}
if (!array_key_exists($key, static::$in_memory)) {
static::$in_memory[$key] = $callable();
}
return static::$in_memory[$key];
}

}

0 comments on commit 37a326b

Please sign in to comment.