Skip to content

Commit e7a52f1

Browse files
authored
Adds MemoryClient
1 parent 0b05342 commit e7a52f1

7 files changed

Lines changed: 145 additions & 6 deletions

File tree

Client/MemoryClient.php

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Koded package.
5+
*
6+
* (c) Mihail Binev <mihail@kodeart.com>
7+
*
8+
* Please view the LICENSE distributed with this source code
9+
* for the full copyright and license information.
10+
*
11+
*/
12+
13+
namespace Koded\Caching\Client;
14+
15+
use Psr\SimpleCache\CacheInterface;
16+
17+
final class MemoryClient implements CacheInterface
18+
{
19+
20+
use ClientTrait, MultiplesTrait;
21+
22+
private $storage = [];
23+
private $expiration = [];
24+
25+
public function get($key, $default = null)
26+
{
27+
if (isset($this->expiration[$key]) && $this->expiration[$key] < time()) {
28+
$this->delete($key);
29+
30+
return $default;
31+
}
32+
33+
return $this->storage[$key] ?? $default;
34+
}
35+
36+
public function set($key, $value, $ttl = null)
37+
{
38+
if ($ttl < 0 || $ttl === 0) {
39+
return $this->delete($key);
40+
}
41+
42+
if ($ttl > 0) {
43+
$this->expiration[$key] = time() + $ttl;
44+
}
45+
46+
$this->storage[$key] = $value;
47+
48+
return true;
49+
}
50+
51+
public function delete($key)
52+
{
53+
unset($this->storage[$key], $this->expiration[$key]);
54+
55+
return false === array_key_exists($key, $this->storage);
56+
}
57+
58+
public function clear()
59+
{
60+
$this->storage = [];
61+
$this->expiration = [];
62+
63+
return true;
64+
}
65+
66+
public function deleteMultiple($keys)
67+
{
68+
$deleted = 0;
69+
foreach ($keys as $key) {
70+
$this->delete($key) && ++$deleted;
71+
}
72+
73+
return count($keys) === $deleted;
74+
}
75+
76+
public function has($key)
77+
{
78+
return array_key_exists($key, $this->storage);
79+
}
80+
}

ClientFactory.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
namespace Koded\Caching;
1414

15-
use Koded\Caching\Client\{ FileClient, MemcachedClient, NullClient, PredisClient, RedisClient, RedisJsonClient };
15+
use Koded\Caching\Client\{ FileClient, MemcachedClient, MemoryClient, NullClient, PredisClient, RedisClient, RedisJsonClient };
1616
use Koded\Caching\Serializer\{ JsonSerializer, PhpSerializer };
1717
use Koded\Stdlib\Interfaces\ConfigurationFactory;
1818
use Psr\Log\{ LoggerInterface, NullLogger };
@@ -78,6 +78,10 @@ public function build(string $client = ''): CacheInterface
7878
return new FileClient($config, $this->getLogger($config));
7979
}
8080

81+
if ('memory' === $client) {
82+
return new MemoryClient;
83+
}
84+
8185
return new NullClient;
8286
}
8387

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Koded package.
5+
*
6+
* (c) Mihail Binev <mihail@kodeart.com>
7+
*
8+
* Please view the LICENSE distributed with this source code
9+
* for the full copyright and license information.
10+
*
11+
*/
12+
13+
namespace Koded\Caching\Configuration;
14+
15+
use Koded\Stdlib\Immutable;
16+
use Koded\Stdlib\Interfaces\Configuration;
17+
18+
final class MemoryConfiguration extends Immutable implements Configuration
19+
{
20+
}

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,14 @@ function [sys_get_temp_dir()][8] is used to determine where to store the cached
252252
$cache = simple_cache_factory('file', ['dir' => '/tmp']);
253253
```
254254

255+
### MemoryClient
256+
257+
This client will store the cached items in the memory for the duration of the script's lifetime.
258+
It is useful for development, but not for production.
259+
260+
```php
261+
$cache = simple_cache_factory('memory');
262+
```
255263

256264
License
257265
-------

Tests/Client/MemoryClientTest.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
namespace Koded\Caching\Client;
4+
5+
use Koded\Caching\ClientFactory;
6+
use Koded\Caching\Configuration\ConfigFactory;
7+
use Koded\Caching\SimpleCache;
8+
use Koded\Caching\SimpleCacheTestCaseTrait;
9+
use PHPUnit\Framework\TestCase;
10+
11+
class MemoryClientTest extends TestCase
12+
{
13+
14+
use SimpleCacheTestCaseTrait;
15+
16+
protected function setUp()
17+
{
18+
putenv('CACHE_CLIENT=memory');
19+
20+
$this->cache = new SimpleCache((new ClientFactory(new ConfigFactory))->build());
21+
}
22+
}

Tests/ClientFactoryTest.php

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@
22

33
namespace Koded\Caching;
44

5-
use Koded\Caching\Client\{
6-
FileClient, MemcachedClient, NullClient, PredisClient, RedisClient
7-
};
5+
use Koded\Caching\Client\{ FileClient, MemcachedClient, MemoryClient, NullClient, PredisClient, RedisClient };
86
use Koded\Caching\Configuration\ConfigFactory;
97
use PHPUnit\Framework\TestCase;
108

@@ -56,14 +54,21 @@ public function test_should_create_file_client()
5654
$this->assertInstanceOf(FileClient::class, $client);
5755
}
5856

57+
public function test_should_create_memory_client()
58+
{
59+
$client = (new ClientFactory(new ConfigFactory))->build('memory');
60+
$this->assertInstanceOf(MemoryClient::class, $client);
61+
}
62+
5963

6064
public function test_non_supported_logger_exception()
6165
{
6266
$this->expectException(CacheException::class);
6367
$this->expectExceptionMessage('The cache logger should be NULL or an instance of Psr\Log\LoggerInterface, Closure given');
6468

6569
(new ClientFactory(new ConfigFactory([
66-
'logger' => function() {}
70+
'logger' => function() {
71+
}
6772
])))->build('file');
6873
}
6974
}

VERSION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
1.8.0
1+
1.9.0

0 commit comments

Comments
 (0)