Skip to content

Commit

Permalink
Forgot to update the readme. Also changed withMemoryLimit to setMemor…
Browse files Browse the repository at this point in the history
…yLimit
  • Loading branch information
imota committed Sep 4, 2019
1 parent 97f6881 commit 941f32d
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 21 deletions.
26 changes: 9 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,14 @@ composer require antevenio/memoize
```

## Usage
### Common code
```php
<?php
require_once('./vendor/autoload.php');

use Antevenio\Memoize\Memoizable;
use Antevenio\Memoize\Memoize;
use Antevenio\Memoize\Cache;

class Toolbox
{
Expand All @@ -58,15 +60,15 @@ class Toolbox
throw new \Exception($argument);
}
}

$toolbox = new Toolbox();
$memoize = new Memoize(new Cache());
```
### Basic
```php
$toolbox = new Toolbox();
$memoize = new Memoize();

for ($i = 0; $i < 10; $i++) {
$result = $memoize->memoize(
new Memoizable([$toolbox, 'multiply'], [10], 5)
(new Memoizable([$toolbox, 'multiply'], [10]))->withTtl(5)
);
echo "Result: $result\n";
sleep(1);
Expand All @@ -90,12 +92,9 @@ Result: 20
```
### Changing arguments
```php
$toolbox = new Toolbox();
$memoize = new Memoize();

for ($i = 0; $i < 10; $i++) {
$result = $memoize->memoize(
new Memoizable([$toolbox, 'multiply'], [$i % 2], 5)
(new Memoizable([$toolbox, 'multiply'], [$i % 2]))->withTtl(5)
);
echo "Result: $result\n";
sleep(1);
Expand All @@ -120,13 +119,9 @@ Result: 2
```
### Custom indexing
```php
$toolbox = new Toolbox();
$memoize = new Memoize();

for ($i = 0; $i < 10; $i++) {
$result = $memoize->memoize(
new Memoizable([$toolbox, 'multiply'], [$i], 5),
'myFixedIndex'
(new Memoizable([$toolbox, 'multiply'], [$i]))->withTtl(5)->withCustomIndex('myFixedIndex')
);
echo "Result: $result\n";
sleep(1);
Expand All @@ -149,12 +144,9 @@ Result: 10
```
### Exceptions
```php
$toolbox = new Toolbox();
$memoize = new Memoize();

for ($i = 0; $i < 10; $i++) {
$result = $memoize->memoize(
new Memoizable([$toolbox, 'throwException'], ['foo'], 5)
(new Memoizable([$toolbox, 'throwException'], ['foo']))->withTtl(5)
);
echo "Result: $result\n";
sleep(1);
Expand Down
2 changes: 1 addition & 1 deletion src/Cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class Cache
protected static $usedMemory = 0;
protected static $memoryLimit = self::DEFAULT_MEMORY_LIMIT;

public function withMemoryLimit($limit)
public function setMemoryLimit($limit)
{
self::$memoryLimit = $limit;

Expand Down
6 changes: 3 additions & 3 deletions test/MemoizeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ public function testMemoizeShouldNeverConsumeMemoryPastTheSpecifiedLimit()
{
$memoryLimit = 10000;

$this->sut->getCache()->withMemoryLimit(10000);
$this->sut->getCache()->setMemoryLimit(10000);

for ($i = 0; $i < 100; $i++) {
$mock = $this->getCallableMock();
Expand Down Expand Up @@ -242,7 +242,7 @@ public function testMemoizeShouldEvictTheFirstKeyWhenOutOfAvailableMemory()
$memoizableUsedMemory = $sampleMemoizable->calculateUsedMemory();
$memoryLimit = $memoizableUsedMemory * 2 +
$memoizableUsedMemory / 2;
$this->sut->getCache()->withMemoryLimit(
$this->sut->getCache()->setMemoryLimit(
$memoryLimit
);
/** @var \PHPUnit_Framework_MockObject_MockObject $mock */
Expand All @@ -265,7 +265,7 @@ public function testMemoizeShouldNotCacheMemoizablesThatExceedTheMemoryLimit()
{
$mock = $this->getCallableMock();
$memoizable = (new Memoizable([$mock, 'doit'], ['a', 'b']))->withTtl(100);
$this->sut->getCache()->withMemoryLimit($memoizable->calculateUsedMemory() - 1);
$this->sut->getCache()->setMemoryLimit($memoizable->calculateUsedMemory() - 1);

$mock->expects($this->exactly(2))
->method('doit')
Expand Down

0 comments on commit 941f32d

Please sign in to comment.