-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #15 from byjg/5.0
Add GarbageCollectorInterface.php
- Loading branch information
Showing
18 changed files
with
599 additions
and
124 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
services: | ||
memcached: | ||
image: memcached | ||
container_name: memcached | ||
ports: | ||
- "11211:11211" | ||
|
||
redis: | ||
image: redis | ||
container_name: redis | ||
ports: | ||
- "6379:6379" | ||
healthcheck: | ||
test: ["CMD", "redis-cli", "ping"] | ||
interval: 10s | ||
timeout: 5s | ||
retries: 5 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
# Atomic Operations | ||
|
||
Some cache engines allow you to do atomic operations such as incrementing or decrementing a value. | ||
|
||
Besides this is not cache operation, it is a common operation in cache engines. | ||
|
||
The advantage of using atomic operations is that you can avoid race conditions when multiple processes | ||
are trying to update the same value. | ||
|
||
The atomic operations are: | ||
- Increment: Increment a value by a given number | ||
- Decrement: Decrement a value by a given number | ||
- Add: Add a value to a list in the cache | ||
|
||
The engines that support atomic operations have to implement the `AtomicOperationInterface`. | ||
|
||
Some engines that support atomic operations are: | ||
- RedisCachedEngine | ||
- MemcachedEngine | ||
- TmpfsCacheEngine | ||
- FileSystemCacheEngine | ||
|
||
## Increment | ||
|
||
The increment operation is used to increment a value by a given number. | ||
|
||
```php | ||
<?php | ||
/** @var \ByJG\Cache\AtomicOperationInterface $cache */ | ||
$cache->increment('my-key', 1); | ||
``` | ||
|
||
## Decrement | ||
|
||
The decrement operation is used to decrement a value by a given number. | ||
|
||
```php | ||
<?php | ||
/** @var \ByJG\Cache\AtomicOperationInterface $cache */ | ||
$cache->decrement('my-key', 1); | ||
``` | ||
|
||
## Add | ||
|
||
The add operation is used to add a value to a list in the cache. | ||
|
||
```php | ||
<?php | ||
/** @var \ByJG\Cache\AtomicOperationInterface $cache */ | ||
$cache->add('my-key', 'value1'); | ||
$cache->add('my-key', 'value2'); | ||
$cache->add('my-key', 'value3'); | ||
|
||
print_r($cache->get('my-key')); // ['value1', 'value2', 'value3'] | ||
``` | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
# Garbage Collection | ||
|
||
Some cache engines need to have a garbage collection process to remove the expired keys. | ||
|
||
In some engines like `Memcached` and `Redis` the garbage collection is done automatically by the engine itself. | ||
|
||
In other engines like `FileSystem` and `Array` there is no such process. The current implementation | ||
is based on the Best Effort. It means an expired key is removed only when you try to access it. | ||
|
||
If the cache engine has a low hit rate, it is recommended to run a garbage collection process | ||
to avoid the cache to grow indefinitely. | ||
|
||
The classes that implement the `GarbageCollectionInterface` have the method `collectGarbage()`. | ||
|
||
Some engines that support garbage collection are: | ||
- FileSystemCacheEngine | ||
- ArrayCacheEngine | ||
- TmpfsCacheEngine | ||
|
||
## Example | ||
|
||
```php | ||
<?php | ||
/** @var \ByJG\Cache\GarbageCollectionInterface $cache */ | ||
$cache->collectGarbage(); | ||
``` | ||
|
||
Note: The garbage collection process is blocking. | ||
It means the process will be slow if you have a lot of keys to remove. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<?php | ||
|
||
namespace ByJG\Cache; | ||
|
||
use DateInterval; | ||
|
||
interface AtomicOperationInterface | ||
{ | ||
public function increment(string $key, int $value = 1, DateInterval|int|null $ttl = null): int; | ||
|
||
public function decrement(string $key, int $value = 1, DateInterval|int|null $ttl = null): int; | ||
|
||
public function add(string $key, $value, DateInterval|int|null $ttl = null): array; | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<?php | ||
|
||
namespace ByJG\Cache; | ||
|
||
interface GarbageCollectorInterface | ||
{ | ||
public function collectGarbage(); | ||
|
||
public function getTtl(string $key): ?int; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.