Skip to content

Commit f63a9f0

Browse files
committed
Merge branch 'release/0.8.6'
2 parents 1a6aa79 + e979cf8 commit f63a9f0

7 files changed

Lines changed: 899 additions & 2 deletions

File tree

.version.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22
"strategy": "semver",
33
"major": 0,
44
"minor": 8,
5-
"patch": 5,
5+
"patch": 6,
66
"build": 0
77
}

VERSIONLOG.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1-
## 0.8.5 2025-11-15
1+
## 0.8.6 2025-11-19
2+
* Added redis repository.
23

4+
## 0.8.5 2025-11-15
35
## 0.8.4 2025-11-15
46
* Updated filter classes.
57

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<?php
2+
3+
namespace Neuron\Data\Repository;
4+
5+
/**
6+
* Repository interface for storing and retrieving serializable data.
7+
*
8+
* The IRepository interface provides a consistent API for persisting data across
9+
* different storage backends (Redis, Memory, File, etc.). It supports basic CRUD
10+
* operations with optional time-to-live (TTL) for automatic expiration.
11+
*
12+
*
13+
* @package Neuron\Data\Repository
14+
*
15+
* @example
16+
* ```php
17+
* // Store a DTO with 1 hour expiration
18+
* $repo = new Redis();
19+
* $repo->save('user:123', $userDto, 3600);
20+
*
21+
* // Retrieve the DTO
22+
* $dto = $repo->find('user:123');
23+
*
24+
* // Check existence
25+
* if ($repo->exists('user:123')) {
26+
* $repo->delete('user:123');
27+
* }
28+
* ```
29+
*/
30+
interface IRepository
31+
{
32+
/**
33+
* Save a value to the repository.
34+
*
35+
* @param string $key The unique key to store the value under
36+
* @param mixed $value The value to store (must be serializable)
37+
* @param int $ttl Time-to-live in seconds (0 = no expiration)
38+
* @return bool True on success, false on failure
39+
*/
40+
public function save( string $key, mixed $value, int $ttl = 0 ): bool;
41+
42+
/**
43+
* Find a value by key.
44+
*
45+
* @param string $key The key to look up
46+
* @return mixed The stored value, or null if not found or expired
47+
*/
48+
public function find( string $key ): mixed;
49+
50+
/**
51+
* Delete a value by key.
52+
*
53+
* @param string $key The key to delete
54+
* @return bool True if deleted, false if key didn't exist
55+
*/
56+
public function delete( string $key ): bool;
57+
58+
/**
59+
* Check if a key exists in the repository.
60+
*
61+
* @param string $key The key to check
62+
* @return bool True if exists and not expired, false otherwise
63+
*/
64+
public function exists( string $key ): bool;
65+
}

src/Data/Repository/Memory.php

Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
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

Comments
 (0)