Skip to content

Commit

Permalink
various updates
Browse files Browse the repository at this point in the history
  • Loading branch information
JoshuaEstes committed Sep 18, 2024
1 parent 70c200a commit 8e57c7e
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 0 deletions.
19 changes: 19 additions & 0 deletions docs/components/cache/adapters.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ Requires the APCu extension is loaded and enabled.
use SonsOfPHP\Componenet\Cache\Adapter\ApcuAdapter;

$cache = new ApcuAdapter();

// You can set a default TTL in seconds and Marshaller as well.
$cache = new ApcuAdapter(60, new CustomMarshaller());
```

## ArrayAdapter
Expand Down Expand Up @@ -49,6 +52,22 @@ $cache = new ChainAdapter([
]);
```

## FilesystemAdapter

Stores cache files on disk

```php
<?php

use SonsOfPHP\Componenet\Cache\Adapter\FilesystemAdapter;

$cache = new FilesystemAdapter();

// You can configure the directory, default permissions, default ttl, and
// marshaller
$cache = new FilesystemAdapter('/path/to/cache', 0777, 60, new CustomMarshaller());
```

## NullAdapter

Mainly used for testing, however you could have some checks in place and if
Expand Down
5 changes: 5 additions & 0 deletions docs/components/cache/marshallers.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,8 @@ use SonsOfPHP\Componenet\Cache\Marshaller\JsonMarshaller;

$marshaller = new JsonMarshaller();
```

# Custom Marshaller

You can create a custom marshaller. For example, you could create one the
encrypts/decrypts the values.
8 changes: 8 additions & 0 deletions src/SonsOfPHP/Component/Cache/Tests/CacheItemTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -129,4 +129,12 @@ public static function invalidKeysProvider(): iterable

yield 'reserved' => ['contains@reserved}characters'];
}

public function testItCanReturnExpiry(): void
{
$item = new CacheItem('testing');
$this->assertNull($item->expiry());
$item->expiresAfter(3600);
$this->assertNotNull($item->expiry());
}
}
14 changes: 14 additions & 0 deletions src/SonsOfPHP/Component/Cache/Tests/SimpleCacheTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -145,4 +145,18 @@ public function testGetMultiple(): void
$this->assertSame('default.value', $items['item.key']);
$this->assertSame('item2.value', $items['item2']);
}

public function testItWillSaveWhenTTLIsSet(): void
{
$item = $this->createMock(CacheItemInterface::class);
$item->expects($this->once())->method('set');
$item->expects($this->once())->method('expiresAfter');

$this->adapter->expects($this->once())->method('getItem')->willReturn($item);
$this->adapter->expects($this->once())->method('save')->willReturn(true);

$cache = new SimpleCache($this->adapter);

$this->assertTrue($cache->set('item.key', 'item.value', 60));
}
}

0 comments on commit 8e57c7e

Please sign in to comment.