|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Neuron\Data\Repository; |
| 4 | + |
| 5 | +/** |
| 6 | + * In-memory repository implementation for testing and development. |
| 7 | + * |
| 8 | + * The Memory repository stores data in a PHP array with automatic expiration |
| 9 | + * support. |
| 10 | + * |
| 11 | + * Note: Data is lost when the PHP process ends. For persistent storage, |
| 12 | + * use Redis or File repository implementations. |
| 13 | + * |
| 14 | + * @package Neuron\Data\Repository |
| 15 | + * |
| 16 | + * @example |
| 17 | + * ```php |
| 18 | + * $repo = new Memory(); |
| 19 | + * $repo->save('temp:data', ['foo' => 'bar'], 60); |
| 20 | + * $data = $repo->find('temp:data'); |
| 21 | + * ``` |
| 22 | + */ |
| 23 | +class Memory implements IRepository |
| 24 | +{ |
| 25 | + private array $storage = []; |
| 26 | + private array $expiration = []; |
| 27 | + |
| 28 | + /** |
| 29 | + * Save a value to memory. |
| 30 | + * |
| 31 | + * @param string $key The unique key to store the value under |
| 32 | + * @param mixed $value The value to store |
| 33 | + * @param int $ttl Time-to-live in seconds (0 = no expiration) |
| 34 | + * @return bool Always returns true |
| 35 | + */ |
| 36 | + public function save( string $key, mixed $value, int $ttl = 0 ): bool |
| 37 | + { |
| 38 | + $this->storage[ $key ] = $value; |
| 39 | + |
| 40 | + if( $ttl > 0 ) |
| 41 | + { |
| 42 | + $this->expiration[ $key ] = time() + $ttl; |
| 43 | + } |
| 44 | + else |
| 45 | + { |
| 46 | + unset( $this->expiration[ $key ] ); |
| 47 | + } |
| 48 | + |
| 49 | + return true; |
| 50 | + } |
| 51 | + |
| 52 | + /** |
| 53 | + * Find a value by key. |
| 54 | + * |
| 55 | + * @param string $key The key to look up |
| 56 | + * @return mixed The stored value, or null if not found or expired |
| 57 | + */ |
| 58 | + public function find( string $key ): mixed |
| 59 | + { |
| 60 | + if( !array_key_exists( $key, $this->storage ) ) |
| 61 | + { |
| 62 | + return null; |
| 63 | + } |
| 64 | + |
| 65 | + // Check if expired |
| 66 | + if( $this->isExpired( $key ) ) |
| 67 | + { |
| 68 | + $this->delete( $key ); |
| 69 | + return null; |
| 70 | + } |
| 71 | + |
| 72 | + return $this->storage[ $key ]; |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * Delete a value by key. |
| 77 | + * |
| 78 | + * @param string $key The key to delete |
| 79 | + * @return bool True if deleted, false if key didn't exist |
| 80 | + */ |
| 81 | + public function delete( string $key ): bool |
| 82 | + { |
| 83 | + if( !array_key_exists( $key, $this->storage ) ) |
| 84 | + { |
| 85 | + return false; |
| 86 | + } |
| 87 | + |
| 88 | + unset( $this->storage[ $key ] ); |
| 89 | + unset( $this->expiration[ $key ] ); |
| 90 | + |
| 91 | + return true; |
| 92 | + } |
| 93 | + |
| 94 | + /** |
| 95 | + * Check if a key exists in the repository. |
| 96 | + * |
| 97 | + * @param string $key The key to check |
| 98 | + * @return bool True if exists and not expired, false otherwise |
| 99 | + */ |
| 100 | + public function exists( string $key ): bool |
| 101 | + { |
| 102 | + if( !array_key_exists( $key, $this->storage ) ) |
| 103 | + { |
| 104 | + return false; |
| 105 | + } |
| 106 | + |
| 107 | + if( $this->isExpired( $key ) ) |
| 108 | + { |
| 109 | + $this->delete( $key ); |
| 110 | + return false; |
| 111 | + } |
| 112 | + |
| 113 | + return true; |
| 114 | + } |
| 115 | + |
| 116 | + /** |
| 117 | + * Check if a key has expired. |
| 118 | + * |
| 119 | + * @param string $key The key to check |
| 120 | + * @return bool True if expired, false otherwise |
| 121 | + */ |
| 122 | + private function isExpired( string $key ): bool |
| 123 | + { |
| 124 | + if( !array_key_exists( $key, $this->expiration ) ) |
| 125 | + { |
| 126 | + return false; |
| 127 | + } |
| 128 | + |
| 129 | + return time() >= $this->expiration[ $key ]; |
| 130 | + } |
| 131 | + |
| 132 | + /** |
| 133 | + * Clear all data from the repository. |
| 134 | + * |
| 135 | + * @return void |
| 136 | + */ |
| 137 | + public function clear(): void |
| 138 | + { |
| 139 | + $this->storage = []; |
| 140 | + $this->expiration = []; |
| 141 | + } |
| 142 | + |
| 143 | + /** |
| 144 | + * Get all keys in the repository (including expired). |
| 145 | + * |
| 146 | + * @return array |
| 147 | + */ |
| 148 | + public function keys(): array |
| 149 | + { |
| 150 | + return array_keys( $this->storage ); |
| 151 | + } |
| 152 | +} |
0 commit comments