Skip to content

Commit 1e30018

Browse files
authored
Merge pull request #17 from byjg/6.0
PHP Cache Engine v6.0
2 parents 30a0b01 + 26a1c2e commit 1e30018

40 files changed

+575
-276
lines changed

.github/workflows/phpunit.yml

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,21 @@ on:
1212
jobs:
1313
Build:
1414
runs-on: 'ubuntu-latest'
15-
container: 'byjg/php:${{ matrix.php-version }}-cli'
15+
container:
16+
image: 'byjg/php:${{ matrix.php-version }}-cli'
17+
options: --user root --privileged
1618
strategy:
1719
matrix:
1820
php-version:
21+
- "8.4"
1922
- "8.3"
2023
- "8.2"
2124
- "8.1"
2225

26+
env:
27+
MEMCACHED_SERVER: "memcached:11211"
28+
REDIS_SERVER: "redis:6379"
29+
2330
# Service containers to run
2431
services:
2532
memcached:
@@ -37,10 +44,10 @@ jobs:
3744
--health-retries 5
3845
3946
steps:
40-
- uses: actions/checkout@v4
47+
- uses: actions/checkout@v5
4148
- run: composer install
42-
- run: ./vendor/bin/phpunit --stderr
4349
- run: ./vendor/bin/psalm
50+
- run: ./vendor/bin/phpunit
4451

4552
Documentation:
4653
if: github.ref == 'refs/heads/master'

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,5 @@ vendor
55
config/cacheconfig.php
66
.idea
77
.phpunit.result.cache
8+
/phpunit.xml.dist.bak
9+
/.claude/settings.local.json

.gitpod.yml

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
tasks:
2+
- name: Run Composer
3+
command: |
4+
composer install
5+
6+
image: byjg/gitpod-image:latest
7+
8+
jetbrains:
9+
phpstorm:
10+
vmoptions: '-Xmx4g'
11+
plugins:
12+
- com.github.copilot
13+
- com.intellij.kubernetes
14+
- com.intellij.mermaid
15+
- ru.adelf.idea.dotenv
16+
- org.toml.lang
17+
18+
vscode:
19+
extensions:
20+
- ikappas.composer
21+
- hbenl.test-adapter-converter
22+
- hbenl.vscode-test-explorer
23+
- felixfbecker.php-debug
24+
- neilbrayfield.php-docblocker
25+
- bmewburn.vscode-intelephense-client
26+
- getpsalm.psalm-vscode-plugin
27+
- SonarSource.sonarlint-vscode
28+
- recca0120.vscode-phpunit

.run/PSalm.run.xml

Lines changed: 0 additions & 5 deletions
This file was deleted.

.run/psalm.run.xml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<component name="ProjectRunConfigurationManager">
2+
<configuration default="false" name="psalm" type="ComposerRunConfigurationType" factoryName="Composer Script">
3+
<option name="commandLineParameters" value="" />
4+
<option name="pathToComposerJson" value="$PROJECT_DIR$/composer.json" />
5+
<option name="script" value="psalm" />
6+
<method v="2" />
7+
</configuration>
8+
</component>

.vscode/launch.json

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
{
2+
"version": "0.2.0",
3+
"configurations": [
4+
{
5+
"name": "Debug current Script in Console",
6+
"type": "php",
7+
"request": "launch",
8+
"program": "${file}",
9+
"cwd": "${fileDirname}",
10+
"port": 9003,
11+
"runtimeArgs": [
12+
"-dxdebug.start_with_request=yes"
13+
],
14+
"env": {
15+
"XDEBUG_MODE": "debug,develop",
16+
"XDEBUG_CONFIG": "client_port=${port}"
17+
}
18+
},
19+
{
20+
"name": "PHPUnit Debug",
21+
"type": "php",
22+
"request": "launch",
23+
"program": "${workspaceFolder}/vendor/bin/phpunit",
24+
"cwd": "${workspaceFolder}",
25+
"port": 9003,
26+
"runtimeArgs": [
27+
"-dxdebug.start_with_request=yes"
28+
],
29+
"env": {
30+
"XDEBUG_MODE": "debug,develop",
31+
"XDEBUG_CONFIG": "client_port=${port}"
32+
}
33+
}
34+
]
35+
}

README.md

Lines changed: 55 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,84 +1,78 @@
1-
# Cache Engine
2-
31
[![Build Status](https://github.com/byjg/php-cache-engine/actions/workflows/phpunit.yml/badge.svg?branch=master)](https://github.com/byjg/php-cache-engine/actions/workflows/phpunit.yml)
42
[![Opensource ByJG](https://img.shields.io/badge/opensource-byjg-success.svg)](http://opensource.byjg.com)
53
[![GitHub source](https://img.shields.io/badge/Github-source-informational?logo=github)](https://github.com/byjg/php-cache-engine/)
64
[![GitHub license](https://img.shields.io/github/license/byjg/php-cache-engine.svg)](https://opensource.byjg.com/opensource/licensing.html)
75
[![GitHub release](https://img.shields.io/github/release/byjg/php-cache-engine.svg)](https://github.com/byjg/php-cache-engine/releases/)
86

7+
# PHP Cache Engine
98

10-
A multipurpose cache engine PSR-6 and PSR-16 implementation with several drivers.
11-
12-
## PSR-16
13-
14-
PSR-16 defines a Simple Cache interface with less verbosity than PSR-6. Below a list
15-
of engines available in this library that is PSR-16 compliant:
16-
17-
PSR-16 Getting Started: [here](docs/basic-usage-psr16-simplecache.md)
18-
19-
## PSR-6
20-
21-
The PSR-6 implementation use the engines defined above. PSR-6 is more verbosity and
22-
have an extra layer do get and set the cache values.
23-
24-
You can use one of the factory methods to create a instance of the CachePool implementation:
25-
26-
PSR-6 Getting Started: [here](docs/basic-usage-psr6-cachepool.md)
27-
28-
## List of Cache Engines
29-
30-
| Class | Description |
31-
|:-------------------------------------------------------------------------------------------------|:-----------------------------------------------------------------------|
32-
| [\ByJG\Cache\Psr16\NoCacheEngine](docs/class-no-cache-engine.md) | Do nothing. Use it for disable the cache without change your code |
33-
| [\ByJG\Cache\Psr16\ArrayCacheEngine](docs/class-array-cache-engine.md) | Local cache only using array. It does not persists between requests |
34-
| [\ByJG\AnyDataset\NoSql\Cache\KeyValueCacheEngine](https://github.com/byjg/php-anydataset-nosql) | Use S3-Like or ClouflareKV as a store for the cache (other repository) |
35-
| [\ByJG\Cache\Psr16\FileSystemCacheEngine](docs/class-filesystem-cache-engine.md) | Save the cache result in the local file system |
36-
| [\ByJG\Cache\Psr16\MemcachedEngine](docs/class-memcached-engine.md) | Uses the Memcached as the cache engine |
37-
| [\ByJG\Cache\Psr16\TmpfsCacheEngine](docs/class-tmpfs-cache-engine.md) | Uses the Tmpfs as the cache engine |
38-
| [\ByJG\Cache\Psr16\RedisCachedEngine](docs/class-redis-cache-engine.md) | uses the Redis as cache |
39-
| [\ByJG\Cache\Psr16\SessionCachedEngine](docs/class-session-cache-engine.md) | uses the PHP session as cache |
40-
| [\ByJG\Cache\Psr16\ShmopCacheEngine](docs/class-shmop-cache-engine.md) (deprecated) | uses the shared memory area for cache. Use TmpfsCacheEngine. |
41-
42-
43-
## Logging cache commands
44-
45-
You can add a PSR Log compatible to the constructor in order to get Log of the operations
46-
47-
See log examples [here](docs/setup-log-handler.md)
9+
A powerful, versatile cache implementation providing both PSR-6 and PSR-16 interfaces with support for multiple storage drivers.
4810

49-
## Use a PSR-11 container to retrieve the cache keys
11+
## Key Features
5012

51-
You can use a PSR-11 compatible to retrieve the cache keys.
13+
- **PSR-16 Simple Cache interface** - Simple, straightforward caching API
14+
- **PSR-6 Cache Pool interface** - More verbose caching with fine-grained control
15+
- **Multiple storage backends** - Choose from memory, file system, Redis, Memcached and more
16+
- **Atomic operations** - Support for increment, decrement and add operations in compatible engines
17+
- **Garbage collection** - Automatic cleanup of expired items
18+
- **PSR-11 container support** - Retrieve cache keys via dependency container
19+
- **Logging capabilities** - PSR-3 compatible logging of cache operations
5220

53-
See more [here](docs/psr11-usage.md)
21+
## Quick Start
5422

55-
## Beyond the PSR protocol
56-
57-
The PSR protocol is a good way to standardize the cache access,
58-
but sometimes you need to go beyond the protocol.
23+
```bash
24+
composer require "byjg/cache-engine"
25+
```
5926

60-
Some cache engines have additional features that are not covered by the PSR protocol.
27+
```php
28+
// PSR-16 Simple Cache
29+
$cache = new \ByJG\Cache\Psr16\FileSystemCacheEngine();
30+
$cache->set('key', 'value', 3600); // Cache for 1 hour
31+
$value = $cache->get('key');
32+
33+
// PSR-6 Cache Pool
34+
$pool = \ByJG\Cache\Factory::createFilePool();
35+
$item = $pool->getItem('key');
36+
if (!$item->isHit()) {
37+
$item->set('value');
38+
$item->expiresAfter(3600);
39+
$pool->save($item);
40+
}
41+
$value = $item->get();
42+
```
6143

62-
Some examples are:
44+
## Documentation
45+
46+
### Getting Started
47+
- [PSR-16 Simple Cache Usage](docs/basic-usage-psr16-simplecache.md)
48+
- [PSR-6 Cache Pool Usage](docs/basic-usage-psr6-cachepool.md)
49+
50+
### Available Cache Engines
51+
| Engine | Description |
52+
|:--------------------------------------------------------------------|:--------------------------------------------------------|
53+
| [NoCacheEngine](docs/class-no-cache-engine.md) | No-op engine for disabling cache without code changes |
54+
| [ArrayCacheEngine](docs/class-array-cache-engine.md) | In-memory array cache (non-persistent between requests) |
55+
| [FileSystemCacheEngine](docs/class-filesystem-cache-engine.md) | File system based caching |
56+
| [MemcachedEngine](docs/class-memcached-engine.md) | Memcached distributed caching |
57+
| [RedisCacheEngine](docs/class-redis-cache-engine.md) | Redis-based caching |
58+
| [SessionCacheEngine](docs/class-session-cache-engine.md) | PHP session-based caching |
59+
| [TmpfsCacheEngine](docs/class-tmpfs-cache-engine.md) | Tmpfs-based caching |
60+
| [ShmopCacheEngine](docs/class-shmop-cache-engine.md) | Shared memory caching (deprecated) |
61+
| [KeyValueCacheEngine](https://github.com/byjg/php-anydataset-nosql) | S3-Like or CloudflareKV storage (separate package) |
62+
63+
### Advanced Features
6364
- [Atomic Operations](docs/atomic-operations.md)
6465
- [Garbage Collection](docs/garbage-collection.md)
66+
- [Logging](docs/setup-log-handler.md)
67+
- [PSR-11 Container Usage](docs/psr11-usage.md)
6568

66-
## Install
67-
68-
Just type:
69-
70-
```
71-
composer require "byjg/cache-engine"
72-
```
73-
74-
75-
## Running Unit Testes
69+
## Running Unit Tests
7670

7771
```
7872
vendor/bin/phpunit --stderr
7973
```
8074

81-
**Note:** the parameter `--stderr` after `phpunit` is to permit run the tests on SessionCacheEngine.
75+
**Note:** The `--stderr` parameter is required for SessionCacheEngine tests to run properly.
8276

8377
## Dependencies
8478

@@ -89,5 +83,6 @@ flowchart TD
8983
byjg/cache-engine --> psr/simple-cache
9084
byjg/cache-engine --> psr/container
9185
```
86+
9287
----
9388
[Open source ByJG](http://opensource.byjg.com)

composer.json

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,15 @@
1212
}
1313
},
1414
"require": {
15-
"php": ">=8.1 <8.4",
15+
"php": ">=8.1 <8.5",
1616
"psr/cache": "^1.0|^2.0|^3.0",
17-
"psr/log": "^1.0|^1.1|^2.0",
17+
"psr/log": "^1.0|^2.0|^3.0",
1818
"psr/simple-cache": "^1.0|^2.0",
1919
"psr/container": "^1.0|^1.1|^2.0"
2020
},
2121
"require-dev": {
22-
"phpunit/phpunit": "^9.6",
23-
"vimeo/psalm": "^5.9"
22+
"phpunit/phpunit": "^10.5|^11.5",
23+
"vimeo/psalm": "^5.9|^6.2"
2424
},
2525
"suggest": {
2626
"ext-memcached": "*",
@@ -31,5 +31,9 @@
3131
"psr/cache-implementation": "1.0",
3232
"psr/simple-cache-implementation": "1.0"
3333
},
34+
"scripts": {
35+
"test": "vendor/bin/phpunit",
36+
"psalm": "vendor/bin/psalm"
37+
},
3438
"license": "MIT"
3539
}

docs/atomic-operations.md

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,28 @@
1+
---
2+
sidebar_position: 11
3+
---
4+
15
# Atomic Operations
26

37
Some cache engines allow you to do atomic operations such as incrementing or decrementing a value.
48

5-
Besides this is not cache operation, it is a common operation in cache engines.
9+
Although this is not a traditional cache operation, it is a common operation in cache engines.
610

7-
The advantage of using atomic operations is that you can avoid race conditions when multiple processes
11+
The advantage of using atomic operations is that you can avoid race conditions when multiple processes
812
are trying to update the same value.
913

10-
The atomic operations are:
11-
- Increment: Increment a value by a given number
12-
- Decrement: Decrement a value by a given number
13-
- Add: Add a value to a list in the cache
14+
**Available atomic operations:**
15+
- **Increment**: Increment a value by a given number
16+
- **Decrement**: Decrement a value by a given number
17+
- **Add**: Add a value to a list in the cache
1418

15-
The engines that support atomic operations have to implement the `AtomicOperationInterface`.
19+
The engines that support atomic operations implement the `AtomicOperationInterface`.
1620

17-
Some engines that support atomic operations are:
18-
- RedisCachedEngine
21+
**Engines that support atomic operations:**
22+
- RedisCacheEngine
1923
- MemcachedEngine
20-
- TmpfsCacheEngine
2124
- FileSystemCacheEngine
25+
- TmpfsCacheEngine (inherits from FileSystemCacheEngine)
2226

2327
## Increment
2428

docs/basic-usage-psr16-simplecache.md

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
1-
# Basic Usage - Psr16 Simple Cache
1+
---
2+
sidebar_position: 1
3+
---
24

3-
Psr16 is a standard for cache in PHP with less verbosity than Psr6.
5+
# Basic Usage - PSR-16 Simple Cache
6+
7+
PSR-16 is a standard for cache in PHP with less verbosity than PSR-6.
48

59
You can just instantiate the cache engine and use it as you can see below.
610

@@ -11,14 +15,13 @@ You can just instantiate the cache engine and use it as you can see below.
1115
$cacheEngine = new \ByJG\Cache\Psr16\FileSystemCacheEngine();
1216

1317
$result = $cacheEngine->get($key);
14-
if (empty($result))
15-
{
16-
// Do the operations will be cached
18+
if (empty($result)) {
19+
// Do the operations that will be cached
1720
// ....
1821
// And set variable result
1922
$result = "...";
2023

21-
// Set the cache:
24+
// Set the cache with 60 seconds TTL:
2225
$cacheEngine->set($key, $result, 60);
2326
}
2427
return $result;

0 commit comments

Comments
 (0)