Merge pull request #103 from utopia-php/feat/absorb-cache

feat: absorb utopia-php/cache
This commit is contained in:
Luke B. Silver authored and GitHub committed 2026-08-04 01:06:02 +01:00
1 parent f2a2eb0368
commit 6f8cb03a5c
67 files changed
+1079 -5420

No files matched your search

Vendored
BIN
View File
Binary file not shown.
-93
View File
@@ -1,93 +0,0 @@
name: "🐛 Bug Report"
description: "Submit a bug report to help us improve"
title: "🐛 Bug Report: "
labels: [bug]
body:
- type: markdown
attributes:
value: |
Thanks for taking the time to fill out our bug report form 🙏
- type: textarea
id: steps-to-reproduce
validations:
required: true
attributes:
label: "👟 Reproduction steps"
description: "How do you trigger this bug? Please walk us through it step by step."
placeholder: "When I ..."
- type: textarea
id: expected-behavior
validations:
required: true
attributes:
label: "👍 Expected behavior"
description: "What did you think would happen?"
placeholder: "It should ..."
- type: textarea
id: actual-behavior
validations:
required: true
attributes:
label: "👎 Actual Behavior"
description: "What did actually happen? Add screenshots, if applicable."
placeholder: "It actually ..."
- type: dropdown
id: utopia-version
attributes:
label: "🎲 Utopia Cache version"
description: "What version of Utopia are you running?"
options:
- Version 0.18.x
- Version 0.17.x
- Version 0.16.x
- Version 0.15.x
- Version 0.14.x
- Different version (specify in environment)
validations:
required: true
- type: dropdown
id: php-version
attributes:
label: "🐘 PHP Version"
description: "What version of PHP are you running?"
options:
- PHP 8.0.x
- PHP 8.1.x
- Different version (specify in environment)
validations:
required: true
- type: dropdown
id: operating-system
attributes:
label: "💻 Operating system"
description: "What OS is your server / device running on?"
options:
- Linux
- MacOS
- Windows
- Something else
validations:
required: true
- type: textarea
id: environment
validations:
required: false
attributes:
label: "🧱 Your Environment"
description: "Is your environment customized in any way?"
placeholder: "I use Cloudflare for ..."
- type: checkboxes
id: no-duplicate-issues
attributes:
label: "👀 Have you spent some time to check if this issue has been raised before?"
description: "Have you Googled for a similar issue or checked our older issues for a similar bug?"
options:
- label: "I checked and didn't find similar issue"
required: true
- type: checkboxes
id: read-code-of-conduct
attributes:
label: "🏢 Have you read the Code of Conduct?"
options:
- label: "I have read the [Code of Conduct](https://github.com/appwrite/appwrite/blob/HEAD/CODE_OF_CONDUCT.md)"
required: true
-32
View File
@@ -1,32 +0,0 @@
name: "📚 Documentation"
description: "Report an issue related to documentation"
title: "📚 Documentation: "
labels: [documentation]
body:
- type: markdown
attributes:
value: |
Thanks for taking the time to make our documentation better 🙏
- type: textarea
id: issue-description
validations:
required: true
attributes:
label: "💭 Description"
description: "A clear and concise description of what the issue is."
placeholder: "Documentation should not ..."
- type: checkboxes
id: no-duplicate-issues
attributes:
label: "👀 Have you spent some time to check if this issue has been raised before?"
description: "Have you Googled for a similar issue or checked our older issues for a similar documentation issues?"
options:
- label: "I checked and didn't find similar issue"
required: true
- type: checkboxes
id: read-code-of-conduct
attributes:
label: "🏢 Have you read the Code of Conduct?"
options:
- label: "I have read the [Code of Conduct](https://github.com/appwrite/appwrite/blob/HEAD/CODE_OF_CONDUCT.md)"
required: true
-40
View File
@@ -1,40 +0,0 @@
name: "🚀 Feature"
description: "Submit a proposal for a new feature"
title: "🚀 Feature: "
labels: [feature]
body:
- type: markdown
attributes:
value: |
Thanks for taking the time to fill out our feature request form 🙏
- type: textarea
id: feature-description
validations:
required: true
attributes:
label: "🔖 Feature description"
description: "A clear and concise description of what the feature is."
placeholder: "You should add ..."
- type: textarea
id: pitch
validations:
required: true
attributes:
label: "🎤 Pitch"
description: "Please explain why this feature should be implemented and how it would be used. Add examples, if applicable."
placeholder: "In my use-case, ..."
- type: checkboxes
id: no-duplicate-issues
attributes:
label: "👀 Have you spent some time to check if this issue has been raised before?"
description: "Have you Googled for a similar issue or checked our older issues for a similar feature request?"
options:
- label: "I checked and didn't find similar issue"
required: true
- type: checkboxes
id: read-code-of-conduct
attributes:
label: "🏢 Have you read the Code of Conduct?"
options:
- label: "I have read the [Code of Conduct](https://github.com/appwrite/appwrite/blob/HEAD/CODE_OF_CONDUCT.md)"
required: true
-20
View File
@@ -1,20 +0,0 @@
name: "CodeQL"
on: [pull_request]
jobs:
lint:
name: CodeQL
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v3
with:
fetch-depth: 2
- run: git checkout HEAD^2
- name: Run CodeQL
run: |
docker run --rm -v $PWD:/app composer sh -c \
"composer install --profile --ignore-platform-reqs && composer check"
-20
View File
@@ -1,20 +0,0 @@
name: "Linter"
on: [pull_request]
jobs:
lint:
name: Linter
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v3
with:
fetch-depth: 2
- run: git checkout HEAD^2
- name: Run Linter
run: |
docker run --rm -v $PWD:/app composer sh -c \
"composer install --profile --ignore-platform-reqs && composer lint"
+17
View File
@@ -0,0 +1,17 @@
name: Mirror
on:
pull_request_target:
types: [opened]
issues:
types: [opened]
permissions:
issues: write
pull-requests: write
jobs:
redirect:
uses: utopia-php/monorepo/.github/workflows/mirror-redirect.yml@main
with:
package: cache
-117
View File
@@ -1,117 +0,0 @@
name: "Tests"
on: [pull_request]
env:
COMPOSE_PROJECT_NAME: cache
jobs:
build:
name: Build (PHP ${{ matrix.php }})
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
php: ['8.4', '8.5']
steps:
- name: Checkout repository
uses: actions/checkout@v3
with:
fetch-depth: 2
- run: git checkout HEAD^2
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Build tests image
uses: docker/build-push-action@v6
with:
context: .
file: Dockerfile.php-${{ matrix.php }}
tags: cache-tests:${{ matrix.php }}
cache-from: type=gha,scope=php-${{ matrix.php }}
cache-to: type=gha,mode=max,scope=php-${{ matrix.php }}
outputs: type=docker,dest=/tmp/cache-tests-${{ matrix.php }}.tar
- name: Cache tests image
uses: actions/cache@v4
with:
key: cache-tests-${{ matrix.php }}-${{ github.event.pull_request.head.sha }}
path: /tmp/cache-tests-${{ matrix.php }}.tar
unit:
name: Unit (PHP ${{ matrix.php }})
runs-on: ubuntu-latest
needs: build
strategy:
fail-fast: false
matrix:
php: ['8.4', '8.5']
steps:
- name: Checkout repository
uses: actions/checkout@v3
with:
fetch-depth: 2
- run: git checkout HEAD^2
- name: Restore tests image
uses: actions/cache@v4
with:
key: cache-tests-${{ matrix.php }}-${{ github.event.pull_request.head.sha }}
path: /tmp/cache-tests-${{ matrix.php }}.tar
fail-on-cache-miss: true
- name: Start container
run: |
docker load --input /tmp/cache-tests-${{ matrix.php }}.tar
PHP_VERSION=${{ matrix.php }} docker compose up -d --no-deps --wait --wait-timeout 180 tests
- name: Run unit tests
run: docker compose exec -T tests vendor/bin/phpunit --configuration phpunit.xml --testsuite unit
adapter:
name: ${{ matrix.adapter.name }} (PHP ${{ matrix.php }})
runs-on: ubuntu-latest
needs: build
strategy:
fail-fast: false
matrix:
php: ['8.4', '8.5']
adapter:
- { name: Memory, suite: memory, services: '', wait: 0 }
- { name: None, suite: none, services: '', wait: 0 }
- { name: Filesystem, suite: filesystem, services: '', wait: 0 }
- { name: Pool, suite: pool, services: '', wait: 0 }
- { name: CircuitBreaker, suite: circuitbreaker, services: '', wait: 0 }
- { name: Telemetry, suite: telemetry, services: '', wait: 0 }
- { name: Memcached, suite: memcached, services: 'memcached', wait: 3 }
- { name: Hazelcast, suite: hazelcast, services: 'hazelcast', wait: 30 }
- { name: Sharding, suite: sharding, services: 'shardA shardB shardC', wait: 0 }
- { name: Redis, suite: redis, services: 'redis', wait: 0 }
- { name: RedisMultiplexing, suite: redis-multiplexing, services: 'redis', wait: 0 }
- { name: RedisCluster, suite: redis-cluster, services: 'redis-cluster', wait: 0 }
steps:
- name: Checkout repository
uses: actions/checkout@v3
with:
fetch-depth: 2
- run: git checkout HEAD^2
- name: Restore tests image
uses: actions/cache@v4
with:
key: cache-tests-${{ matrix.php }}-${{ github.event.pull_request.head.sha }}
path: /tmp/cache-tests-${{ matrix.php }}.tar
fail-on-cache-miss: true
- name: Start services
run: |
docker load --input /tmp/cache-tests-${{ matrix.php }}.tar
PHP_VERSION=${{ matrix.php }} docker compose up -d --no-deps --wait --wait-timeout 180 tests ${{ matrix.adapter.services }}
if [ "${{ matrix.adapter.wait }}" -gt 0 ]; then sleep ${{ matrix.adapter.wait }}; fi
- name: Run ${{ matrix.adapter.name }} tests
run: docker compose exec -T tests vendor/bin/phpunit --configuration phpunit.xml --testsuite ${{ matrix.adapter.suite }}
+2 -3
View File
@@ -1,5 +1,4 @@
/vendor/
/.idea/
.phpunit.result.cache
tests/data
tests/Cache/E2E/tests/pool/
.phpunit.cache
.DS_Store
-76
View File
@@ -1,76 +0,0 @@
# Contributor Covenant Code of Conduct
## Our Pledge
In the interest of fostering an open and welcoming environment, we as
contributors and maintainers pledge to make participation in our project and
our community a harassment-free experience for everyone, regardless of age, body
size, disability, ethnicity, sex characteristics, gender identity, expression,
level of experience, education, socio-economic status, nationality, personal
appearance, race, religion, or sexual identity and orientation.
## Our Standards
Examples of behavior that contributes to creating a positive environment
include:
- Using welcoming and inclusive language
- Being respectful of differing viewpoints and experiences
- Gracefully accepting constructive criticism
- Focusing on what is best for the community
- Showing empathy towards other community members
Examples of unacceptable behavior by participants include:
- The use of sexualized language or imagery and unwelcome sexual attention or
advances
- Trolling, insulting/derogatory comments, and personal or political attacks
- Public or private harassment
- Publishing others' private information, such as a physical or electronic
address, without explicit permission
- Other conduct which could reasonably be considered inappropriate in a
professional setting
## Our Responsibilities
Project maintainers are responsible for clarifying the standards of acceptable
behavior and are expected to take appropriate and fair corrective action in
response to any instances of unacceptable behavior.
Project maintainers have the right and responsibility to remove, edit, or
reject comments, commits, code, wiki edits, issues, and other contributions
that are not aligned to this Code of Conduct, or to ban temporarily or
permanently any contributor for other behaviors that they deem inappropriate,
threatening, offensive, or harmful.
## Scope
This Code of Conduct applies both within project spaces and in public spaces
when an individual is representing the project or its community. Examples of
representing a project or community include using an official project e-mail
address, posting via an official social media account, or acting as an appointed
representative at an online or offline event. Representation of a project may be
further defined and clarified by project maintainers.
## Enforcement
Instances of abusive, harassing, or otherwise unacceptable behavior may be
reported by contacting the project team at team@appwrite.io. All
complaints will be reviewed and investigated and will result in a response that
is deemed necessary and appropriate to the circumstances. The project team is
obligated to maintain confidentiality with regard to the reporter of an incident.
Further details of specific enforcement policies may be posted separately.
Project maintainers who do not follow or enforce the Code of Conduct in good
faith may face temporary or permanent repercussions as determined by other
members of the project's leadership.
## Attribution
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
available at https://www.contributor-covenant.org/version/1/4/code-of-conduct.html
[homepage]: https://www.contributor-covenant.org
For answers to common questions about this code of conduct, see
https://www.contributor-covenant.org/faq
-101
View File
@@ -1,101 +0,0 @@
# Contributing
We would ❤️ for you to contribute to Utopia-php and help make it better! We want contributing to Utopia-php to be fun, enjoyable, and educational for anyone and everyone. All contributions are welcome, including issues, new docs as well as updates and tweaks, blog posts, workshops, and more.
## How to Start?
If you are worried or don’t know where to start, check out our next section explaining what kind of help we could use and where can you get involved. You can reach out with questions to [Eldad Fux (@eldadfux)](https://twitter.com/eldadfux) or anyone from the [Appwrite team on Discord](https://discord.gg/GSeTUeA). You can also submit an issue, and a maintainer can guide you!
## Code of Conduct
Help us keep Utopia-php open and inclusive. Please read and follow our [Code of Conduct](/CODE_OF_CONDUCT.md).
## Submit a Pull Request 🚀
Branch naming convention is as following
`TYPE-ISSUE_ID-DESCRIPTION`
example:
```
doc-548-submit-a-pull-request-section-to-contribution-guide
```
When `TYPE` can be:
- **feat** - is a new feature
- **doc** - documentation only changes
- **cicd** - changes related to CI/CD system
- **fix** - a bug fix
- **refactor** - code change that neither fixes a bug nor adds a feature
**All PRs must include a commit message with the changes description!**
For the initial start, fork the project and use git clone command to download the repository to your computer. A standard procedure for working on an issue would be to:
1. `git pull`, before creating a new branch, pull the changes from upstream. Your master needs to be up to date.
```
$ git pull
```
2. Create new branch from `master` like: `doc-548-submit-a-pull-request-section-to-contribution-guide`<br/>
```
$ git checkout -b [name_of_your_new_branch]
```
3. Work - commit - repeat ( be sure to be in your branch )
4. Push changes to GitHub
```
$ git push origin [name_of_your_new_branch]
```
5. Submit your changes for review
If you go to your repository on GitHub, you'll see a `Compare & pull request` button. Click on that button.
6. Start a Pull Request
Now submit the pull request and click on `Create pull request`.
7. Get a code review approval/reject
8. After approval, merge your PR
9. GitHub will automatically delete the branch after the merge is done. (they can still be restored).
## Introducing New Features
We would 💖 you to contribute to Utopia-php, but we would also like to make sure Utopia-php is as great as possible and loyal to its vision and mission statement 🙏.
For us to find the right balance, please open an issue explaining your ideas before introducing a new pull request.
This will allow the Utopia-php community to have sufficient discussion about the new feature value and how it fits in the product roadmap and vision.
This is also important for the Utopia-php lead developers to be able to give technical input and different emphasis regarding the feature design and architecture. Some bigger features might need to go through our [RFC process](https://github.com/appwrite/rfc).
## Other Ways to Help
Pull requests are great, but there are many other areas where you can help Utopia-php.
### Blogging & Speaking
Blogging, speaking about, or creating tutorials about one of Utopia-php’s many features is great way to contribute and help our project grow.
### Presenting at Meetups
Presenting at meetups and conferences about your Utopia-php projects. Your unique challenges and successes in building things with Utopia-php can provide great speaking material. We’d love to review your talk abstract/CFP, so get in touch with us if you’d like some help!
### Sending Feedbacks & Reporting Bugs
Sending feedback is a great way for us to understand your different use cases of Utopia-php better. If you had any issues, bugs, or want to share about your experience, feel free to do so on our GitHub issues page or at our [Discord channel](https://discord.gg/GSeTUeA).
### Submitting New Ideas
If you think Utopia-php could use a new feature, please open an issue on our GitHub repository, stating as much information as you can think about your new idea and it's implications. We would also use this issue to gather more information, get more feedback from the community, and have a proper discussion about the new feature.
### Improving Documentation
Submitting documentation updates, enhancements, designs, or bug fixes. Spelling or grammar fixes will be very much appreciated.
### Helping Someone
Searching for Utopia-php, GitHub or StackOverflow and helping someone else who needs help. You can also help by teaching others how to contribute to Utopia-php's repo!
-44
View File
@@ -1,44 +0,0 @@
FROM composer:2.7 as step0
ARG TESTING=false
ENV TESTING=$TESTING
WORKDIR /usr/local/src/
COPY composer.lock /usr/local/src/
COPY composer.json /usr/local/src/
RUN composer update --ignore-platform-reqs --optimize-autoloader \
--no-plugins --no-scripts --prefer-dist
FROM appwrite/utopia-base:php-8.4-2.0.0 as final
ENV PHP_MEMCACHED_VERSION=v3.4.0
LABEL maintainer="team@appwrite.io"
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone
RUN \
apk update \
&& apk add --no-cache make automake autoconf g++ gcc zlib-dev libmemcached-dev docker-cli \
&& rm -rf /var/cache/apk/*
RUN \
# Memcached Extension
git clone --branch $PHP_MEMCACHED_VERSION https://github.com/php-memcached-dev/php-memcached.git && \
cd php-memcached && \
phpize && \
./configure && \
make && make install && \
cd ..
RUN echo extension=memcached.so >> /usr/local/etc/php/conf.d/memcached.ini
WORKDIR /usr/src/code
COPY ./ /usr/src/code
COPY --from=step0 /usr/local/src/vendor /usr/src/code/vendor
CMD [ "tail", "-f", "/dev/null" ]
-44
View File
@@ -1,44 +0,0 @@
FROM composer:2.7 as step0
ARG TESTING=false
ENV TESTING=$TESTING
WORKDIR /usr/local/src/
COPY composer.lock /usr/local/src/
COPY composer.json /usr/local/src/
RUN composer update --ignore-platform-reqs --optimize-autoloader \
--no-plugins --no-scripts --prefer-dist
FROM appwrite/utopia-base:php-8.5-2.0.0 as final
ENV PHP_MEMCACHED_VERSION=v3.4.0
LABEL maintainer="team@appwrite.io"
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone
RUN \
apk update \
&& apk add --no-cache make automake autoconf g++ gcc zlib-dev libmemcached-dev docker-cli \
&& rm -rf /var/cache/apk/*
RUN \
# Memcached Extension
git clone --branch $PHP_MEMCACHED_VERSION https://github.com/php-memcached-dev/php-memcached.git && \
cd php-memcached && \
phpize && \
./configure && \
make && make install && \
cd ..
RUN echo extension=memcached.so >> /usr/local/etc/php/conf.d/memcached.ini
WORKDIR /usr/src/code
COPY ./ /usr/src/code
COPY --from=step0 /usr/local/src/vendor /usr/src/code/vendor
CMD [ "tail", "-f", "/dev/null" ]
+32 -12
View File
@@ -1,6 +1,8 @@
# Utopia Cache
[![Build Status](https://travis-ci.org/utopia-php/cache.svg?branch=master)](https://travis-ci.com/utopia-php/cache)
> [!IMPORTANT]
> This repository is a read-only mirror of the [utopia-php monorepo](https://github.com/utopia-php/monorepo). Development happens in [`packages/cache`](https://github.com/utopia-php/monorepo/tree/main/packages/cache) — please open issues and pull requests there.
![Total Downloads](https://img.shields.io/packagist/dt/utopia-php/cache.svg)
[![Discord](https://img.shields.io/discord/564160730845151244?label=discord)](https://appwrite.io/discord)
@@ -8,9 +10,10 @@ Utopia framework cache library is simple and lite library for managing applicati
Although this library is part of the [Utopia Framework](https://github.com/utopia-php/framework) project it is dependency free and can be used as standalone with any other PHP project or framework.
## Getting Started
## Getting started
Install using Composer:
Install using composer:
```bash
composer require utopia-php/cache
```
@@ -39,24 +42,41 @@ if(!$data) {
echo $data;
```
## Contribute
## Adapters
Currently we support only a Filesystem adapter for usage as a cache storage, send a pull request to add redis, memcached or any other storage adapter you might need to use with this library.
| Adapter | Notes |
| --- | --- |
| `Filesystem` | Files on disk, with optional streaming reads. |
| `Memory` | In-process array, useful in tests. |
| `None` | Stores nothing, for turning caching off. |
| `Redis` | phpredis, with reconnect and leases. |
| `Redis\Multiplexing` | Many Swoole coroutines over one Redis connection — see [the multiplexing guide](docs/multiplexing.md). |
| `RedisCluster` | phpredis against a Redis cluster. |
| `Memcached` | Memcached over the binary protocol. |
| `Hazelcast` | Hazelcast over its Memcached protocol. |
| `Sharding` | Spreads keys across several adapters. |
| `Pool` | Checks an adapter out of a `utopia-php/pools` pool per call. |
| `CircuitBreaker` | Wraps an adapter so a failing cache stops being called. |
## System Requirements
## System requirements
Utopia Framework requires PHP 8.0 or later. We recommend using the latest PHP version whenever possible.
The library requires PHP 8.4 or later. The Redis, Memcached and Hazelcast adapters need the matching extension — see the `suggest` block in `composer.json`.
## Tests
To run all unit tests, use the following Docker command:
The unit tier needs nothing running:
`docker compose exec tests vendor/bin/phpunit --configuration phpunit.xml tests`
```bash
composer test
```
To run static code analysis, use the following Psalm command:
`docker-compose exec php8 vendor/bin/psalm --show-info=true`
The end-to-end tier runs on the host against this package's services:
```bash
docker compose up -d --wait
composer test:e2e
docker compose down -v
```
## Copyright and license
+34 -19
View File
@@ -2,36 +2,51 @@
"name": "utopia-php/cache",
"description": "A simple cache library to manage application cache storing, loading and purging",
"type": "library",
"keywords": ["php","framework", "upf", "utopia", "cache"],
"keywords": [
"php",
"framework",
"upf",
"utopia",
"cache"
],
"license": "MIT",
"minimum-stability": "stable",
"scripts": {
"check": "./vendor/bin/phpstan analyse --level max src tests",
"lint": "./vendor/bin/pint --test",
"format": "./vendor/bin/pint"
},
"authors": [
{
"name": "Team Appwrite",
"email": "team@appwrite.io"
}
],
"autoload": {
"psr-4": {"Utopia\\Cache\\": "src/Cache"}
"psr-4": {
"Utopia\\Cache\\": "src/Cache"
}
},
"autoload-dev": {
"psr-4": {"Utopia\\Tests\\": "tests/Cache"}
"psr-4": {
"Utopia\\Tests\\": "tests/Cache"
}
},
"require": {
"php": ">=8.4",
"ext-json": "*",
"ext-memcached": "*",
"ext-redis": "*",
"utopia-php/circuit-breaker": "0.3.*",
"utopia-php/pools": "2.*",
"utopia-php/telemetry": "*"
"utopia-php/circuit-breaker": "^0.3",
"utopia-php/pools": "^2.0",
"utopia-php/telemetry": "^0.4"
},
"require-dev": {
"laravel/pint": "1.2.*",
"phpstan/phpstan": "^1.12",
"phpunit/phpunit": "^9.3",
"swoole/ide-helper": "^6.0",
"vimeo/psalm": "4.13.1"
"swoole/ide-helper": "^6.0"
},
"suggest": {
"ext-redis": "Required for the Redis, RedisCluster and Sharding adapters.",
"ext-memcached": "Required for the Memcached and Hazelcast adapters.",
"ext-swoole": "Required for the multiplexing Redis adapter (>=6.0)."
},
"scripts": {
"test": "phpunit --testsuite unit",
"test:e2e": "phpunit --testsuite e2e"
},
"minimum-stability": "stable",
"prefer-stable": true,
"config": {
"allow-plugins": {
"php-http/discovery": true,
Generated
+126 -3557
View File
File diff suppressed because it is too large. Load diff
+58 -85
View File
@@ -1,100 +1,73 @@
name: cache
x-redis: &redis
image: redis:7-alpine
command: ["redis-server", "--save", "", "--appendonly", "no"]
healthcheck:
test: ["CMD", "redis-cli", "ping"]
interval: 1s
timeout: 1s
retries: 30
services:
tests:
container_name: tests
image: cache-tests:${PHP_VERSION:-8.4}
build:
context: .
dockerfile: Dockerfile.php-${PHP_VERSION:-8.4}
networks:
- database
volumes:
- ./phpunit.xml:/usr/src/code/phpunit.xml
- ./src:/usr/src/code/src
- ./tests:/usr/src/code/tests
- /var/run/docker.sock:/var/run/docker.sock
depends_on:
- redis
- redis-cluster
- hazelcast
- memcached
- shardA
- shardB
- shardC
redis:
image: redis:6.0-alpine
<<: *redis
container_name: cache-redis
healthcheck:
test: ["CMD", "redis-cli", "ping"]
interval: 2s
timeout: 3s
retries: 20
networks:
- database
ports:
- "16381:6379"
shard-a:
<<: *redis
container_name: cache-shard-a
ports:
- "16382:6379"
shard-b:
<<: *redis
container_name: cache-shard-b
ports:
- "16383:6379"
shard-c:
<<: *redis
container_name: cache-shard-c
ports:
- "16384:6379"
# The cluster advertises the addresses clients must dial, so its published
# ports have to match the ones inside the container.
redis-cluster:
image: grokzen/redis-cluster:7.0.10
container_name: redis-cluster
hostname: redis-cluster
container_name: cache-redis-cluster
environment:
- IP=redis-cluster
IP: 127.0.0.1
INITIAL_PORT: 17000
ports:
- "17000-17005:17000-17005"
healthcheck:
test: ["CMD-SHELL", "redis-cli -p 7000 cluster info | grep -q cluster_state:ok"]
interval: 3s
timeout: 5s
retries: 30
start_period: 10s
networks:
- database
hazelcast:
image: hazelcast/hazelcast:5.3.7
environment:
HZ_NETWORK_MEMCACHEPROTOCOL_ENABLED: "true"
container_name: hazelcast
networks:
- database
test: ["CMD-SHELL", "redis-cli -p 17000 cluster info | grep -q cluster_state:ok"]
interval: 2s
timeout: 3s
retries: 60
memcached:
image: memcached:1.6.17-alpine
container_name: memcached
networks:
- database
shardA:
image: redis:6.0-alpine
container_name: shardA
container_name: cache-memcached
ports:
- "11212:11211"
healthcheck:
test: ["CMD", "redis-cli", "ping"]
test: ["CMD-SHELL", "echo version | nc -w 1 127.0.0.1 11211 | grep -q VERSION"]
interval: 1s
timeout: 2s
retries: 30
hazelcast:
image: hazelcast/hazelcast:5.3.7
container_name: cache-hazelcast
environment:
HZ_NETWORK_MEMCACHEPROTOCOL_ENABLED: "true"
ports:
- "15701:5701"
healthcheck:
test: ["CMD-SHELL", "curl -sf http://127.0.0.1:5701/hazelcast/health/node-state | grep -q ACTIVE"]
interval: 2s
timeout: 3s
retries: 20
networks:
- database
shardB:
image: redis:6.0-alpine
container_name: shardB
healthcheck:
test: ["CMD", "redis-cli", "ping"]
interval: 2s
timeout: 3s
retries: 20
networks:
- database
shardC:
image: redis:6.0-alpine
container_name: shardC
healthcheck:
test: ["CMD", "redis-cli", "ping"]
interval: 2s
timeout: 3s
retries: 20
networks:
- database
networks:
database:
retries: 60
+4 -4
View File
@@ -1,7 +1,7 @@
includes:
- ../../phpstan.neon
parameters:
level: max
paths:
- src
- tests
treatPhpDocTypesAsCertain: false
scanDirectories:
- vendor/swoole/ide-helper
+12 -48
View File
@@ -1,56 +1,20 @@
<phpunit backupGlobals="false"
backupStaticAttributes="false"
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/12.0/phpunit.xsd"
bootstrap="vendor/autoload.php"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false"
>
cacheDirectory=".phpunit.cache">
<testsuites>
<testsuite name="unit">
<directory>./tests/Cache/Unit</directory>
<directory>tests/Cache/Unit</directory>
</testsuite>
<testsuite name="memory">
<file>./tests/Cache/E2E/MemoryTest.php</file>
</testsuite>
<testsuite name="none">
<file>./tests/Cache/E2E/NoneTest.php</file>
</testsuite>
<testsuite name="filesystem">
<file>./tests/Cache/E2E/FilesystemTest.php</file>
</testsuite>
<testsuite name="pool">
<file>./tests/Cache/E2E/PoolTest.php</file>
</testsuite>
<testsuite name="circuitbreaker">
<file>./tests/Cache/E2E/CircuitBreakerTest.php</file>
</testsuite>
<testsuite name="telemetry">
<file>./tests/Cache/E2E/TelemetryTest.php</file>
</testsuite>
<testsuite name="memcached">
<file>./tests/Cache/E2E/MemcachedTest.php</file>
</testsuite>
<testsuite name="hazelcast">
<file>./tests/Cache/E2E/HazelcastTest.php</file>
</testsuite>
<testsuite name="sharding">
<file>./tests/Cache/E2E/ShardingTest.php</file>
</testsuite>
<testsuite name="redis">
<directory>./tests/Cache/E2E/Redis</directory>
<exclude>./tests/Cache/E2E/Redis/RedisClusterTest.php</exclude>
<exclude>./tests/Cache/E2E/Redis/MultiplexingTest.php</exclude>
<exclude>./tests/Cache/E2E/Redis/LeasableMultiplexingTest.php</exclude>
</testsuite>
<testsuite name="redis-cluster">
<file>./tests/Cache/E2E/Redis/RedisClusterTest.php</file>
</testsuite>
<testsuite name="redis-multiplexing">
<file>./tests/Cache/E2E/Redis/MultiplexingTest.php</file>
<file>./tests/Cache/E2E/Redis/LeasableMultiplexingTest.php</file>
<testsuite name="e2e">
<directory>tests/Cache/E2E</directory>
</testsuite>
</testsuites>
<source>
<include>
<directory>src</directory>
</include>
</source>
</phpunit>
-15
View File
@@ -1,15 +0,0 @@
<?xml version="1.0"?>
<psalm
errorLevel="5"
resolveFromConfigFile="true"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="https://getpsalm.org/schema/config"
xsi:schemaLocation="https://getpsalm.org/schema/config vendor/vimeo/psalm/config.xsd"
>
<projectFiles>
<directory name="src" />
<ignoreFiles>
<directory name="vendor" />
</ignoreFiles>
</projectFiles>
</psalm>
+2 -21
View File
@@ -1,19 +1,18 @@
<?php
declare(strict_types=1);
namespace Utopia\Cache;
interface Adapter
{
/**
* @param string $key
* @param int $ttl time in seconds
* @param string $hash optional
* @return mixed
*/
public function load(string $key, int $ttl, string $hash = ''): mixed;
/**
* @param string $key
* @param string|array<int|string, mixed> $data
* @param string $hash optional
* @return bool|string|array<int|string, mixed>
@@ -21,43 +20,25 @@ interface Adapter
public function save(string $key, array|string $data, string $hash = ''): bool|string|array;
/**
* @param string $key
* @param string $hash optional
* @return bool
*/
public function touch(string $key, string $hash = ''): bool;
/**
* @param string $key
* @return string[]
*/
public function list(string $key): array;
/**
* @param string $key
* @param string $hash optional
* @return bool
*/
public function purge(string $key, string $hash = ''): bool;
/**
* @return bool
*/
public function flush(): bool;
/**
* @return bool
*/
public function ping(): bool;
/**
* @return int
*/
public function getSize(): int;
/**
* @param string|null $key
* @return string
*/
public function getName(?string $key = null): string;
}
+3 -6
View File
@@ -12,23 +12,20 @@ class CircuitBreaker implements Adapter, Feature\Leasable, Feature\Telemetry
public function __construct(
private readonly Adapter $adapter,
private readonly UtopiaCircuitBreaker $breaker,
) {
}
) {}
/**
* Forward method calls to the internal adapter through the circuit breaker.
*
* Required because __call() can't be used to implement abstract methods.
*
* @param string $method
* @param array<mixed> $args
* @return mixed
*/
public function delegate(string $method, array $args, mixed $fallback): mixed
{
return $this->breaker->call(
open: fn (): mixed => $fallback,
close: fn (): mixed => $this->adapter->{$method}(...$args),
open: fn(): mixed => $fallback,
close: fn(): mixed => $this->adapter->{$method}(...$args),
);
}
+18 -66
View File
@@ -1,5 +1,7 @@
<?php
declare(strict_types=1);
namespace Utopia\Cache\Adapter;
use Exception;
@@ -7,55 +9,34 @@ use Utopia\Cache\Adapter;
class Filesystem implements Adapter
{
/**
* @var string
*/
protected $path = '';
/**
* @var bool
*/
protected bool $streaming = false;
/**
* Filesystem constructor.
*
* @param string $path
* @param bool $streaming
*/
public function __construct(string $path, bool $streaming = false)
{
$this->path = $path;
$this->streaming = $streaming;
}
public function __construct(protected string $path, protected bool $streaming = false) {}
/**
* @param string $key
* @param int $ttl time in seconds
* @param string $hash optional
* @return mixed
*/
public function load(string $key, int $ttl, string $hash = ''): mixed
{
$file = $this->getPath($key);
if (\file_exists($file) && (\filemtime($file) + $ttl > \time())) { // Cache is valid
if (file_exists($file) && (filemtime($file) + $ttl > time())) { // Cache is valid
if ($this->streaming) {
return \fopen($file, 'rb');
return fopen($file, 'rb');
}
return \file_get_contents($file);
return file_get_contents($file);
}
return false;
}
/**
* @param string $key
* @param array<int|string, mixed>|string $data
* @param string $hash optional
* @return bool|string|array<int|string, mixed>
*
* @throws Exception
*/
public function save(string $key, array|string $data, string $hash = ''): bool|string|array
@@ -65,24 +46,20 @@ class Filesystem implements Adapter
}
$file = $this->getPath($key);
$dir = dirname($file);
$dir = \dirname($file);
try {
if (! file_exists($dir)) {
if (! mkdir($dir, 0755, true) && ! file_exists($dir)) {
throw new Exception("Can't create directory {$dir}");
}
if (!file_exists($dir) && (!mkdir($dir, 0755, true) && ! file_exists($dir))) {
throw new Exception("Can't create directory {$dir}");
}
return (\file_put_contents($file, $data, LOCK_EX)) ? $data : false;
return (file_put_contents($file, $data, LOCK_EX)) ? $data : false;
} catch (Exception $e) {
throw new Exception($e->getMessage());
throw new Exception($e->getMessage(), $e->getCode(), $e);
}
}
/**
* @param string $key
* @param string $hash optional
* @return bool
*/
public function touch(string $key, string $hash = ''): bool
{
@@ -98,7 +75,6 @@ class Filesystem implements Adapter
}
/**
* @param string $key
* @return string[]
*/
public function list(string $key): array
@@ -107,32 +83,24 @@ class Filesystem implements Adapter
}
/**
* @param string $key
* @param string $hash optional
* @return bool
*/
public function purge(string $key, string $hash = ''): bool
{
$file = $this->getPath($key);
if (\file_exists($file)) {
return \unlink($file);
if (file_exists($file)) {
return unlink($file);
}
return false;
}
/**
* @return bool
*/
public function flush(): bool
{
return $this->deleteDirectory($this->path);
}
/**
* @return bool
*/
public function ping(): bool
{
return file_exists($this->path) && is_writable($this->path) && is_readable($this->path);
@@ -140,26 +108,20 @@ class Filesystem implements Adapter
/**
* Returning root directory size in bytes
*
* @return int
*/
public function getSize(): int
{
try {
return $this->getDirectorySize(dirname($this->path));
return $this->getDirectorySize($this->path);
} catch (Exception) {
return 0;
}
}
/**
* @param string $dir
* @return int
*/
private function getDirectorySize(string $dir): int
{
$size = 0;
$normalizedPath = rtrim($dir, '/').'/*';
$normalizedPath = rtrim($dir, '/') . '/*';
$paths = glob($normalizedPath, GLOB_NOSORT);
if ($paths === false) {
@@ -178,18 +140,12 @@ class Filesystem implements Adapter
return $size;
}
/**
* @param string $filename
* @return string
*/
public function getPath(string $filename): string
{
return $this->path.DIRECTORY_SEPARATOR.$filename;
return $this->path . DIRECTORY_SEPARATOR . $filename;
}
/**
* @param string $path
* @return bool
*
* @throws Exception
*/
@@ -199,11 +155,11 @@ class Filesystem implements Adapter
throw new Exception("$path must be a directory");
}
if (substr($path, strlen($path) - 1, 1) != '/') {
if (substr($path, \strlen($path) - 1, 1) !== '/') {
$path .= '/';
}
$files = glob($path.'*', GLOB_MARK);
$files = glob($path . '*', GLOB_MARK);
if (! $files) {
throw new Exception('Error happened during glob');
@@ -220,10 +176,6 @@ class Filesystem implements Adapter
return rmdir($path);
}
/**
* @param string|null $key
* @return string
*/
public function getName(?string $key = null): string
{
return 'filesystem';
+16 -48
View File
@@ -8,23 +8,14 @@ use Utopia\Cache\Feature\Retryable;
class Hazelcast implements Adapter, Retryable
{
/**
* @var Client
*/
protected Client $memcached;
private int $maxRetries = 0;
private int $retryDelay = 1000; // milliseconds
public function __construct(Client $memcached)
{
$this->memcached = $memcached;
}
public function __construct(protected Client $memcached) {}
/**
* @param int $maxRetries (0-10)
* @return self
*/
public function setMaxRetries(int $maxRetries): self
{
@@ -35,7 +26,6 @@ class Hazelcast implements Adapter, Retryable
/**
* @param int $retryDelay time in milliseconds
* @return self
*/
public function setRetryDelay(int $retryDelay): self
{
@@ -45,19 +35,17 @@ class Hazelcast implements Adapter, Retryable
}
/**
* @param string $key
* @param int $ttl time in seconds
* @param string $hash optional
* @return mixed
*/
public function load(string $key, int $ttl, string $hash = ''): mixed
{
$cache = $this->execute(fn () => $this->memcached->get($key));
if (is_string($cache)) {
$cache = $this->execute(fn(): mixed => $this->memcached->get($key));
if (\is_string($cache)) {
$cache = json_decode($cache, true);
}
if (! is_array($cache)) {
if (! \is_array($cache)) {
return false;
}
@@ -69,14 +57,13 @@ class Hazelcast implements Adapter, Retryable
}
/**
* @param string $key
* @param array<int|string, mixed>|string $data
* @param string $hash optional
* @return bool|string|array<int|string, mixed>
*/
public function save(string $key, array|string $data, string $hash = ''): bool|string|array
{
if (empty($key) || empty($data)) {
if ($key === '' || $key === '0' || empty($data)) {
return false;
}
@@ -85,32 +72,29 @@ class Hazelcast implements Adapter, Retryable
'data' => $data,
];
return ($this->execute(fn () => $this->memcached->set($key, json_encode($cache)))) ? $data : false;
return ($this->execute(fn(): bool => $this->memcached->set($key, json_encode($cache)))) ? $data : false;
}
/**
* @param string $key
* @param string $hash optional
* @return bool
*/
public function touch(string $key, string $hash = ''): bool
{
$cache = $this->execute(fn () => $this->memcached->get($key));
if (is_string($cache)) {
$cache = $this->execute(fn(): mixed => $this->memcached->get($key));
if (\is_string($cache)) {
$cache = json_decode($cache, true);
}
if (! is_array($cache)) {
if (! \is_array($cache)) {
return false;
}
$cache['time'] = time();
return (bool) $this->execute(fn () => $this->memcached->set($key, json_encode($cache)));
return (bool) $this->execute(fn(): bool => $this->memcached->set($key, json_encode($cache)));
}
/**
* @param string $key
* @return string[]
*/
public function list(string $key): array
@@ -119,13 +103,11 @@ class Hazelcast implements Adapter, Retryable
}
/**
* @param string $key
* @param string $hash optional
* @return bool
*/
public function purge(string $key, string $hash = ''): bool
{
return (bool) $this->execute(fn () => $this->memcached->delete($key));
return (bool) $this->execute(fn(): bool => $this->memcached->delete($key));
}
/**
@@ -137,24 +119,19 @@ class Hazelcast implements Adapter, Retryable
return false;
}
/**
* @return bool
*/
public function ping(): bool
{
try {
$statuses = $this->execute(fn () => $this->memcached->getServerList());
$statuses = $this->execute(fn(): array => $this->memcached->getServerList());
return ! empty($statuses);
} catch (\MemcachedException $e) {
} catch (\MemcachedException) {
return false;
}
}
/**
* Returning total number of keys
*
* @return int
*/
public function getSize(): int
{
@@ -162,7 +139,7 @@ class Hazelcast implements Adapter, Retryable
$servers = $this->memcached->getServerList();
if (! empty($servers)) {
$stats = $this->memcached->getStats();
$key = $servers[0]['host'].':'.$servers[0]['port'];
$key = $servers[0]['host'] . ':' . $servers[0]['port'];
if (isset($stats[$key])) {
$size = $stats[$key]['total_items'] ?? 0;
}
@@ -171,25 +148,16 @@ class Hazelcast implements Adapter, Retryable
return $size;
}
/**
* @return string
*/
public function getName(?string $key = null): string
{
return 'hazelcast';
}
/**
* @return int
*/
public function getMaxRetries(): int
{
return $this->maxRetries;
}
/**
* @return int
*/
public function getRetryDelay(): int
{
return $this->retryDelay;
@@ -211,7 +179,7 @@ class Hazelcast implements Adapter, Retryable
while ($attempts < $maxAttempts) {
$result = $callback();
if ($result === false && in_array($this->memcached->getResultCode(), [
if ($result === false && \in_array($this->memcached->getResultCode(), [
\Memcached::RES_HOST_LOOKUP_FAILURE,
\Memcached::RES_UNKNOWN_READ_FAILURE,
\Memcached::RES_WRITE_FAILURE,
@@ -226,7 +194,7 @@ class Hazelcast implements Adapter, Retryable
$attempts++;
if ($attempts >= $maxAttempts) {
throw new \MemcachedException('Hazelcast connection failed after '.$attempts.' attempts. Error: '.$this->memcached->getResultMessage());
throw new \MemcachedException('Hazelcast connection failed after ' . $attempts . ' attempts. Error: ' . $this->memcached->getResultMessage());
}
usleep($this->retryDelay * 1000);
+13 -53
View File
@@ -8,28 +8,16 @@ use Utopia\Cache\Feature\Retryable;
class Memcached implements Adapter, Retryable
{
/**
* @var Client
*/
protected Client $memcached;
private int $maxRetries = 0;
private int $retryDelay = 1000; // milliseconds
/**
* Memcached constructor.
*
* @param Client $memcached
*/
public function __construct(Client $memcached)
{
$this->memcached = $memcached;
}
public function __construct(protected Client $memcached) {}
/**
* @param int $maxRetries (0-10)
* @return self
*/
public function setMaxRetries(int $maxRetries): self
{
@@ -40,7 +28,6 @@ class Memcached implements Adapter, Retryable
/**
* @param int $retryDelay time in milliseconds
* @return self
*/
public function setRetryDelay(int $retryDelay): self
{
@@ -50,15 +37,12 @@ class Memcached implements Adapter, Retryable
}
/**
* @param string $key
* @param int $ttl
* @param string $hash optional
* @return mixed
*/
public function load(string $key, int $ttl, string $hash = ''): mixed
{
/** @var array{time: int, data: string}|false */
$cache = $this->execute(fn () => $this->memcached->get($key));
$cache = $this->execute(fn(): mixed => $this->memcached->get($key));
if ($cache === false) {
return false;
}
@@ -71,45 +55,41 @@ class Memcached implements Adapter, Retryable
}
/**
* @param string $key
* @param array<int|string, mixed>|string $data
* @param string $hash optional
* @return bool|string|array<int|string, mixed>
*/
public function save(string $key, array|string $data, string $hash = ''): bool|string|array
{
if (empty($key) || empty($data)) {
if ($key === '' || $key === '0' || empty($data)) {
return false;
}
$cache = [
'time' => \time(),
'time' => time(),
'data' => $data,
];
return $this->execute(fn () => $this->memcached->set($key, $cache)) ? $data : false;
return $this->execute(fn(): bool => $this->memcached->set($key, $cache)) ? $data : false;
}
/**
* @param string $key
* @param string $hash optional
* @return bool
*/
public function touch(string $key, string $hash = ''): bool
{
/** @var array{time: int, data: string|array<int|string, mixed>}|false */
$cache = $this->execute(fn () => $this->memcached->get($key));
$cache = $this->execute(fn(): mixed => $this->memcached->get($key));
if ($cache === false) {
return false;
}
$cache['time'] = time();
return (bool) $this->execute(fn () => $this->memcached->set($key, $cache));
return (bool) $this->execute(fn(): bool => $this->memcached->set($key, $cache));
}
/**
* @param string $key
* @return string[]
*/
public function list(string $key): array
@@ -118,41 +98,31 @@ class Memcached implements Adapter, Retryable
}
/**
* @param string $key
* @param string $hash optional
* @return bool
*/
public function purge(string $key, string $hash = ''): bool
{
return (bool) $this->execute(fn () => $this->memcached->delete($key));
return (bool) $this->execute(fn(): bool => $this->memcached->delete($key));
}
/**
* @return bool
*/
public function flush(): bool
{
return (bool) $this->execute(fn () => $this->memcached->flush());
return (bool) $this->execute(fn(): bool => $this->memcached->flush());
}
/**
* @return bool
*/
public function ping(): bool
{
try {
$statuses = $this->memcached->getStats();
return ! empty($statuses);
} catch (\MemcachedException $e) {
} catch (\MemcachedException) {
return false;
}
}
/**
* Returning total number of keys
*
* @return int
*/
public function getSize(): int
{
@@ -160,7 +130,7 @@ class Memcached implements Adapter, Retryable
$servers = $this->memcached->getServerList();
if (! empty($servers)) {
$stats = $this->memcached->getStats();
$key = $servers[0]['host'].':'.$servers[0]['port'];
$key = $servers[0]['host'] . ':' . $servers[0]['port'];
if (isset($stats[$key])) {
$size = $stats[$key]['curr_items'] ?? 0;
}
@@ -169,26 +139,16 @@ class Memcached implements Adapter, Retryable
return $size;
}
/**
* @param string|null $key
* @return string
*/
public function getName(?string $key = null): string
{
return 'memcached';
}
/**
* @return int
*/
public function getMaxRetries(): int
{
return $this->maxRetries;
}
/**
* @return int
*/
public function getRetryDelay(): int
{
return $this->retryDelay;
@@ -210,7 +170,7 @@ class Memcached implements Adapter, Retryable
while ($attempts < $maxAttempts) {
$result = $callback();
if ($result === false && in_array($this->memcached->getResultCode(), [
if ($result === false && \in_array($this->memcached->getResultCode(), [
\Memcached::RES_HOST_LOOKUP_FAILURE,
\Memcached::RES_UNKNOWN_READ_FAILURE,
\Memcached::RES_WRITE_FAILURE,
@@ -225,7 +185,7 @@ class Memcached implements Adapter, Retryable
$attempts++;
if ($attempts >= $maxAttempts) {
throw new \MemcachedException('Memcached connection failed after '.$attempts.' attempts. Error: '.$this->memcached->getResultMessage());
throw new \MemcachedException('Memcached connection failed after ' . $attempts . ' attempts. Error: ' . $this->memcached->getResultMessage());
}
usleep($this->retryDelay * 1000);
+6 -34
View File
@@ -12,21 +12,11 @@ class Memory implements Adapter
public $store = [];
/**
* Memory constructor.
*/
public function __construct()
{
}
/**
* @param string $key
* @param int $ttl
* @param string $hash optional
* @return mixed
*/
public function load(string $key, int $ttl, string $hash = ''): mixed
{
if (! empty($key) && isset($this->store[$key])) {
if ($key !== '' && $key !== '0' && isset($this->store[$key])) {
/** @var array{time: int, data: string} */
$saved = $this->store[$key];
@@ -37,19 +27,18 @@ class Memory implements Adapter
}
/**
* @param string $key
* @param array<int|string, mixed>|string $data
* @param string $hash optional
* @return bool|string|array<int|string, mixed>
*/
public function save(string $key, array|string $data, string $hash = ''): bool|string|array
{
if (empty($key) || empty($data)) {
if ($key === '' || $key === '0' || empty($data)) {
return false;
}
$saved = [
'time' => \time(),
'time' => time(),
'data' => $data,
];
@@ -59,13 +48,11 @@ class Memory implements Adapter
}
/**
* @param string $key
* @param string $hash optional
* @return bool
*/
public function touch(string $key, string $hash = ''): bool
{
if (empty($key) || ! isset($this->store[$key])) {
if ($key === '' || $key === '0' || ! isset($this->store[$key])) {
return false;
}
@@ -78,7 +65,6 @@ class Memory implements Adapter
}
/**
* @param string $key
* @return string[]
*/
public function list(string $key): array
@@ -87,13 +73,11 @@ class Memory implements Adapter
}
/**
* @param string $key
* @param string $hash optional
* @return bool
*/
public function purge(string $key, string $hash = ''): bool
{
if (! empty($key) && isset($this->store[$key])) { // if a key is passed and it exists in cache
if ($key !== '' && $key !== '0' && isset($this->store[$key])) { // if a key is passed and it exists in cache
unset($this->store[$key]);
return true;
@@ -102,9 +86,6 @@ class Memory implements Adapter
return false;
}
/**
* @return bool
*/
public function flush(): bool
{
$this->store = [];
@@ -112,9 +93,6 @@ class Memory implements Adapter
return true;
}
/**
* @return bool
*/
public function ping(): bool
{
return true;
@@ -122,18 +100,12 @@ class Memory implements Adapter
/**
* Returning total number of keys
*
* @return int
*/
public function getSize(): int
{
return count($this->store);
return \count($this->store);
}
/**
* @param string|null $key
* @return string
*/
public function getName(?string $key = null): string
{
return 'memory';
+2 -29
View File
@@ -1,5 +1,7 @@
<?php
declare(strict_types=1);
namespace Utopia\Cache\Adapter;
use Utopia\Cache\Adapter;
@@ -7,17 +9,7 @@ use Utopia\Cache\Adapter;
class None implements Adapter
{
/**
* None constructor.
*/
public function __construct()
{
}
/**
* @param string $key
* @param int $ttl
* @param string $hash optional
* @return mixed
*/
public function load(string $key, int $ttl, string $hash = ''): mixed
{
@@ -25,7 +17,6 @@ class None implements Adapter
}
/**
* @param string $key
* @param array<int|string, mixed>|string $data
* @param string $hash optional
* @return bool|string|array<int|string, mixed>
@@ -36,9 +27,7 @@ class None implements Adapter
}
/**
* @param string $key
* @param string $hash optional
* @return bool
*/
public function touch(string $key, string $hash = ''): bool
{
@@ -46,7 +35,6 @@ class None implements Adapter
}
/**
* @param string $key
* @return string[]
*/
public function list(string $key): array
@@ -55,43 +43,28 @@ class None implements Adapter
}
/**
* @param string $key
* @param string $hash optional
* @return bool
*/
public function purge(string $key, string $hash = ''): bool
{
return true;
}
/**
* @return bool
*/
public function flush(): bool
{
return true;
}
/**
* @return bool
*/
public function ping(): bool
{
return true;
}
/**
* @return int
*/
public function getSize(): int
{
return 0;
}
/**
* @param string|null $key
* @return string
*/
public function getName(?string $key = null): string
{
return 'none';
+8 -23
View File
@@ -8,23 +8,16 @@ use Utopia\Pools\Pool as UtopiaPool;
class Pool implements Adapter, Leasable
{
/**
* @var UtopiaPool<covariant Adapter>
*/
protected UtopiaPool $pool;
/**
* @param UtopiaPool<covariant Adapter> $pool The pool to use for connections. Must contain instances of Adapter.
*
* @throws \Exception
*/
public function __construct(UtopiaPool $pool)
public function __construct(protected UtopiaPool $pool)
{
$this->pool = $pool;
$this->pool->use(function (mixed $resource) {
$this->pool->use(function (mixed $resource): void {
if (! ($resource instanceof Adapter)) {
throw new \Exception('Pool must contain instances of '.Adapter::class);
throw new \Exception('Pool must contain instances of ' . Adapter::class);
}
});
}
@@ -34,15 +27,11 @@ class Pool implements Adapter, Leasable
*
* Required because __call() can't be used to implement abstract methods.
*
* @param string $method
* @param array<mixed> $args
* @return mixed
*/
public function delegate(string $method, array $args): mixed
{
return $this->pool->use(function (Adapter $adapter) use ($method, $args) {
return $adapter->{$method}(...$args);
});
return $this->pool->use(fn(Adapter $adapter) => $adapter->{$method}(...$args));
}
public function load(string $key, int $ttl, string $hash = ''): mixed
@@ -62,9 +51,7 @@ class Pool implements Adapter, Leasable
public function getGeneration(string $key): string
{
return $this->pool->use(function (Adapter $adapter) use ($key): string {
return $adapter instanceof Leasable ? $adapter->getGeneration($key) : '0';
});
return $this->pool->use(fn(Adapter $adapter): string => $adapter instanceof Leasable ? $adapter->getGeneration($key) : '0');
}
public function saveWithLease(string $key, array|string $data, string $hash, string $generation): bool|string|array
@@ -72,11 +59,9 @@ class Pool implements Adapter, Leasable
/**
* @var bool|string|array<mixed> $result
*/
$result = $this->pool->use(function (Adapter $adapter) use ($key, $data, $hash, $generation) {
return $adapter instanceof Leasable
? $adapter->saveWithLease($key, $data, $hash, $generation)
: $adapter->save($key, $data, $hash);
});
$result = $this->pool->use(fn(Adapter $adapter): bool|string|array => $adapter instanceof Leasable
? $adapter->saveWithLease($key, $data, $hash, $generation)
: $adapter->save($key, $data, $hash));
return $result;
}
+38 -71
View File
@@ -13,29 +13,29 @@ use Utopia\Cache\Feature\Retryable;
class Redis extends Leasable implements Adapter, Retryable
{
/**
* @var Client
*/
protected Client $redis;
private int $maxRetries = 0;
private int $retryDelay = 1000; // milliseconds
private string $host;
private readonly string $host;
private int $port;
private readonly int $port;
private float $timeout;
private readonly float $timeout;
private ?string $persistentId;
private readonly ?string $persistentId;
private float $readTimeout;
private readonly float $readTimeout;
/**
* @var string|array<string>|null
* Credentials replayed on reconnect, as the argument list AUTH takes:
* [password] for a legacy password, [username, password] for an ACL user.
*
* @var array<string>
*/
private string|array|null $auth = null;
private array $auth = [];
/**
* Whether the original connection was persistent (pconnect)
@@ -46,15 +46,13 @@ class Redis extends Leasable implements Adapter, Retryable
/**
* Redis constructor.
*
* @param Client $redis
*/
public function __construct(Client $redis)
{
$this->host = $redis->getHost();
$this->port = $redis->getPort();
$timeout = $redis->getTimeout();
$this->timeout = ($timeout !== false) ? (float) $timeout : 0.0;
$this->timeout = ($timeout !== false) ? $timeout : 0.0;
$this->persistentId = $redis->getPersistentId();
$this->readTimeout = $redis->getReadTimeout();
@@ -62,16 +60,17 @@ class Redis extends Leasable implements Adapter, Retryable
$this->dbIndex = $redis->getDbNum();
$auth = $redis->getAuth();
if ($auth !== null && $auth !== false) {
$this->auth = $auth;
}
$this->auth = match (true) {
\is_array($auth) => $auth,
\is_string($auth) => [$auth],
default => [],
};
$this->redis = $redis;
}
/**
* @param int $maxRetries (0-10)
* @return self
*/
public function setMaxRetries(int $maxRetries): self
{
@@ -82,7 +81,6 @@ class Redis extends Leasable implements Adapter, Retryable
/**
* @param int $retryDelay time in milliseconds
* @return self
*/
public function setRetryDelay(int $retryDelay): self
{
@@ -92,14 +90,12 @@ class Redis extends Leasable implements Adapter, Retryable
}
/**
* @param string $key
* @param int $ttl time in seconds
* @param string $hash optional
* @return mixed
*/
public function load(string $key, int $ttl, string $hash = ''): mixed
{
if (empty($hash)) {
if ($hash === '' || $hash === '0') {
$hash = $key;
}
@@ -107,9 +103,9 @@ class Redis extends Leasable implements Adapter, Retryable
return false;
}
$redis_string = $this->execute(fn () => $this->redis->hGet($key, $hash));
$redis_string = $this->execute(fn(): \Redis|string|false => $this->redis->hGet($key, $hash));
if (! is_string($redis_string)) {
if (! \is_string($redis_string)) {
return false;
}
@@ -117,18 +113,17 @@ class Redis extends Leasable implements Adapter, Retryable
}
/**
* @param string $key
* @param array<int|string, mixed>|string $data
* @param string $hash optional
* @return bool|string|array<int|string, mixed>
*/
public function save(string $key, array|string $data, string $hash = ''): bool|string|array
{
if (empty($key) || empty($data)) {
if ($key === '' || $key === '0' || empty($data)) {
return false;
}
if (empty($hash)) {
if ($hash === '' || $hash === '0') {
$hash = $key;
}
@@ -138,22 +133,20 @@ class Redis extends Leasable implements Adapter, Retryable
try {
$value = Envelope::encode($data, time());
$this->execute(fn () => $this->redis->hSet($key, $hash, $value));
$this->execute(fn(): \Redis|int|false => $this->redis->hSet($key, $hash, $value));
return $data;
} catch (Throwable $th) {
} catch (Throwable) {
return false;
}
}
/**
* @param string $key
* @param string $hash optional
* @return bool
*/
public function touch(string $key, string $hash = ''): bool
{
if (empty($hash)) {
if ($hash === '' || $hash === '0') {
$hash = $key;
}
@@ -161,9 +154,9 @@ class Redis extends Leasable implements Adapter, Retryable
return false;
}
$redis_string = $this->execute(fn () => $this->redis->hGet($key, $hash));
$redis_string = $this->execute(fn(): \Redis|string|false => $this->redis->hGet($key, $hash));
if (! is_string($redis_string)) {
if (! \is_string($redis_string)) {
return false;
}
@@ -172,24 +165,23 @@ class Redis extends Leasable implements Adapter, Retryable
return false;
}
return $this->execute(fn () => $this->redis->hSet($key, $hash, $value)) !== false;
return $this->execute(fn(): \Redis|int|false => $this->redis->hSet($key, $hash, $value)) !== false;
}
/**
* @param string $key
* @return string[]
*/
public function list(string $key): array
{
/** @var array<string> */
$keys = $this->execute(fn () => $this->redis->hKeys($key));
$keys = $this->execute(fn(): \Redis|array|false => $this->redis->hKeys($key));
if (empty($keys)) {
return [];
}
// Don't expose reserved internal fields (generation, tombstone) as listable cache fields.
return \array_values(\array_filter($keys, fn (string $field): bool => ! $this->isReserved($field)));
return array_values(array_filter($keys, fn(string $field): bool => ! $this->isReserved($field)));
}
protected function leaseEvalSha(string $sha, string $key, array $args): mixed
@@ -224,62 +216,48 @@ class Redis extends Leasable implements Adapter, Retryable
protected function leaseEval(string $script, string $key, array $args): mixed
{
return $this->execute(fn () => $this->redis->eval($script, [$key, ...$args], 1));
return $this->execute(fn(): mixed => $this->redis->eval($script, [$key, ...$args], 1));
}
protected function leaseHget(string $key, string $field): mixed
{
return $this->execute(fn () => $this->redis->hGet($key, $field));
return $this->execute(fn(): \Redis|string|false => $this->redis->hGet($key, $field));
}
/**
* @return bool
*/
public function flush(): bool
{
return (bool) $this->execute(fn () => $this->redis->flushDB());
return (bool) $this->execute(fn(): \Redis|bool => $this->redis->flushDB());
}
/**
* @return bool
*/
public function ping(): bool
{
try {
$this->redis->ping();
return true;
} catch (Exception $e) {
} catch (Exception) {
return false;
}
}
/**
* Returning total number of keys
*
* @return int
*/
public function getSize(): int
{
// A purged key keeps its reserved field(s) until re-cached, so DBSIZE can
// slightly over-count purged-but-not-yet-recached keys.
/** @var int $size */
$size = $this->execute(fn () => $this->redis->dbSize());
$size = $this->execute(fn(): \Redis|int|false => $this->redis->dbSize());
return $size;
}
/**
* @return int
*/
public function getMaxRetries(): int
{
return $this->maxRetries;
}
/**
* @return int
*/
public function getRetryDelay(): int
{
return $this->retryDelay;
@@ -288,8 +266,6 @@ class Redis extends Leasable implements Adapter, Retryable
/**
* Execute a Redis command with retry logic
*
* @param callable $callback
* @return mixed
*
* @throws \RedisException
*/
@@ -316,7 +292,7 @@ class Redis extends Leasable implements Adapter, Retryable
try {
$this->reconnect();
} catch (\RedisException $e) {
} catch (\RedisException) {
// Reconnect failed, will retry on next iteration
}
}
@@ -331,9 +307,6 @@ class Redis extends Leasable implements Adapter, Retryable
*
* RedisException always returns error code 0 with no subclasses for different error types.
* The only way to differentiate connection errors from command errors is by message matching.
*
* @param Exception $e
* @return bool
*/
private function isConnectionError(Exception $e): bool
{
@@ -350,13 +323,7 @@ class Redis extends Leasable implements Adapter, Retryable
];
$message = strtolower($e->getMessage());
foreach ($connectionErrors as $needle) {
if (str_contains($message, $needle)) {
return true;
}
}
return false;
return array_any($connectionErrors, fn(string $needle): bool => str_contains($message, $needle));
}
private function reconnect(): void
@@ -383,8 +350,8 @@ class Redis extends Leasable implements Adapter, Retryable
);
}
if ($this->auth !== null) {
$newRedis->auth($this->auth);
if ($this->auth !== []) {
$newRedis->auth(\count($this->auth) === 1 ? $this->auth[0] : $this->auth);
}
if ($this->dbIndex !== 0) {
+10 -10
View File
@@ -19,7 +19,7 @@ class Client
*/
public const INCOMPLETE = "\0__INCOMPLETE__\0";
private SwooleClient $client;
private readonly SwooleClient $client;
private string $buffer = '';
@@ -36,7 +36,7 @@ class Client
$errMsg = $this->client->errMsg;
$this->close();
throw new ConnectionException('Failed to connect to Redis: '.$errMsg);
throw new ConnectionException('Failed to connect to Redis: ' . $errMsg);
}
}
@@ -76,7 +76,7 @@ class Client
public function send(string $payload): void
{
$written = $this->client->send($payload);
if ($written === strlen($payload)) {
if ($written === \strlen($payload)) {
return;
}
@@ -84,7 +84,7 @@ class Client
? ($this->client->errMsg ?: 'send failed')
: 'partial send';
throw new ConnectionException('Redis send failed: '.$message);
throw new ConnectionException('Redis send failed: ' . $message);
}
public function recv(float $timeout): string|false
@@ -109,7 +109,7 @@ class Client
{
try {
$this->client->close();
} catch (Throwable $th) {
} catch (Throwable) {
// ignore
}
}
@@ -129,7 +129,7 @@ class Client
throw $value->exception;
}
if (! is_array($value)) {
if (! \is_array($value)) {
return $value;
}
@@ -147,10 +147,10 @@ class Client
*/
public static function encode(array $args): string
{
$out = '*'.count($args)."\r\n";
$out = '*' . \count($args) . "\r\n";
foreach ($args as $arg) {
$arg = (string) $arg;
$out .= '$'.strlen($arg)."\r\n".$arg."\r\n";
$out .= '$' . \strlen($arg) . "\r\n" . $arg . "\r\n";
}
return $out;
@@ -188,7 +188,7 @@ class Client
if ($len === -1) {
return null;
}
if (strlen($buffer) < $offset + $len + 2) {
if (\strlen($buffer) < $offset + $len + 2) {
return self::INCOMPLETE;
}
$value = substr($buffer, $offset, $len);
@@ -211,7 +211,7 @@ class Client
return $items;
default:
throw new \RedisException('Unknown RESP type: '.$type);
throw new \RedisException('Unknown RESP type: ' . $type);
}
}
}
@@ -1,5 +1,7 @@
<?php
declare(strict_types=1);
namespace Utopia\Cache\Adapter\Redis;
use SplQueue;
@@ -12,6 +14,5 @@ class ConnectionContext
public function __construct(
public Client $client,
public SplQueue $pending,
) {
}
) {}
}
+3 -2
View File
@@ -1,11 +1,12 @@
<?php
declare(strict_types=1);
namespace Utopia\Cache\Adapter\Redis;
final class ConnectionError
{
public function __construct(
public ConnectionException $exception,
) {
}
) {}
}
@@ -1,5 +1,7 @@
<?php
declare(strict_types=1);
namespace Utopia\Cache\Adapter\Redis;
/**
@@ -7,6 +9,4 @@ namespace Utopia\Cache\Adapter\Redis;
* unhealthy and the operation should be retried (or surfaced as a connection
* error rather than a Redis-level error).
*/
class ConnectionException extends \RedisException
{
}
class ConnectionException extends \RedisException {}
+4 -2
View File
@@ -1,5 +1,7 @@
<?php
declare(strict_types=1);
namespace Utopia\Cache\Adapter\Redis;
use JsonException;
@@ -33,7 +35,7 @@ final class Envelope
public static function decode(string $value, int $ttl, int $now): mixed
{
$cache = json_decode($value, true);
if (! is_array($cache) || ! isset($cache['time'], $cache['data']) || ! is_int($cache['time'])) {
if (! \is_array($cache) || ! isset($cache['time'], $cache['data']) || ! \is_int($cache['time'])) {
return false;
}
@@ -56,7 +58,7 @@ final class Envelope
return false;
}
if (! is_array($cache) || ! array_key_exists('data', $cache)) {
if (! \is_array($cache) || ! \array_key_exists('data', $cache)) {
return false;
}
+5 -5
View File
@@ -141,11 +141,11 @@ abstract class Leasable implements \Utopia\Cache\Feature\Leasable
public function saveWithLease(string $key, array|string $data, string $hash, string $generation): bool|string|array
{
if (empty($key) || empty($data)) {
if ($key === '' || $key === '0' || empty($data)) {
return false;
}
if (empty($hash)) {
if ($hash === '' || $hash === '0') {
$hash = $key;
}
@@ -160,14 +160,14 @@ abstract class Leasable implements \Utopia\Cache\Feature\Leasable
]);
return $stored ? $data : false;
} catch (Throwable $th) {
} catch (Throwable) {
return false;
}
}
public function purge(string $key, string $hash = ''): bool
{
if (! empty($hash)) {
if ($hash !== '' && $hash !== '0') {
if ($this->isReserved($hash)) {
return false;
}
@@ -211,7 +211,7 @@ abstract class Leasable implements \Utopia\Cache\Feature\Leasable
/** SHA1 digest Redis identifies a cached script by — the digest of its body. */
private function shaFor(string $script): string
{
return self::$scriptSha[$script] ??= \sha1($script);
return self::$scriptSha[$script] ??= sha1($script);
}
/**
+28 -33
View File
@@ -31,28 +31,25 @@ class Multiplexing extends Leasable implements Adapter, TelemetryFeature
* Serializes pending enqueue + send so the FIFO invariant holds even when
* many coroutines issue commands concurrently.
*/
private Lock $sendLock;
private readonly Lock $sendLock;
private Telemetry $telemetry;
private ?UpDownCounter $pendingDepth = null;
/**
* @param string $host
* @param int $port
* @param float $timeout connect timeout in seconds
* @param float $readTimeout read timeout in seconds — caches should
* fail fast, default 0.25s
* @param string|array<string>|null $auth password or [username, password]
* @param int $dbIndex
*/
public function __construct(
private string $host,
private int $port = 6379,
private float $timeout = 1.0,
private float $readTimeout = 0.25,
private string|array|null $auth = null,
private int $dbIndex = 0,
private readonly string $host,
private readonly int $port = 6379,
private readonly float $timeout = 1.0,
private readonly float $readTimeout = 0.25,
private readonly string|array|null $auth = null,
private readonly int $dbIndex = 0,
) {
if ($this->timeout <= 0) {
throw new \InvalidArgumentException('timeout must be greater than 0');
@@ -102,7 +99,7 @@ class Multiplexing extends Leasable implements Adapter, TelemetryFeature
public function load(string $key, int $ttl, string $hash = ''): mixed
{
if (empty($hash)) {
if ($hash === '' || $hash === '0') {
$hash = $key;
}
@@ -112,7 +109,7 @@ class Multiplexing extends Leasable implements Adapter, TelemetryFeature
$value = $this->command(['HGET', $key, $hash]);
if (! is_string($value)) {
if (! \is_string($value)) {
return false;
}
@@ -121,11 +118,11 @@ class Multiplexing extends Leasable implements Adapter, TelemetryFeature
public function save(string $key, array|string $data, string $hash = ''): bool|string|array
{
if (empty($key) || empty($data)) {
if ($key === '' || $key === '0' || empty($data)) {
return false;
}
if (empty($hash)) {
if ($hash === '' || $hash === '0') {
$hash = $key;
}
@@ -141,7 +138,7 @@ class Multiplexing extends Leasable implements Adapter, TelemetryFeature
public function touch(string $key, string $hash = ''): bool
{
if (empty($hash)) {
if ($hash === '' || $hash === '0') {
$hash = $key;
}
@@ -150,7 +147,7 @@ class Multiplexing extends Leasable implements Adapter, TelemetryFeature
}
$value = $this->command(['HGET', $key, $hash]);
if (! is_string($value)) {
if (! \is_string($value)) {
return false;
}
@@ -168,12 +165,12 @@ class Multiplexing extends Leasable implements Adapter, TelemetryFeature
public function list(string $key): array
{
$keys = $this->command(['HKEYS', $key]);
if (! is_array($keys)) {
if (! \is_array($keys)) {
return [];
}
/** @var string[] $keys */
return \array_values(\array_filter($keys, fn (string $field): bool => ! $this->isReserved($field)));
return array_values(array_filter($keys, fn(string $field): bool => ! $this->isReserved($field)));
}
protected function leaseEvalSha(string $sha, string $key, array $args): mixed
@@ -211,7 +208,7 @@ class Multiplexing extends Leasable implements Adapter, TelemetryFeature
{
try {
return $this->command(['PING']) === 'PONG';
} catch (Throwable $th) {
} catch (Throwable) {
return false;
}
}
@@ -220,7 +217,7 @@ class Multiplexing extends Leasable implements Adapter, TelemetryFeature
{
$size = $this->command(['DBSIZE']);
return is_int($size) ? $size : 0;
return \is_int($size) ? $size : 0;
}
public function getName(?string $key = null): string
@@ -240,7 +237,7 @@ class Multiplexing extends Leasable implements Adapter, TelemetryFeature
{
try {
return $this->dispatch($args);
} catch (ConnectionException $th) {
} catch (ConnectionException) {
$this->ensureConnected();
return $this->dispatch($args);
@@ -258,7 +255,7 @@ class Multiplexing extends Leasable implements Adapter, TelemetryFeature
$locked = $this->lockSend();
try {
$context = $this->connection;
if ($context === null) {
if (!$context instanceof \Utopia\Cache\Adapter\Redis\ConnectionContext) {
throw new ConnectionException('Redis connection is not open');
}
$response = new Channel(1);
@@ -275,7 +272,7 @@ class Multiplexing extends Leasable implements Adapter, TelemetryFeature
$this->unlockSend($locked);
}
if ($error !== null) {
if ($error instanceof \Utopia\Cache\Adapter\Redis\ConnectionException) {
$this->teardownIfCurrent($context, $error);
throw $error;
@@ -288,7 +285,7 @@ class Multiplexing extends Leasable implements Adapter, TelemetryFeature
{
$locked = $this->lockSend();
try {
if ($this->connection === null) {
if (!$this->connection instanceof \Utopia\Cache\Adapter\Redis\ConnectionContext) {
$this->connect();
}
} finally {
@@ -321,7 +318,7 @@ class Multiplexing extends Leasable implements Adapter, TelemetryFeature
try {
if ($this->auth !== null) {
$authArgs = is_array($this->auth)
$authArgs = \is_array($this->auth)
? array_merge(['AUTH'], array_values($this->auth))
: ['AUTH', $this->auth];
@@ -330,10 +327,8 @@ class Multiplexing extends Leasable implements Adapter, TelemetryFeature
}
}
if ($this->dbIndex !== 0) {
if ($client->command(['SELECT', (string) $this->dbIndex], $this->readTimeout) !== 'OK') {
throw new \RedisException('Redis SELECT failed');
}
if ($this->dbIndex !== 0 && $client->command(['SELECT', (string) $this->dbIndex], $this->readTimeout) !== 'OK') {
throw new \RedisException('Redis SELECT failed');
}
} catch (Throwable $th) {
$client->close();
@@ -346,7 +341,7 @@ class Multiplexing extends Leasable implements Adapter, TelemetryFeature
$context = new ConnectionContext($client, $pending);
$this->connection = $context;
Coroutine::create(function () use ($context) {
Coroutine::create(function () use ($context): void {
$this->readerLoop($context);
});
}
@@ -356,7 +351,7 @@ class Multiplexing extends Leasable implements Adapter, TelemetryFeature
$context = null;
$locked = $this->lockSend();
try {
if ($this->connection !== null) {
if ($this->connection instanceof \Utopia\Cache\Adapter\Redis\ConnectionContext) {
$context = $this->connection;
$this->connection = null;
}
@@ -364,7 +359,7 @@ class Multiplexing extends Leasable implements Adapter, TelemetryFeature
$this->unlockSend($locked);
}
if ($context !== null) {
if ($context instanceof \Utopia\Cache\Adapter\Redis\ConnectionContext) {
$this->finishTeardown($context, new ConnectionException('Connection closed'));
}
}
@@ -431,7 +426,7 @@ class Multiplexing extends Leasable implements Adapter, TelemetryFeature
try {
$value = Client::parse($readBuffer, $offset);
} catch (Throwable $th) {
$this->teardownIfCurrent($context, new ConnectionException('Redis protocol parse failed: '.$th->getMessage()));
$this->teardownIfCurrent($context, new ConnectionException('Redis protocol parse failed: ' . $th->getMessage()));
return;
}
+2 -2
View File
@@ -16,7 +16,7 @@ final class NoScript extends \RuntimeException
* code is the reply's leading token — "NOSCRIPT No matching script." — the
* same across the raw RESP frame and php-redis getLastError().
*/
private const CODE = 'NOSCRIPT';
private const string CODE = 'NOSCRIPT';
/**
* True when a Redis error string's code (its leading token) is NOSCRIPT.
@@ -25,7 +25,7 @@ final class NoScript extends \RuntimeException
*/
public static function matches(string $error): bool
{
return \explode(' ', $error, 2)[0] === self::CODE;
return explode(' ', $error, 2)[0] === self::CODE;
}
/** Build the signal from the underlying Redis error (exception or message). */
+3 -2
View File
@@ -1,11 +1,12 @@
<?php
declare(strict_types=1);
namespace Utopia\Cache\Adapter\Redis;
final class RedisError
{
public function __construct(
public \RedisException $exception,
) {
}
) {}
}
+32 -116
View File
@@ -10,66 +10,18 @@ use Utopia\Cache\Feature\Retryable;
class RedisCluster implements Adapter, Retryable
{
/**
* @var Client
*/
protected Client $redis;
/**
* @var array<string>
*/
protected array $seeds;
/**
* @var ?string
*/
protected ?string $name;
private int $maxRetries = 0;
private int $retryDelay = 1000; // milliseconds
private float $timeout;
private float $readTimeout;
private bool $persistent;
private int $retryDelay = 1000;
/**
* @var string|array<string>|null
*/
private string|array|null $auth;
/**
* @param Client $redis
* @param array<string> $seeds
* @param string|null $name
* @param float $timeout
* @param float $readTimeout
* @param bool $persistent
* @param string|array<string>|null $auth Password string or ['username', 'password'] array for ACL
*/
public function __construct(
Client $redis,
array $seeds,
?string $name = null,
float $timeout = 1.5,
float $readTimeout = 1.5,
bool $persistent = false,
string|array|null $auth = null
) {
$this->redis = $redis;
$this->seeds = $seeds;
$this->name = $name;
$this->timeout = $timeout;
$this->readTimeout = $readTimeout;
$this->persistent = $persistent;
$this->auth = $auth;
}
public function __construct(protected Client $redis, protected array $seeds, protected ?string $name = null, private readonly float $timeout = 1.5, private readonly float $readTimeout = 1.5, private readonly bool $persistent = false, private readonly string|array|null $auth = null) {}
/**
* @param int $maxRetries (0-10)
* @return self
*/
public function setMaxRetries(int $maxRetries): self
{
@@ -80,7 +32,6 @@ class RedisCluster implements Adapter, Retryable
/**
* @param int $retryDelay time in milliseconds
* @return self
*/
public function setRetryDelay(int $retryDelay): self
{
@@ -90,27 +41,30 @@ class RedisCluster implements Adapter, Retryable
}
/**
* @param string $key
* @param int $ttl time in seconds
* @param string $hash optional
* @return mixed
*/
public function load(string $key, int $ttl, string $hash = ''): mixed
{
if (empty($hash)) {
if ($hash === '' || $hash === '0') {
$hash = $key;
}
/** @var string|false */
$redis_string = $this->execute(fn () => $this->redis->hGet($key, $hash));
$redis_string = $this->execute(fn(): string => $this->redis->hGet($key, $hash));
if ($redis_string === false || ! is_string($redis_string)) {
if ($redis_string === false || ! \is_string($redis_string)) {
return false;
}
/** @var array{time: int, data: string} $cache */
$cache = json_decode($redis_string, true);
// A purged key keeps its field until re-cached, holding a value that
// is not an envelope.
if (! \is_array($cache) || ! isset($cache['time'], $cache['data'])) {
return false;
}
if ($cache['time'] + $ttl > time()) { // Cache is valid
return $cache['data'];
}
@@ -119,54 +73,51 @@ class RedisCluster implements Adapter, Retryable
}
/**
* @param string $key
* @param array<int|string, mixed>|string $data
* @param string $hash optional
* @return bool|string|array<int|string, mixed>
*/
public function save(string $key, array|string $data, string $hash = ''): bool|string|array
{
if (empty($key) || empty($data)) {
if ($key === '' || $key === '0' || empty($data)) {
return false;
}
if (empty($hash)) {
if ($hash === '' || $hash === '0') {
$hash = $key;
}
try {
$value = json_encode([
'time' => \time(),
'time' => time(),
'data' => $data,
], flags: JSON_THROW_ON_ERROR);
} catch (Throwable $th) {
} catch (Throwable) {
return false;
}
try {
$this->execute(fn () => $this->redis->hSet($key, $hash, $value));
$this->execute(fn(): int => $this->redis->hSet($key, $hash, $value));
return $data;
} catch (Throwable $th) {
} catch (Throwable) {
return false;
}
}
/**
* @param string $key
* @param string $hash optional
* @return bool
*/
public function touch(string $key, string $hash = ''): bool
{
if (empty($hash)) {
if ($hash === '' || $hash === '0') {
$hash = $key;
}
/** @var string|false */
$redis_string = $this->execute(fn () => $this->redis->hGet($key, $hash));
$redis_string = $this->execute(fn(): string => $this->redis->hGet($key, $hash));
if ($redis_string === false || ! is_string($redis_string)) {
if ($redis_string === false || ! \is_string($redis_string)) {
return false;
}
@@ -175,21 +126,20 @@ class RedisCluster implements Adapter, Retryable
$cache = json_decode($redis_string, true, flags: JSON_THROW_ON_ERROR);
$cache['time'] = time();
$value = json_encode($cache, flags: JSON_THROW_ON_ERROR);
} catch (Throwable $th) {
} catch (Throwable) {
return false;
}
return $this->execute(fn () => $this->redis->hSet($key, $hash, $value)) !== false;
return $this->execute(fn(): int => $this->redis->hSet($key, $hash, $value)) !== false;
}
/**
* @param string $key
* @return string[]
*/
public function list(string $key): array
{
/** @var array<string> */
$keys = (array) $this->execute(fn () => $this->redis->hKeys($key));
$keys = (array) $this->execute(fn(): array => $this->redis->hKeys($key));
if (empty($keys)) {
return [];
@@ -199,25 +149,20 @@ class RedisCluster implements Adapter, Retryable
}
/**
* @param string $key
* @param string $hash optional
* @return bool
*/
public function purge(string $key, string $hash = ''): bool
{
if (! empty($hash)) {
return (bool) $this->execute(fn () => $this->redis->hdel($key, $hash));
if ($hash !== '' && $hash !== '0') {
return (bool) $this->execute(fn(): int => $this->redis->hdel($key, $hash));
}
return (bool) $this->execute(fn () => $this->redis->del($key));
return (bool) $this->execute(fn(): int => $this->redis->del($key));
}
/**
* @return bool
*/
public function flush(): bool
{
return (bool) $this->execute(function () {
return (bool) $this->execute(function (): true {
/** @var array<string> $masters */
$masters = $this->redis->_masters();
foreach ($masters as $master) {
@@ -228,28 +173,23 @@ class RedisCluster implements Adapter, Retryable
});
}
/**
* @return bool
*/
public function ping(): bool
{
try {
return (bool) $this->execute(function () {
return (bool) $this->execute(function (): true {
foreach ($this->redis->_masters() as $master) {
$this->redis->ping($master);
}
return true;
});
} catch (Exception $e) {
} catch (Exception) {
return false;
}
}
/**
* Returning total number of keys
*
* @return int
*/
public function getSize(): int
{
@@ -269,25 +209,16 @@ class RedisCluster implements Adapter, Retryable
return (int) $size;
}
/**
* @return int
*/
public function getMaxRetries(): int
{
return $this->maxRetries;
}
/**
* @return int
*/
public function getRetryDelay(): int
{
return $this->retryDelay;
}
/**
* @return ?string
*/
public function getClusterName(): ?string
{
return $this->name;
@@ -304,8 +235,6 @@ class RedisCluster implements Adapter, Retryable
/**
* Execute a Redis command with retry logic
*
* @param callable $callback
* @return mixed
*
* @throws \RedisClusterException
*/
@@ -332,7 +261,7 @@ class RedisCluster implements Adapter, Retryable
try {
$this->reconnect();
} catch (\RedisClusterException $e) {
} catch (\RedisClusterException) {
// Reconnect failed, will retry on next iteration
}
}
@@ -347,9 +276,6 @@ class RedisCluster implements Adapter, Retryable
*
* RedisClusterException always returns error code 0 with no subclasses for different error types.
* The only way to differentiate connection errors from command errors is by message matching.
*
* @param Exception $e
* @return bool
*/
private function isConnectionError(Exception $e): bool
{
@@ -371,13 +297,7 @@ class RedisCluster implements Adapter, Retryable
];
$message = strtolower($e->getMessage());
foreach ($connectionErrors as $needle) {
if (str_contains($message, $needle)) {
return true;
}
}
return false;
return array_any($connectionErrors, fn(string $needle): bool => str_contains($message, $needle));
}
private function reconnect(): void
@@ -388,16 +308,12 @@ class RedisCluster implements Adapter, Retryable
$this->timeout,
$this->readTimeout,
$this->persistent,
$this->auth
$this->auth,
);
$this->redis = $newRedis;
}
/**
* @param string|null $key
* @return string
*/
public function getName(?string $key = null): string
{
return 'redis-cluster';
+4 -36
View File
@@ -12,9 +12,6 @@ class Sharding implements Adapter, Leasable
*/
protected array $adapters;
/**
* @var int
*/
protected int $count = 0;
/**
@@ -37,7 +34,7 @@ class Sharding implements Adapter, Leasable
*/
public function __construct(array $adapters)
{
if (empty($adapters)) {
if ($adapters === []) {
throw new \Exception('No adapters provided');
}
@@ -46,10 +43,8 @@ class Sharding implements Adapter, Leasable
}
/**
* @param string $key
* @param int $ttl time in seconds
* @param string $hash optional
* @return mixed
*/
public function load(string $key, int $ttl, string $hash = ''): mixed
{
@@ -57,7 +52,6 @@ class Sharding implements Adapter, Leasable
}
/**
* @param string $key
* @param array<int|string, mixed>|string $data
* @param string $hash optional
* @return bool|string|array<int|string, mixed>
@@ -84,9 +78,7 @@ class Sharding implements Adapter, Leasable
}
/**
* @param string $key
* @param string $hash optional
* @return bool
*/
public function touch(string $key, string $hash = ''): bool
{
@@ -94,7 +86,6 @@ class Sharding implements Adapter, Leasable
}
/**
* @param string $key
* @return string[]
*/
public function list(string $key): array
@@ -103,46 +94,30 @@ class Sharding implements Adapter, Leasable
}
/**
* @param string $key
* @param string $hash optional
* @return bool
*/
public function purge(string $key, string $hash = ''): bool
{
return $this->getAdapter($key)->purge($key, $hash);
}
/**
* @return bool
*/
public function flush(): bool
{
$result = true;
foreach ($this->adapters as $value) {
$result = ($value->flush()) ? $result : false;
$result = $value->flush() && $result;
}
return $result;
}
/**
* @return bool
*/
public function ping(): bool
{
foreach ($this->adapters as $value) {
if (! ($value->ping())) {
return false;
}
}
return true;
return array_all($this->adapters, fn(\Utopia\Cache\Adapter $value): bool => $value->ping());
}
/**
* Returning total number of keys of all adapters
*
* @return int
*/
public function getSize(): int
{
@@ -154,9 +129,6 @@ class Sharding implements Adapter, Leasable
return $size;
}
/**
* @return string
*/
public function getName(?string $key = null): string
{
if ($key === null) {
@@ -166,13 +138,9 @@ class Sharding implements Adapter, Leasable
return $this->getAdapter($key)->getName();
}
/**
* @param string $key
* @return Adapter
*/
protected function getAdapter(string $key): Adapter
{
$hash = \crc32($key);
$hash = crc32($key);
$index = $hash % $this->count;
return $this->adapters[$index];
+10 -44
View File
@@ -10,8 +10,6 @@ use Utopia\Telemetry\Histogram;
class Cache
{
private Adapter $adapter;
/**
* @var bool If cache keys are case-sensitive
*/
@@ -19,21 +17,13 @@ class Cache
protected Telemetry $telemetry;
/**
* @var Histogram|null
*/
protected ?Histogram $operationDuration = null;
/**
* @var Counter|null
*/
protected ?Counter $loadResults = null;
/**
* Set telemetry adapter. Instruments are created lazily on first use to
* avoid emitting empty data point streams on every export interval.
*
* @param Telemetry $telemetry
*/
public function setTelemetry(Telemetry $telemetry): void
{
@@ -52,7 +42,7 @@ class Cache
'cache.operation.duration',
's',
null,
['ExplicitBucketBoundaries' => [0.001, 0.005, 0.01, 0.025, 0.05, 0.1, 0.25, 0.5, 1]]
['ExplicitBucketBoundaries' => [0.001, 0.005, 0.01, 0.025, 0.05, 0.1, 0.25, 0.5, 1]],
);
}
@@ -67,12 +57,9 @@ class Cache
/**
* Initialize with a no-op telemetry adapter by default.
*
* @param Adapter $adapter
*/
public function __construct(Adapter $adapter)
public function __construct(private readonly Adapter $adapter)
{
$this->adapter = $adapter;
$this->telemetry = new NoTelemetry();
}
@@ -80,7 +67,6 @@ class Cache
* Toggle case sensitivity of keys inside cache
*
* @param bool $value if true, cache keys will be case-sensitive
* @return bool
*/
public function setCaseSensitivity(bool $value): bool
{
@@ -90,15 +76,13 @@ class Cache
/**
* Load cached data. return false in no valid cache.
*
* @param string $key
* @param int $ttl time in seconds
* @param string $hash optional
* @return mixed
*/
public function load(string $key, int $ttl, string $hash = ''): mixed
{
$key = $this->caseSensitive ? $key : \strtolower($key);
$hash = $this->caseSensitive ? $hash : \strtolower($hash);
$key = $this->caseSensitive ? $key : strtolower($key);
$hash = $this->caseSensitive ? $hash : strtolower($hash);
$start = microtime(true);
$result = $this->adapter->load($key, $ttl, $hash);
@@ -119,7 +103,6 @@ class Cache
/**
* Save data to cache. Returns data on success of false on failure.
*
* @param string $key
* @param string|array<int|string, mixed> $data
* @param string $hash optional
* @return bool|string|array<int|string, mixed>
@@ -144,9 +127,6 @@ class Cache
/**
* Current generation token for $key (advances on each purge). Returns '0'
* when the underlying adapter does not support leasing.
*
* @param string $key
* @return string
*/
public function getGeneration(string $key): string
{
@@ -154,7 +134,7 @@ class Cache
return '0';
}
$key = $this->caseSensitive ? $key : \strtolower($key);
$key = $this->caseSensitive ? $key : strtolower($key);
return $this->adapter->getGeneration($key);
}
@@ -165,16 +145,13 @@ class Cache
* cache-aside read-after-write race. Falls back to an unconditional save when
* the adapter does not support leasing.
*
* @param string $key
* @param string|array<int|string, mixed> $data
* @param string $hash
* @param string $generation
* @return bool|string|array<int|string, mixed>
*/
public function saveWithLease(string $key, mixed $data, string $hash, string $generation): bool|string|array
{
$key = $this->caseSensitive ? $key : \strtolower($key);
$hash = $this->caseSensitive ? $hash : \strtolower($hash);
$key = $this->caseSensitive ? $key : strtolower($key);
$hash = $this->caseSensitive ? $hash : strtolower($hash);
$start = microtime(true);
try {
@@ -195,9 +172,7 @@ class Cache
/**
* Refresh a cache entry timestamp without replacing its data.
*
* @param string $key
* @param string $hash optional
* @return bool
*/
public function touch(string $key, string $hash = ''): bool
{
@@ -218,12 +193,11 @@ class Cache
/**
* Returns a list of keys.
*
* @param string $key
* @return string[]
*/
public function list(string $key): array
{
$key = $this->caseSensitive ? $key : \strtolower($key);
$key = $this->caseSensitive ? $key : strtolower($key);
$start = microtime(true);
$result = $this->adapter->list($key);
@@ -239,14 +213,12 @@ class Cache
/**
* Removes data from cache. Returns true on success of false on failure.
*
* @param string $key
* @param string $hash optional
* @return bool
*/
public function purge(string $key, string $hash = ''): bool
{
$key = $this->caseSensitive ? $key : \strtolower($key);
$hash = $this->caseSensitive ? $hash : \strtolower($hash);
$key = $this->caseSensitive ? $key : strtolower($key);
$hash = $this->caseSensitive ? $hash : strtolower($hash);
$start = microtime(true);
$result = $this->adapter->purge($key, $hash);
@@ -261,8 +233,6 @@ class Cache
/**
* Removes all data from cache. Returns true on success of false on failure.
*
* @return bool
*/
public function flush(): bool
{
@@ -279,8 +249,6 @@ class Cache
/**
* Check Cache Connecitivity
*
* @return bool
*/
public function ping(): bool
{
@@ -289,8 +257,6 @@ class Cache
/**
* Get db size.
*
* @return int
*/
public function getSize(): int
{
+2
View File
@@ -1,5 +1,7 @@
<?php
declare(strict_types=1);
namespace Utopia\Cache\Feature;
/**
+4 -10
View File
@@ -1,32 +1,26 @@
<?php
declare(strict_types=1);
namespace Utopia\Cache\Feature;
interface Retryable
{
const MIN_RETRIES = 0;
public const MIN_RETRIES = 0;
const MAX_RETRIES = 10;
public const MAX_RETRIES = 10;
/**
* @param int $maxRetries (0-10)
* @return self
*/
public function setMaxRetries(int $maxRetries): self;
/**
* @param int $retryDelay time in milliseconds
* @return self
*/
public function setRetryDelay(int $retryDelay): self;
/**
* @return int
*/
public function getMaxRetries(): int;
/**
* @return int
*/
public function getRetryDelay(): int;
}
+2
View File
@@ -1,5 +1,7 @@
<?php
declare(strict_types=1);
namespace Utopia\Cache\Feature;
use Utopia\Telemetry\Adapter as TelemetryAdapter;
@@ -1,6 +1,8 @@
<?php
namespace Utopia\Tests\E2E;
declare(strict_types=1);
namespace Utopia\Tests;
use PHPUnit\Framework\TestCase;
use Utopia\Cache\Cache;
@@ -18,6 +20,51 @@ abstract class Base extends TestCase
*/
protected array $dataArray = ['test', 'data', 'string'];
/**
* An empty scratch directory for the filesystem-backed adapters. It is
* cleared first, so a previous run cannot leak keys into this one.
*/
protected static function scratch(string $name): string
{
$path = sys_get_temp_dir() . '/utopia-cache/' . $name;
self::deletePath($path);
mkdir($path, 0777, true);
return $path;
}
protected static function deletePath(string $path): void
{
if (! file_exists($path)) {
return;
}
if (is_file($path)) {
unlink($path);
return;
}
foreach (glob($path . '/*') ?: [] as $file) {
self::deletePath($file);
}
rmdir($path);
}
/**
* macOS and Windows fold filename case, so the filesystem-backed adapters
* cannot tell 'color' from 'COLOR' there however the cache is configured.
*/
protected static function foldsFilenameCase(string $path): bool
{
touch($path . '/case-probe');
$folds = file_exists($path . '/CASE-PROBE');
unlink($path . '/case-probe');
return $folds;
}
/**
* General tests
* Can be overwritten in a specific adapter if required, such as None cache
@@ -32,7 +79,7 @@ abstract class Base extends TestCase
// test $data string
$result = self::$cache->save($this->key, $this->data, $this->key);
$this->assertEquals($this->data, $result);
$this->assertSame($this->data, $result);
}
public function testNotEmptyCacheKey(): void
@@ -60,7 +107,7 @@ abstract class Base extends TestCase
public function testCacheTouch(): void
{
$result = self::$cache->save('touch-key', 'touch data', 'touch-key');
$this->assertEquals('touch data', $result);
$this->assertSame('touch data', $result);
sleep(3);
@@ -76,7 +123,7 @@ abstract class Base extends TestCase
{
// Ensure case in-sensitivity first (configured in adapter's setUp)
$data = self::$cache->save('planet', 'Earth', 'planet');
$this->assertEquals('Earth', $data);
$this->assertSame('Earth', $data);
$data = self::$cache->load('planet', 60 * 60 * 24 * 30 * 3 /* 3 months */, 'planet');
$this->assertEquals('Earth', $data);
@@ -92,12 +139,14 @@ abstract class Base extends TestCase
$this->assertEquals(false, $data);
$data = self::$cache->load('PLANET', 60 * 60 * 24 * 30 * 3 /* 3 months */, 'PLANET');
$this->assertEquals(false, $data);
}
// Test case sensitivity
public function testCaseSensitivity(): void
{
self::$cache->setCaseSensitivity(true);
$data = self::$cache->save('color', 'pink', 'color');
$this->assertEquals('pink', $data);
$this->assertSame('pink', $data);
$data = self::$cache->load('color', 60 * 60 * 24 * 30 * 3 /* 3 months */, 'color');
$this->assertEquals('pink', $data);
$data = self::$cache->load('COLOR', 60 * 60 * 24 * 30 * 3 /* 3 months */, 'COLOR');
@@ -105,6 +154,8 @@ abstract class Base extends TestCase
$result = self::$cache->purge('color');
$this->assertEquals(true, $result);
self::$cache->setCaseSensitivity(false);
}
public function testPing(): void
-88
View File
@@ -1,88 +0,0 @@
<?php
namespace Utopia\Tests\E2E;
use Utopia\Cache\Adapter\Filesystem;
use Utopia\Cache\Cache;
class FilesystemTest extends Base
{
public static function setUpBeforeClass(): void
{
$path = __DIR__.'/tests/data';
if (! file_exists($path)) {
mkdir($path, 0777, true);
}
self::$cache = new Cache(new Filesystem($path));
}
public function testGetSize(): void
{
self::$cache->save('test', 'test');
$this->assertEquals(4, self::$cache->getSize());
}
public function testStreamingLoad(): void
{
$path = __DIR__.'/tests/stream-data';
try {
if (! file_exists($path)) {
mkdir($path, 0777, true);
}
$cache = new Cache(new Filesystem($path, true));
$cache->save('stream-test', 'stream data');
$stream = $cache->load('stream-test', 60);
$this->assertTrue(is_resource($stream));
$this->assertEquals('stream data', stream_get_contents($stream));
fclose($stream);
} finally {
$this->deletePath($path);
}
}
public function testStreamingLoadMissingKey(): void
{
$path = __DIR__.'/tests/stream-missing-data';
try {
if (! file_exists($path)) {
mkdir($path, 0777, true);
}
$cache = new Cache(new Filesystem($path, true));
$this->assertEquals(false, $cache->load('missing-stream-test', 60));
} finally {
$this->deletePath($path);
}
}
private function deletePath(string $path): void
{
if (! file_exists($path)) {
return;
}
if (is_file($path)) {
unlink($path);
return;
}
$files = glob($path.'/*');
if ($files !== false) {
foreach ($files as $file) {
$this->deletePath($file);
}
}
rmdir($path);
}
}
+18 -15
View File
@@ -1,53 +1,56 @@
<?php
declare(strict_types=1);
namespace Utopia\Tests\E2E;
use Memcached as Memcached;
use MemcachedException;
use Utopia\Cache\Adapter\Hazelcast as HazelcastAdapter;
use Utopia\Cache\Cache;
use Utopia\Tests\Base;
use Utopia\Tests\Services;
class HazelcastTest extends Base
final class HazelcastTest extends Base
{
public static function setUpBeforeClass(): void
{
$memcached = new Memcached();
$memcached->addServer('hazelcast', 5701);
$memcached->addServer(Services::HOST, Services::HAZELCAST_PORT);
self::$cache = new Cache(new HazelcastAdapter($memcached));
}
public function testGetSize(): void
{
$this->assertEquals(0, self::$cache->getSize());
$this->assertSame(0, self::$cache->getSize());
}
public function testCacheReconnect(): void
{
$memcached = new Memcached();
$memcached->addServer('hazelcast', 5701);
self::$cache = new Cache((new HazelcastAdapter($memcached))->setMaxRetries(3));
$memcached->addServer(Services::HOST, Services::HAZELCAST_PORT);
self::$cache = new Cache(new HazelcastAdapter($memcached)->setMaxRetries(3));
self::$cache->save('test:reconnect', 'reconnect', 'test:reconnect');
$stopCmd = 'docker ps -a --filter "name=hazelcast" --format "{{.Names}}" | xargs -r docker stop';
exec($stopCmd.' 2>&1', $output, $exitCode);
$this->assertEquals(0, $exitCode, "Docker stop failed: $stopCmd\nOutput: ".implode("\n", $output));
Services::compose('stop', 'hazelcast');
sleep(3);
try {
self::$cache->load('test:reconnect', 5);
$this->fail('Hazelcast connection should have failed');
} catch (\MemcachedException $e) {
} catch (MemcachedException) {
}
$output = [];
$startCmd = 'docker ps -a --filter "name=hazelcast" --format "{{.Names}}" | xargs -r docker start';
exec($startCmd.' 2>&1', $output, $exitCode);
$this->assertEquals(0, $exitCode, "Docker start failed: $startCmd\nOutput: ".implode("\n", $output));
sleep(6);
Services::compose('up', '-d', '--wait', 'hazelcast');
// Reconnecting is what the adapter is on the hook for; serving the
// first write afterwards is Hazelcast's own start-up.
Services::waitUntil(fn(): bool => self::$cache->save('test:ready', 'ready') !== false);
$this->assertEquals('reconnect', self::$cache->save('test:reconnect', 'reconnect', 'test:reconnect'));
$this->assertSame('reconnect', self::$cache->save('test:reconnect', 'reconnect', 'test:reconnect'));
$this->assertEquals('reconnect', self::$cache->load('test:reconnect', 5));
}
#[\Override]
public function testFlush(): void
{
//not implemented as Hazelcast doesn't support flush functionality
+17 -15
View File
@@ -1,52 +1,54 @@
<?php
declare(strict_types=1);
namespace Utopia\Tests\E2E;
use Memcached as Memcached;
use MemcachedException;
use Utopia\Cache\Adapter\Memcached as MemcachedAdapter;
use Utopia\Cache\Cache;
use Utopia\Tests\Base;
use Utopia\Tests\Services;
class MemcachedTest extends Base
final class MemcachedTest extends Base
{
public static function setUpBeforeClass(): void
{
$mc = new Memcached();
$mc->addServer('memcached', 11211);
$mc->addServer(Services::HOST, Services::MEMCACHED_PORT);
self::$cache = new Cache(new MemcachedAdapter($mc));
}
public function testGetSize(): void
{
// Memcached expires flushed items lazily, so they can still be counted
// — measure the delta rather than the absolute item count.
$before = self::$cache->getSize();
self::$cache->save('test:file33', 'file33');
$this->assertEquals(1, self::$cache->getSize());
$this->assertSame($before + 1, self::$cache->getSize());
}
public function testCacheReconnect(): void
{
$mc = new Memcached();
$mc->addServer('memcached', 11211);
self::$cache = new Cache((new MemcachedAdapter($mc))->setMaxRetries(3));
$mc->addServer(Services::HOST, Services::MEMCACHED_PORT);
self::$cache = new Cache(new MemcachedAdapter($mc)->setMaxRetries(3));
self::$cache->save('test:reconnect', 'reconnect');
$stopCmd = 'docker ps -a --filter "name=memcached" --format "{{.Names}}" | xargs -r docker stop';
exec($stopCmd.' 2>&1', $output, $exitCode);
$this->assertEquals(0, $exitCode, "Docker stop failed: $stopCmd\nOutput: ".implode("\n", $output));
Services::compose('stop', 'memcached');
sleep(3);
try {
self::$cache->load('test:reconnect', 5);
$this->fail('Memcached connection should have failed');
} catch (\MemcachedException $e) {
} catch (MemcachedException) {
}
$output = [];
$startCmd = 'docker ps -a --filter "name=memcached" --format "{{.Names}}" | xargs -r docker start';
exec($startCmd.' 2>&1', $output, $exitCode);
$this->assertEquals(0, $exitCode, "Docker start failed: $startCmd\nOutput: ".implode("\n", $output));
sleep(3);
Services::compose('up', '-d', '--wait', 'memcached');
$this->assertEquals('reconnect', self::$cache->save('test:reconnect', 'reconnect', 'test:reconnect'));
$this->assertSame('reconnect', self::$cache->save('test:reconnect', 'reconnect', 'test:reconnect'));
$this->assertEquals('reconnect', self::$cache->load('test:reconnect', 5));
}
}
@@ -1,14 +1,19 @@
<?php
namespace Utopia\Tests\E2E\Redis;
declare(strict_types=1);
namespace Utopia\Tests\E2E;
use PHPUnit\Framework\TestCase;
use Redis as Redis;
use function Swoole\Coroutine\run;
use Utopia\Cache\Adapter\Redis\Multiplexing as RedisMultiplexing;
use Utopia\Cache\Cache;
use Utopia\Tests\Services;
class LeasableMultiplexingTest extends TestCase
final class MultiplexingLeasableTest extends TestCase
{
private RedisMultiplexing $adapter;
@@ -20,7 +25,7 @@ class LeasableMultiplexingTest extends TestCase
private function runCo(callable $fn): void
{
$error = null;
run(function () use ($fn, &$error) {
run(function () use ($fn, &$error): void {
try {
$fn();
} catch (\Throwable $th) {
@@ -29,21 +34,21 @@ class LeasableMultiplexingTest extends TestCase
$this->close();
}
});
if ($error !== null) {
if ($error instanceof \Throwable) {
throw $error;
}
}
private function makeCache(): Cache
{
$this->adapter = new RedisMultiplexing('redis', 6379);
$this->adapter = new RedisMultiplexing(Services::HOST, Services::REDIS_PORT);
return new Cache($this->adapter);
}
private function graceCache(int $milliseconds): Cache
{
$this->adapter = new RedisMultiplexing('redis', 6379);
$this->adapter = new RedisMultiplexing(Services::HOST, Services::REDIS_PORT);
$this->adapter->setLeaseGraceWindow($milliseconds);
return new Cache($this->adapter);
@@ -58,7 +63,7 @@ class LeasableMultiplexingTest extends TestCase
public function testFreshKeyHasZeroGeneration(): void
{
$this->runCo(function () {
$this->runCo(function (): void {
$cache = $this->makeCache();
$cache->flush();
@@ -68,7 +73,7 @@ class LeasableMultiplexingTest extends TestCase
public function testSaveWithLeaseStoresWhenGenerationUnchanged(): void
{
$this->runCo(function () {
$this->runCo(function (): void {
$cache = $this->makeCache();
$cache->flush();
@@ -81,7 +86,7 @@ class LeasableMultiplexingTest extends TestCase
public function testPurgeAdvancesGeneration(): void
{
$this->runCo(function () {
$this->runCo(function (): void {
$cache = $this->makeCache();
$cache->flush();
@@ -95,7 +100,7 @@ class LeasableMultiplexingTest extends TestCase
public function testHashPurgeAdvancesGeneration(): void
{
$this->runCo(function () {
$this->runCo(function (): void {
$cache = $this->makeCache();
$cache->flush();
@@ -121,7 +126,7 @@ class LeasableMultiplexingTest extends TestCase
*/
public function testStaleSaveAfterPurgeIsRejected(): void
{
$this->runCo(function () {
$this->runCo(function (): void {
$cache = $this->makeCache();
$cache->flush();
@@ -140,7 +145,7 @@ class LeasableMultiplexingTest extends TestCase
public function testFreshSaveAfterPurgeSucceeds(): void
{
$this->runCo(function () {
$this->runCo(function (): void {
$cache = $this->makeCache();
$cache->flush();
@@ -160,7 +165,7 @@ class LeasableMultiplexingTest extends TestCase
*/
public function testLeaseSurvivesScriptCacheFlush(): void
{
$this->runCo(function () {
$this->runCo(function (): void {
$cache = $this->makeCache();
$cache->flush();
@@ -169,7 +174,7 @@ class LeasableMultiplexingTest extends TestCase
// Evict every cached script server-side; the next EVALSHA now misses.
$raw = new Redis();
$raw->connect('redis', 6379);
$raw->connect(Services::HOST, Services::REDIS_PORT);
$raw->script('flush');
$raw->close();
@@ -179,7 +184,7 @@ class LeasableMultiplexingTest extends TestCase
$fresh = $cache->getGeneration('doc:1');
$this->assertNotFalse(
$cache->saveWithLease('doc:1', ['v' => 2], 'doc:1', $fresh),
'saveWithLease must fall back to EVAL after NOSCRIPT'
'saveWithLease must fall back to EVAL after NOSCRIPT',
);
$this->assertSame(['v' => 2], $cache->load('doc:1', 60, 'doc:1'));
});
@@ -187,7 +192,7 @@ class LeasableMultiplexingTest extends TestCase
public function testPurgePreservesDeletionResult(): void
{
$this->runCo(function () {
$this->runCo(function (): void {
$cache = $this->makeCache();
$cache->flush();
@@ -200,7 +205,7 @@ class LeasableMultiplexingTest extends TestCase
public function testPurgeDropsValueButKeepsGenerationField(): void
{
$this->runCo(function () {
$this->runCo(function (): void {
$cache = $this->makeCache();
$cache->flush();
@@ -221,7 +226,7 @@ class LeasableMultiplexingTest extends TestCase
public function testReservedGenerationFieldIsProtected(): void
{
$this->runCo(function () {
$this->runCo(function (): void {
$cache = $this->makeCache();
$cache->flush();
@@ -245,7 +250,7 @@ class LeasableMultiplexingTest extends TestCase
public function testListHidesGenerationField(): void
{
$this->runCo(function () {
$this->runCo(function (): void {
$cache = $this->makeCache();
$cache->flush();
@@ -261,8 +266,8 @@ class LeasableMultiplexingTest extends TestCase
public function testLeaseGraceWindowDefaultsToZero(): void
{
$this->runCo(function () {
$this->adapter = new RedisMultiplexing('redis', 6379);
$this->runCo(function (): void {
$this->adapter = new RedisMultiplexing(Services::HOST, Services::REDIS_PORT);
$this->assertSame(0, $this->adapter->getLeaseGraceWindow());
});
}
@@ -273,7 +278,7 @@ class LeasableMultiplexingTest extends TestCase
*/
public function testTombstoneRejectsTokenValidSaveWithinGraceWindow(): void
{
$this->runCo(function () {
$this->runCo(function (): void {
$cache = $this->graceCache(500);
$cache->flush();
@@ -282,7 +287,7 @@ class LeasableMultiplexingTest extends TestCase
$generation = $cache->getGeneration('doc:1');
$this->assertFalse(
$cache->saveWithLease('doc:1', ['stale' => true], 'doc:1', $generation),
'A token-valid save inside the grace window must be refused by the tombstone'
'A token-valid save inside the grace window must be refused by the tombstone',
);
$this->assertFalse($cache->load('doc:1', 60, 'doc:1'), 'Cache must not hold the stale value');
@@ -290,7 +295,7 @@ class LeasableMultiplexingTest extends TestCase
// multiplexing adapter exposes no raw write, so use a temporary
// synchronous php-redis client for this out-of-band poke.
$raw = new Redis();
$raw->connect('redis', 6379);
$raw->connect(Services::HOST, Services::REDIS_PORT);
// Derive from the Redis server clock (what the tombstone Lua uses via
// TIME), not the PHP wall clock, so a client/server skew can't flake it.
$past = ((int) $raw->time()[0] - 60) * 1000000;
@@ -299,17 +304,17 @@ class LeasableMultiplexingTest extends TestCase
$this->assertNotFalse(
$cache->saveWithLease('doc:1', ['fresh' => true], 'doc:1', $generation),
'After the grace window a token-valid save must succeed'
'After the grace window a token-valid save must succeed',
);
$this->assertSame(['fresh' => true], $cache->load('doc:1', 60, 'doc:1'));
$raw = new Redis();
$raw->connect('redis', 6379);
$raw->connect(Services::HOST, Services::REDIS_PORT);
$lingering = (bool) $raw->hExists('doc:1', '__utopia_tomb__');
$raw->close();
$this->assertFalse(
$lingering,
'The spent tombstone must be dropped on the next save, not linger in the hash'
'The spent tombstone must be dropped on the next save, not linger in the hash',
);
});
}
@@ -317,7 +322,7 @@ class LeasableMultiplexingTest extends TestCase
/** A field-level purge must open the tombstone too. */
public function testFieldPurgeAlsoOpensTombstone(): void
{
$this->runCo(function () {
$this->runCo(function (): void {
$cache = $this->graceCache(500);
$cache->flush();
@@ -327,7 +332,7 @@ class LeasableMultiplexingTest extends TestCase
$generation = $cache->getGeneration('doc:1');
$this->assertFalse(
$cache->saveWithLease('doc:1', ['stale' => true], 'field-a', $generation),
'A token-valid field save inside the grace window must be refused'
'A token-valid field save inside the grace window must be refused',
);
$this->assertFalse($cache->load('doc:1', 60, 'field-a'));
});
@@ -335,7 +340,7 @@ class LeasableMultiplexingTest extends TestCase
public function testListHidesTombstoneField(): void
{
$this->runCo(function () {
$this->runCo(function (): void {
$cache = $this->graceCache(500);
$cache->flush();
@@ -348,7 +353,7 @@ class LeasableMultiplexingTest extends TestCase
public function testReservedTombstoneFieldIsProtected(): void
{
$this->runCo(function () {
$this->runCo(function (): void {
$cache = $this->graceCache(500);
$cache->flush();
@@ -365,7 +370,7 @@ class LeasableMultiplexingTest extends TestCase
*/
public function testTombstoneIgnoresImplausibleFutureDeadline(): void
{
$this->runCo(function () {
$this->runCo(function (): void {
$cache = $this->graceCache(500);
$cache->flush();
@@ -376,14 +381,14 @@ class LeasableMultiplexingTest extends TestCase
// The multiplexing adapter exposes no raw write, so stamp it via a
// temporary synchronous php-redis client.
$raw = new Redis();
$raw->connect('redis', 6379);
$raw->connect(Services::HOST, Services::REDIS_PORT);
$farFutureMicros = ((int) $raw->time()[0] + 3600) * 1000000;
$raw->hSet('doc:1', '__utopia_tomb__', (string) $farFutureMicros);
$raw->close();
$this->assertNotFalse(
$cache->saveWithLease('doc:1', ['v' => 1], 'doc:1', $generation),
'An implausibly far-future deadline (backward clock step) must be ignored, not wedge saves'
'An implausibly far-future deadline (backward clock step) must be ignored, not wedge saves',
);
$this->assertSame(['v' => 1], $cache->load('doc:1', 60, 'doc:1'));
});
@@ -1,15 +1,20 @@
<?php
namespace Utopia\Tests\E2E\Redis;
declare(strict_types=1);
namespace Utopia\Tests\E2E;
use PHPUnit\Framework\TestCase;
use Swoole\Coroutine;
use function Swoole\Coroutine\run;
use Swoole\Coroutine\WaitGroup;
use Utopia\Cache\Adapter\Redis\Multiplexing as RedisMultiplexing;
use Utopia\Cache\Cache;
use Utopia\Tests\Services;
class MultiplexingTest extends TestCase
final class MultiplexingTest extends TestCase
{
protected string $key = 'test-key-for-cache';
@@ -24,7 +29,7 @@ class MultiplexingTest extends TestCase
private function runCo(callable $fn): void
{
$error = null;
run(function () use ($fn, &$error) {
run(function () use ($fn, &$error): void {
try {
$fn();
} catch (\Throwable $th) {
@@ -33,7 +38,7 @@ class MultiplexingTest extends TestCase
$this->close();
}
});
if ($error !== null) {
if ($error instanceof \Throwable) {
throw $error;
}
}
@@ -42,7 +47,7 @@ class MultiplexingTest extends TestCase
private function makeCache(): Cache
{
$this->adapter = new RedisMultiplexing('redis', 6379);
$this->adapter = new RedisMultiplexing(Services::HOST, Services::REDIS_PORT);
return new Cache($this->adapter);
}
@@ -56,7 +61,7 @@ class MultiplexingTest extends TestCase
public function testCacheSave(): void
{
$this->runCo(function () {
$this->runCo(function (): void {
$cache = $this->makeCache();
$cache->flush();
@@ -64,7 +69,7 @@ class MultiplexingTest extends TestCase
$this->assertEquals($this->dataArray, $result);
$result = $cache->save($this->key, $this->data, $this->key);
$this->assertEquals($this->data, $result);
$this->assertSame($this->data, $result);
$loaded = $cache->load($this->key, 60, $this->key);
$this->assertEquals($this->data, $loaded);
@@ -73,7 +78,7 @@ class MultiplexingTest extends TestCase
public function testCachePurge(): void
{
$this->runCo(function () {
$this->runCo(function (): void {
$cache = $this->makeCache();
$cache->save($this->key, $this->data, $this->key);
@@ -87,7 +92,7 @@ class MultiplexingTest extends TestCase
public function testCacheTouch(): void
{
$this->runCo(function () {
$this->runCo(function (): void {
$cache = $this->makeCache();
$cache->save('touch-key', 'touch data', 'touch-key');
@@ -95,7 +100,7 @@ class MultiplexingTest extends TestCase
$this->assertFalse($cache->load('touch-key', 2, 'touch-key'));
$this->assertTrue($cache->touch('touch-key', 'touch-key'));
$this->assertEquals('touch data', $cache->load('touch-key', 2, 'touch-key'));
$this->assertSame('touch data', $cache->load('touch-key', 2, 'touch-key'));
$this->assertFalse($cache->touch('missing-touch-key', 'missing-touch-key'));
$cache->purge('touch-key');
@@ -104,7 +109,7 @@ class MultiplexingTest extends TestCase
public function testPing(): void
{
$this->runCo(function () {
$this->runCo(function (): void {
$cache = $this->makeCache();
$this->assertTrue($cache->ping());
});
@@ -112,7 +117,7 @@ class MultiplexingTest extends TestCase
public function testFlush(): void
{
$this->runCo(function () {
$this->runCo(function (): void {
$cache = $this->makeCache();
$cache->save('x', 'x', 'x');
$cache->save('y', 'y', 'y');
@@ -128,7 +133,7 @@ class MultiplexingTest extends TestCase
public function testGetSize(): void
{
$this->runCo(function () {
$this->runCo(function (): void {
$cache = $this->makeCache();
$cache->flush();
@@ -136,13 +141,13 @@ class MultiplexingTest extends TestCase
$cache->save('test:file34', 'file34', 'test:file34');
$cache->save('test:file35', 'file35', 'test:file35');
$this->assertEquals(3, $cache->getSize());
$this->assertSame(3, $cache->getSize());
});
}
public function testJsonData(): void
{
$this->runCo(function () {
$this->runCo(function (): void {
$cache = $this->makeCache();
$cache->flush();
@@ -171,14 +176,14 @@ class MultiplexingTest extends TestCase
$jsonString = json_encode($payload, JSON_THROW_ON_ERROR);
$this->assertNotFalse($jsonString);
$this->assertEquals($jsonString, $cache->save('json:string', $jsonString, 'json:string'));
$this->assertSame($jsonString, $cache->save('json:string', $jsonString, 'json:string'));
$this->assertEquals($jsonString, $cache->load('json:string', 60, 'json:string'));
});
}
public function testLargeJsonPayload(): void
{
$this->runCo(function () {
$this->runCo(function (): void {
$cache = $this->makeCache();
$cache->flush();
@@ -186,7 +191,7 @@ class MultiplexingTest extends TestCase
for ($i = 0; $i < 1000; $i++) {
$rows[] = [
'i' => $i,
'name' => 'row-'.$i,
'name' => 'row-' . $i,
'value' => str_repeat('x', 64),
];
}
@@ -202,7 +207,7 @@ class MultiplexingTest extends TestCase
public function testConcurrentJsonMultiplexing(): void
{
$this->runCo(function () {
$this->runCo(function (): void {
$cache = $this->makeCache();
$cache->flush();
@@ -212,14 +217,14 @@ class MultiplexingTest extends TestCase
for ($i = 0; $i < $count; $i++) {
$wg->add();
Coroutine::create(function () use ($cache, $i, $wg, &$errors) {
Coroutine::create(function () use ($cache, $i, $wg, &$errors): void {
try {
$key = 'json-mux:'.$i;
$key = 'json-mux:' . $i;
$value = [
'i' => $i,
'tags' => ['a', 'b', 'c-'.$i],
'tags' => ['a', 'b', 'c-' . $i],
'meta' => ['nested' => ['count' => $i * 2]],
'text' => 'café 🚀 row '.$i,
'text' => 'café 🚀 row ' . $i,
];
$saved = $cache->save($key, $value, $key);
if ($saved !== $value) {
@@ -227,7 +232,7 @@ class MultiplexingTest extends TestCase
}
$loaded = $cache->load($key, 60, $key);
if ($loaded != $value) {
$errors[] = "load mismatch for $i: ".var_export($loaded, true);
$errors[] = "load mismatch for $i: " . var_export($loaded, true);
}
} catch (\Throwable $th) {
$errors[] = $th->getMessage();
@@ -239,8 +244,8 @@ class MultiplexingTest extends TestCase
$wg->wait();
$this->assertEmpty($errors, 'Concurrent errors: '.implode('; ', $errors));
$this->assertEquals($count, $cache->getSize());
$this->assertEmpty($errors, 'Concurrent errors: ' . implode('; ', $errors));
$this->assertSame($count, $cache->getSize());
});
}
@@ -249,7 +254,7 @@ class MultiplexingTest extends TestCase
*/
public function testConcurrentMultiplexing(): void
{
$this->runCo(function () {
$this->runCo(function (): void {
$cache = $this->makeCache();
$cache->flush();
@@ -259,17 +264,17 @@ class MultiplexingTest extends TestCase
for ($i = 0; $i < $count; $i++) {
$wg->add();
Coroutine::create(function () use ($cache, $i, $wg, &$errors) {
Coroutine::create(function () use ($cache, $i, $wg, &$errors): void {
try {
$key = 'mux:'.$i;
$value = 'value-'.$i;
$key = 'mux:' . $i;
$value = 'value-' . $i;
$saved = $cache->save($key, $value, $key);
if ($saved !== $value) {
$errors[] = "save mismatch for $i: ".var_export($saved, true);
$errors[] = "save mismatch for $i: " . var_export($saved, true);
}
$loaded = $cache->load($key, 60, $key);
if ($loaded !== $value) {
$errors[] = "load mismatch for $i: ".var_export($loaded, true);
$errors[] = "load mismatch for $i: " . var_export($loaded, true);
}
} catch (\Throwable $th) {
$errors[] = $th->getMessage();
@@ -281,8 +286,8 @@ class MultiplexingTest extends TestCase
$wg->wait();
$this->assertEmpty($errors, 'Concurrent errors: '.implode('; ', $errors));
$this->assertEquals($count, $cache->getSize());
$this->assertEmpty($errors, 'Concurrent errors: ' . implode('; ', $errors));
$this->assertSame($count, $cache->getSize());
});
}
}
@@ -1,83 +0,0 @@
<?php
namespace Utopia\Tests\E2E\Redis;
use RedisCluster as RedisCluster;
use Utopia\Cache\Adapter\RedisCluster as RedisAdapter;
use Utopia\Cache\Cache;
use Utopia\Tests\E2E\Base;
const SEEDS = [
'redis-cluster:7000',
'redis-cluster:7001',
'redis-cluster:7002',
];
const TIMEOUT = 1.5;
class RedisClusterTest extends Base
{
protected static RedisCluster $redis;
public static function setUpBeforeClass(): void
{
self::$redis = new RedisCluster(null, SEEDS, TIMEOUT, TIMEOUT);
self::$cache = new Cache(new RedisAdapter(self::$redis, SEEDS));
}
public function testGetSize(): void
{
for ($i = 0; $i < 20; $i++) {
self::$cache->save("test:file$i", "file$i", "test:file$i");
}
$this->assertEquals(20, self::$cache->getSize());
}
/**
* @depends testGetSize
*/
public function testCacheReconnect(): void
{
self::$redis = new RedisCluster(null, SEEDS, TIMEOUT, TIMEOUT);
self::$cache = new Cache((new RedisAdapter(self::$redis, SEEDS))->setMaxRetries(3));
self::$cache->save('test:reconnect', 'reconnect', 'test:reconnect');
// Must recreate container because grokzen/redis-cluster doesn't persist cluster state across stop/start
$rmCmd = 'docker rm -f redis-cluster';
exec($rmCmd.' 2>&1', $output, $exitCode);
$this->assertEquals(0, $exitCode, "Docker rm failed: $rmCmd\nOutput: ".implode("\n", $output));
sleep(1);
try {
self::$cache->load('test:reconnect', 5);
$this->fail('Redis connection should have failed');
} catch (\RedisClusterException $e) {
}
$output = [];
$runCmd = 'docker run -d --name redis-cluster --hostname redis-cluster --network cache_database -e IP=redis-cluster grokzen/redis-cluster:7.0.10';
exec($runCmd.' 2>&1', $output, $exitCode);
$this->assertEquals(0, $exitCode, "Docker run failed: $runCmd\nOutput: ".implode("\n", $output));
// Wait for cluster to be ready with retry logic (max 60 seconds) to reduce flaky tests
$maxWait = 60;
$waited = 0;
$saveResult = false;
while ($waited < $maxWait && $saveResult === false) {
try {
$saveResult = self::$cache->save('test:reconnect', 'reconnect', 'test:reconnect');
if ($saveResult !== false) {
break;
}
} catch (\RedisClusterException $e) {
// Exception thrown, will retry
}
sleep(5);
$waited += 5;
}
$this->assertEquals('reconnect', self::$cache->save('test:reconnect', 'reconnect', 'test:reconnect'));
$this->assertEquals('reconnect', self::$cache->load('test:reconnect', 5));
}
}
-91
View File
@@ -1,91 +0,0 @@
<?php
namespace Utopia\Tests\E2E\Redis;
use Redis as Redis;
use Utopia\Cache\Adapter\Redis as RedisAdapter;
use Utopia\Cache\Cache;
use Utopia\Tests\E2E\Base;
class RedisTest extends Base
{
public static function setUpBeforeClass(): void
{
$redis = new Redis();
$redis->connect('redis', 6379);
self::$cache = new Cache(new RedisAdapter($redis));
}
public function testGetSize(): void
{
self::$cache->flush();
self::$cache->save('test:file33', 'file33', 'test:file33');
self::$cache->save('test:file34', 'file34', 'test:file34');
self::$cache->save('test:file35', 'file35', 'test:file35');
$this->assertEquals(3, self::$cache->getSize());
}
/**
* @depends testGetSize
*/
public function testCacheReconnect(): void
{
$redis = new Redis();
$redis->connect('redis', 6379);
self::$cache = new Cache((new RedisAdapter($redis))->setMaxRetries(3));
self::$cache->save('test:reconnect', 'reconnect', 'test:reconnect');
$stopCmd = 'docker ps -a --filter "name=cache-redis" --format "{{.Names}}" | xargs -r docker stop';
exec($stopCmd.' 2>&1', $output, $exitCode);
$this->assertEquals(0, $exitCode, "Docker stop failed: $stopCmd\nOutput: ".implode("\n", $output));
sleep(1);
try {
self::$cache->load('test:reconnect', 5);
$this->fail('Redis connection should have failed');
} catch (\RedisException $e) {
}
$output = [];
$startCmd = 'docker ps -a --filter "name=cache-redis" --format "{{.Names}}" | xargs -r docker start';
exec($startCmd.' 2>&1', $output, $exitCode);
$this->assertEquals(0, $exitCode, "Docker start failed: $startCmd\nOutput: ".implode("\n", $output));
sleep(3);
$this->assertEquals('reconnect', self::$cache->save('test:reconnect', 'reconnect', 'test:reconnect'));
$this->assertEquals('reconnect', self::$cache->load('test:reconnect', 5));
}
/**
* @depends testCacheReconnect
*/
public function testCacheReconnectPersistent(): void
{
$redis = new Redis();
$redis->pconnect('redis', 6379);
self::$cache = new Cache((new RedisAdapter($redis))->setMaxRetries(3));
self::$cache->save('test:reconnect_persistent', 'reconnect_persistent', 'test:reconnect_persistent');
$stopCmd = 'docker ps -a --filter "name=cache-redis" --format "{{.Names}}" | xargs -r docker stop';
exec($stopCmd.' 2>&1', $output, $exitCode);
$this->assertEquals(0, $exitCode, "Docker stop failed: $stopCmd\nOutput: ".implode("\n", $output));
sleep(1);
try {
self::$cache->load('test:reconnect_persistent', 5);
$this->fail('Redis connection should have failed');
} catch (\RedisException $e) {
}
$output = [];
$startCmd = 'docker ps -a --filter "name=cache-redis" --format "{{.Names}}" | xargs -r docker start';
exec($startCmd.' 2>&1', $output, $exitCode);
$this->assertEquals(0, $exitCode, "Docker start failed: $startCmd\nOutput: ".implode("\n", $output));
sleep(3);
$this->assertEquals('reconnect_persistent', self::$cache->save('test:reconnect_persistent', 'reconnect_persistent', 'test:reconnect_persistent'));
$this->assertEquals('reconnect_persistent', self::$cache->load('test:reconnect_persistent', 5));
}
}
+60
View File
@@ -0,0 +1,60 @@
<?php
declare(strict_types=1);
namespace Utopia\Tests\E2E;
use PHPUnit\Framework\Attributes\Depends;
use RedisCluster as RedisCluster;
use RedisClusterException;
use Utopia\Cache\Adapter\RedisCluster as RedisAdapter;
use Utopia\Cache\Cache;
use Utopia\Tests\Base;
use Utopia\Tests\Services;
final class RedisClusterTest extends Base
{
private const float TIMEOUT = 1.5;
protected static RedisCluster $redis;
public static function setUpBeforeClass(): void
{
self::$redis = new RedisCluster(null, Services::CLUSTER_SEEDS, self::TIMEOUT, self::TIMEOUT);
self::$cache = new Cache(new RedisAdapter(self::$redis, Services::CLUSTER_SEEDS));
}
public function testGetSize(): void
{
for ($i = 0; $i < 20; $i++) {
self::$cache->save("test:file$i", "file$i", "test:file$i");
}
$this->assertSame(20, self::$cache->getSize());
}
#[Depends('testGetSize')]
public function testCacheReconnect(): void
{
self::$redis = new RedisCluster(null, Services::CLUSTER_SEEDS, self::TIMEOUT, self::TIMEOUT);
self::$cache = new Cache(new RedisAdapter(self::$redis, Services::CLUSTER_SEEDS)->setMaxRetries(3));
self::$cache->save('test:reconnect', 'reconnect', 'test:reconnect');
// The container must be recreated: grokzen/redis-cluster does not
// persist cluster state across a stop and start.
Services::compose('rm', '-sf', 'redis-cluster');
sleep(1);
try {
self::$cache->load('test:reconnect', 5);
$this->fail('Redis connection should have failed');
} catch (RedisClusterException) {
}
Services::compose('up', '-d', '--force-recreate', '--wait', 'redis-cluster');
$this->assertSame('reconnect', self::$cache->save('test:reconnect', 'reconnect', 'test:reconnect'));
$this->assertEquals('reconnect', self::$cache->load('test:reconnect', 5));
}
}
@@ -1,20 +1,23 @@
<?php
namespace Utopia\Tests\E2E\Redis;
declare(strict_types=1);
namespace Utopia\Tests\E2E;
use PHPUnit\Framework\TestCase;
use Redis as Redis;
use Utopia\Cache\Adapter\Redis as RedisAdapter;
use Utopia\Cache\Cache;
use Utopia\Tests\Services;
class LeasableTest extends TestCase
final class RedisLeasableTest extends TestCase
{
protected Cache $cache;
protected function setUp(): void
{
$redis = new Redis();
$redis->connect('redis', 6379);
$redis->connect(Services::HOST, Services::REDIS_PORT);
$this->cache = new Cache(new RedisAdapter($redis));
$this->cache->flush();
}
@@ -100,7 +103,7 @@ class LeasableTest extends TestCase
// Evict every cached script server-side; the next EVALSHA now misses.
$raw = new Redis();
$raw->connect('redis', 6379);
$raw->connect(Services::HOST, Services::REDIS_PORT);
$raw->script('flush');
$raw->close();
@@ -110,7 +113,7 @@ class LeasableTest extends TestCase
$fresh = $this->cache->getGeneration('doc:1');
$this->assertNotFalse(
$this->cache->saveWithLease('doc:1', ['v' => 2], 'doc:1', $fresh),
'saveWithLease must fall back to EVAL after NOSCRIPT'
'saveWithLease must fall back to EVAL after NOSCRIPT',
);
$this->assertSame(['v' => 2], $this->cache->load('doc:1', 60, 'doc:1'));
}
@@ -175,7 +178,7 @@ class LeasableTest extends TestCase
private function graceCache(int $milliseconds): Cache
{
$redis = new Redis();
$redis->connect('redis', 6379);
$redis->connect(Services::HOST, Services::REDIS_PORT);
$adapter = new RedisAdapter($redis);
$adapter->setLeaseGraceWindow($milliseconds);
@@ -185,8 +188,8 @@ class LeasableTest extends TestCase
public function testLeaseGraceWindowDefaultsToZero(): void
{
$redis = new Redis();
$redis->connect('redis', 6379);
$this->assertSame(0, (new RedisAdapter($redis))->getLeaseGraceWindow());
$redis->connect(Services::HOST, Services::REDIS_PORT);
$this->assertSame(0, new RedisAdapter($redis)->getLeaseGraceWindow());
}
/**
@@ -196,7 +199,7 @@ class LeasableTest extends TestCase
public function testTombstoneRejectsTokenValidSaveWithinGraceWindow(): void
{
$redis = new Redis();
$redis->connect('redis', 6379);
$redis->connect(Services::HOST, Services::REDIS_PORT);
$adapter = new RedisAdapter($redis);
$adapter->setLeaseGraceWindow(500);
$cache = new Cache($adapter);
@@ -207,7 +210,7 @@ class LeasableTest extends TestCase
$generation = $cache->getGeneration('doc:1');
$this->assertFalse(
$cache->saveWithLease('doc:1', ['stale' => true], 'doc:1', $generation),
'A token-valid save inside the grace window must be refused by the tombstone'
'A token-valid save inside the grace window must be refused by the tombstone',
);
$this->assertFalse($cache->load('doc:1', 60, 'doc:1'), 'Cache must not hold the stale value');
@@ -219,12 +222,12 @@ class LeasableTest extends TestCase
$this->assertNotFalse(
$cache->saveWithLease('doc:1', ['fresh' => true], 'doc:1', $generation),
'After the grace window a token-valid save must succeed'
'After the grace window a token-valid save must succeed',
);
$this->assertSame(['fresh' => true], $cache->load('doc:1', 60, 'doc:1'));
$this->assertFalse(
(bool) $redis->hExists('doc:1', '__utopia_tomb__'),
'The spent tombstone must be dropped on the next save, not linger in the hash'
'The spent tombstone must be dropped on the next save, not linger in the hash',
);
}
@@ -240,7 +243,7 @@ class LeasableTest extends TestCase
$generation = $cache->getGeneration('doc:1');
$this->assertFalse(
$cache->saveWithLease('doc:1', ['stale' => true], 'field-a', $generation),
'A token-valid field save inside the grace window must be refused'
'A token-valid field save inside the grace window must be refused',
);
$this->assertFalse($cache->load('doc:1', 60, 'field-a'));
}
@@ -274,7 +277,7 @@ class LeasableTest extends TestCase
public function testTombstoneIgnoresImplausibleFutureDeadline(): void
{
$redis = new Redis();
$redis->connect('redis', 6379);
$redis->connect(Services::HOST, Services::REDIS_PORT);
$adapter = new RedisAdapter($redis);
$adapter->setLeaseGraceWindow(500);
$cache = new Cache($adapter);
@@ -289,7 +292,7 @@ class LeasableTest extends TestCase
$this->assertNotFalse(
$cache->saveWithLease('doc:1', ['v' => 1], 'doc:1', $generation),
'An implausibly far-future deadline (backward clock step) must be ignored, not wedge saves'
'An implausibly far-future deadline (backward clock step) must be ignored, not wedge saves',
);
$this->assertSame(['v' => 1], $cache->load('doc:1', 60, 'doc:1'));
}
+74
View File
@@ -0,0 +1,74 @@
<?php
declare(strict_types=1);
namespace Utopia\Tests\E2E;
use PHPUnit\Framework\Attributes\Depends;
use Redis as Redis;
use RedisException;
use Utopia\Cache\Adapter\Redis as RedisAdapter;
use Utopia\Cache\Cache;
use Utopia\Tests\Base;
use Utopia\Tests\Services;
final class RedisTest extends Base
{
public static function setUpBeforeClass(): void
{
$redis = new Redis();
$redis->connect(Services::HOST, Services::REDIS_PORT);
self::$cache = new Cache(new RedisAdapter($redis));
}
public function testGetSize(): void
{
self::$cache->flush();
self::$cache->save('test:file33', 'file33', 'test:file33');
self::$cache->save('test:file34', 'file34', 'test:file34');
self::$cache->save('test:file35', 'file35', 'test:file35');
$this->assertSame(3, self::$cache->getSize());
}
#[Depends('testGetSize')]
public function testCacheReconnect(): void
{
$this->assertReconnects(persistent: false);
}
#[Depends('testCacheReconnect')]
public function testCacheReconnectPersistent(): void
{
$this->assertReconnects(persistent: true);
}
/**
* Restarts Redis underneath a live adapter and asserts it recovers.
*/
private function assertReconnects(bool $persistent): void
{
$key = $persistent ? 'test:reconnect_persistent' : 'test:reconnect';
$redis = new Redis();
$persistent
? $redis->pconnect(Services::HOST, Services::REDIS_PORT)
: $redis->connect(Services::HOST, Services::REDIS_PORT);
self::$cache = new Cache(new RedisAdapter($redis)->setMaxRetries(3));
self::$cache->save($key, 'reconnect', $key);
Services::compose('stop', 'redis');
sleep(1);
try {
self::$cache->load($key, 5);
$this->fail('Redis connection should have failed');
} catch (RedisException) {
}
Services::compose('up', '-d', '--wait', 'redis');
$this->assertSame('reconnect', self::$cache->save($key, 'reconnect', $key));
$this->assertEquals('reconnect', self::$cache->load($key, 5));
}
}
+13 -14
View File
@@ -1,5 +1,7 @@
<?php
declare(strict_types=1);
namespace Utopia\Tests\E2E;
use Redis as Redis;
@@ -7,32 +9,29 @@ use Throwable;
use Utopia\Cache\Adapter\Redis as RedisAdapter;
use Utopia\Cache\Adapter\Sharding;
use Utopia\Cache\Cache;
use Utopia\Tests\Base;
use Utopia\Tests\Services;
class ShardingTest extends Base
final class ShardingTest extends Base
{
public static function setUpBeforeClass(): void
{
$shardA = new Redis();
$shardA->connect('shardA', 6379);
$shards = [];
$shardB = new Redis();
$shardB->connect('shardB', 6379);
foreach (Services::SHARD_PORTS as $port) {
$redis = new Redis();
$redis->connect(Services::HOST, $port);
$shards[] = new RedisAdapter($redis);
}
$shardC = new Redis();
$shardC->connect('shardC', 6379);
self::$cache = new Cache(new Sharding([
new RedisAdapter($shardA),
new RedisAdapter($shardB),
new RedisAdapter($shardC),
]));
self::$cache = new Cache(new Sharding($shards));
}
public function testGetSize(): void
{
self::$cache->save('test:file33', 'file33', 'test:file33');
self::$cache->save('test:file34', 'file34', 'test:file33');
$this->assertEquals(2, self::$cache->getSize());
$this->assertSame(2, self::$cache->getSize());
}
public function testEmptyAdapters(): void
+71
View File
@@ -0,0 +1,71 @@
<?php
namespace Utopia\Tests;
use RuntimeException;
use Throwable;
/**
* Where the package's compose services listen on the host. Ports are offset so
* they never collide with a local stack — keep them in sync with
* docker-compose.yml.
*/
final class Services
{
public const string HOST = '127.0.0.1';
public const int REDIS_PORT = 16381;
public const array SHARD_PORTS = [16382, 16383, 16384];
/** The cluster advertises its own ports, so they are not offset. */
public const array CLUSTER_SEEDS = ['127.0.0.1:17000', '127.0.0.1:17001', '127.0.0.1:17002'];
public const int MEMCACHED_PORT = 11212;
public const int HAZELCAST_PORT = 15701;
/**
* Polls until the probe passes. A healthy container is not always a
* service ready to answer — Hazelcast reports its node active before the
* Memcached listener serves traffic.
*/
public static function waitUntil(callable $probe, int $seconds = 60): void
{
for ($attempt = 0; $attempt < $seconds; $attempt++) {
try {
if ($probe() === true) {
return;
}
} catch (Throwable) {
// Not up yet.
}
sleep(1);
}
throw new RuntimeException('Timed out waiting for the service to come back.');
}
/**
* Runs a docker compose command against this package's stack.
*/
public static function compose(string ...$arguments): void
{
self::docker('compose', '-f', \dirname(__DIR__, 2) . '/docker-compose.yml', ...$arguments);
}
/**
* Runs a docker command against one of the compose containers, so the
* reconnect tests can take a service down and bring it back up.
*/
public static function docker(string ...$arguments): void
{
$command = 'docker ' . implode(' ', $arguments);
exec($command . ' 2>&1', $output, $code);
if ($code !== 0) {
throw new RuntimeException("`{$command}` failed: " . implode("\n", $output));
}
}
}
@@ -1,6 +1,8 @@
<?php
namespace Utopia\Tests\E2E;
declare(strict_types=1);
namespace Utopia\Tests\Unit;
use PHPUnit\Framework\TestCase;
use RuntimeException;
@@ -14,7 +16,7 @@ use Utopia\CircuitBreaker\CircuitBreaker as UtopiaCircuitBreaker;
use Utopia\Telemetry\Adapter as Telemetry;
use Utopia\Telemetry\Adapter\Test as TestTelemetry;
class CircuitBreakerTest extends TestCase
final class CircuitBreakerTest extends TestCase
{
public function testPassesThroughHealthyCacheOperations(): void
{
@@ -68,8 +70,7 @@ class CircuitBreakerTest extends TestCase
public function testTelemetryPropagatesToInnerAdapter(): void
{
$telemetry = new TestTelemetry();
$adapter = new class extends Memory implements Feature\Telemetry
{
$adapter = new class extends Memory implements Feature\Telemetry {
public ?Telemetry $telemetry = null;
public function setTelemetry(Telemetry $telemetry): void
+69
View File
@@ -0,0 +1,69 @@
<?php
declare(strict_types=1);
namespace Utopia\Tests\Unit;
use Override;
use Utopia\Cache\Adapter\Filesystem;
use Utopia\Cache\Cache;
use Utopia\Tests\Base;
final class FilesystemTest extends Base
{
private static string $path;
public static function setUpBeforeClass(): void
{
self::$path = self::scratch('data');
self::$cache = new Cache(new Filesystem(self::$path));
}
public function testGetSize(): void
{
self::$cache->save('test', 'test');
$this->assertSame(4, self::$cache->getSize());
}
#[Override]
public function testCaseSensitivity(): void
{
if (self::foldsFilenameCase(self::$path)) {
$this->markTestSkipped('The host filesystem folds filename case.');
}
parent::testCaseSensitivity();
}
public function testStreamingLoad(): void
{
$path = self::scratch('stream-data');
try {
$cache = new Cache(new Filesystem($path, true));
$cache->save('stream-test', 'stream data');
$stream = $cache->load('stream-test', 60);
$this->assertTrue(\is_resource($stream));
$this->assertSame('stream data', stream_get_contents($stream));
fclose($stream);
} finally {
self::deletePath($path);
}
}
public function testStreamingLoadMissingKey(): void
{
$path = self::scratch('stream-missing-data');
try {
$cache = new Cache(new Filesystem($path, true));
$this->assertEquals(false, $cache->load('missing-stream-test', 60));
} finally {
self::deletePath($path);
}
}
}
+15 -26
View File
@@ -1,5 +1,7 @@
<?php
declare(strict_types=1);
namespace Utopia\Tests\Unit;
use PHPUnit\Framework\TestCase;
@@ -7,7 +9,7 @@ use Utopia\Cache\Adapter\Memory;
use Utopia\Cache\Cache;
use Utopia\Telemetry\Adapter\Test as TestTelemetry;
class LoadResultsTelemetryTest extends TestCase
final class LoadResultsTelemetryTest extends TestCase
{
public function testLoadEmitsHitAndMissCounts(): void
{
@@ -30,14 +32,11 @@ class LoadResultsTelemetryTest extends TestCase
$captured = [];
$cache = new Cache(new Memory());
$telemetry = new class($captured) extends TestTelemetry
{
$telemetry = new class ($captured) extends TestTelemetry {
/**
* @param array<int, array<string, mixed>> $captured
*/
public function __construct(public array &$captured)
{
}
public function __construct(public array &$captured) {}
public function createCounter(string $name, ?string $unit = null, ?string $description = null, array $advisory = []): \Utopia\Telemetry\Counter
{
@@ -46,18 +45,15 @@ class LoadResultsTelemetryTest extends TestCase
}
$captured = &$this->captured;
return $this->counters[$name] = new class($captured) extends \Utopia\Telemetry\Counter
{
return $this->counters[$name] = new class ($captured) extends \Utopia\Telemetry\Counter {
/**
* @param array<int, array<string, mixed>> $captured
*/
public function __construct(public array &$captured)
{
}
public function __construct(public array &$captured) {}
public function add(float|int $amount, iterable $attributes = []): void
{
$this->captured[] = \is_array($attributes) ? $attributes : \iterator_to_array($attributes);
$this->captured[] = \is_array($attributes) ? $attributes : iterator_to_array($attributes);
}
};
}
@@ -68,7 +64,7 @@ class LoadResultsTelemetryTest extends TestCase
$cache->save('here', 'value');
$cache->load('here', 60);
$results = \array_column($captured, 'result');
$results = array_column($captured, 'result');
$this->assertSame(['miss', 'hit'], $results);
}
@@ -76,8 +72,7 @@ class LoadResultsTelemetryTest extends TestCase
{
$captured = [];
$adapter = new class extends Memory
{
$adapter = new class extends Memory {
public function load(string $key, int $ttl, string $hash = ''): mixed
{
return null;
@@ -85,14 +80,11 @@ class LoadResultsTelemetryTest extends TestCase
};
$cache = new Cache($adapter);
$telemetry = new class($captured) extends TestTelemetry
{
$telemetry = new class ($captured) extends TestTelemetry {
/**
* @param array<int, array<string, mixed>> $captured
*/
public function __construct(public array &$captured)
{
}
public function __construct(public array &$captured) {}
public function createCounter(string $name, ?string $unit = null, ?string $description = null, array $advisory = []): \Utopia\Telemetry\Counter
{
@@ -101,18 +93,15 @@ class LoadResultsTelemetryTest extends TestCase
}
$captured = &$this->captured;
return $this->counters[$name] = new class($captured) extends \Utopia\Telemetry\Counter
{
return $this->counters[$name] = new class ($captured) extends \Utopia\Telemetry\Counter {
/**
* @param array<int, array<string, mixed>> $captured
*/
public function __construct(public array &$captured)
{
}
public function __construct(public array &$captured) {}
public function add(float|int $amount, iterable $attributes = []): void
{
$this->captured[] = \is_array($attributes) ? $attributes : \iterator_to_array($attributes);
$this->captured[] = \is_array($attributes) ? $attributes : iterator_to_array($attributes);
}
};
}
@@ -1,11 +1,14 @@
<?php
namespace Utopia\Tests\E2E;
declare(strict_types=1);
namespace Utopia\Tests\Unit;
use Utopia\Cache\Adapter\Memory;
use Utopia\Cache\Cache;
use Utopia\Tests\Base;
class MemoryTest extends Base
final class MemoryTest extends Base
{
public static function setUpBeforeClass(): void
{
@@ -18,6 +21,6 @@ class MemoryTest extends Base
self::$cache->save('test:file34', 'file34');
self::$cache->save('test:file35', 'file35');
self::$cache->save('test:file36', 'file36');
$this->assertEquals(4, self::$cache->getSize());
$this->assertSame(4, self::$cache->getSize());
}
}
@@ -1,11 +1,15 @@
<?php
namespace Utopia\Tests\E2E;
declare(strict_types=1);
namespace Utopia\Tests\Unit;
use PHPUnit\Framework\Attributes\Depends;
use Utopia\Cache\Adapter\None;
use Utopia\Cache\Cache;
use Utopia\Tests\Base;
class NoneTest extends Base
final class NoneTest extends Base
{
public static function setUpBeforeClass(): void
{
@@ -14,7 +18,7 @@ class NoneTest extends Base
public function testGetSize(): void
{
$this->assertEquals(0, self::$cache->getSize());
$this->assertSame(0, self::$cache->getSize());
}
public function testEmptyCacheKey(): void
@@ -26,6 +30,7 @@ class NoneTest extends Base
$this->assertEquals(false, $data);
}
#[\Override]
public function testCacheSave(): void
{
$result = self::$cache->save($this->key, $this->data);
@@ -33,9 +38,7 @@ class NoneTest extends Base
$this->assertEquals(false, $result);
}
/**
* @depends testCacheSave
*/
#[Depends('testCacheSave')]
public function testCacheLoad(): void
{
$data = self::$cache->load($this->key, 60 * 60 * 24 * 30 * 3 /* 3 months */);
@@ -43,9 +46,8 @@ class NoneTest extends Base
$this->assertEquals(false, $data);
}
/**
* @depends testCacheLoad
*/
#[Depends('testCacheLoad')]
#[\Override]
public function testNotEmptyCacheKey(): void
{
$data = self::$cache->load($this->key, 60 * 60 * 24 * 30 * 3 /* 3 months */);
@@ -53,6 +55,7 @@ class NoneTest extends Base
$this->assertEquals(false, $data);
}
#[\Override]
public function testCachePurge(): void
{
$result = self::$cache->purge($this->key);
@@ -60,14 +63,21 @@ class NoneTest extends Base
$this->assertEquals(true, $result);
}
#[\Override]
public function testCacheTouch(): void
{
$this->assertEquals(false, self::$cache->touch($this->key));
}
#[\Override]
public function testCaseInsensitivity(): void
{
// None adapter does not expect case sensitivity/insensitivy
$this->assertEquals(true, true);
$this->markTestSkipped('The None adapter stores nothing, so key casing cannot matter.');
}
#[\Override]
public function testCaseSensitivity(): void
{
$this->markTestSkipped('The None adapter stores nothing, so key casing cannot matter.');
}
}
@@ -1,25 +1,26 @@
<?php
namespace Utopia\Tests\E2E;
declare(strict_types=1);
namespace Utopia\Tests\Unit;
use Override;
use Utopia\Cache\Adapter\Filesystem;
use Utopia\Cache\Adapter\Pool;
use Utopia\Cache\Cache;
use Utopia\Pools\Adapter\Stack;
use Utopia\Pools\Pool as UtopiaPool;
use Utopia\Tests\Base;
class PoolTest extends Base
final class PoolTest extends Base
{
private static string $path;
public static function setUpBeforeClass(): void
{
$path = __DIR__.'/tests/pool';
if (! file_exists($path)) {
mkdir($path, 0777, true);
}
self::$path = self::scratch('pool');
$pool = new UtopiaPool(new Stack(), 'test', 10, function () use ($path) {
return new Filesystem($path);
}, timeout: 0.0);
$pool = new UtopiaPool(new Stack(), 'test', 10, fn(): Filesystem => new Filesystem(self::$path), timeout: 0.0);
self::$cache = new Cache(new Pool($pool));
}
@@ -27,7 +28,17 @@ class PoolTest extends Base
public function testGetSize(): void
{
self::$cache->save('test', 'test');
$this->assertEquals(4, self::$cache->getSize());
$this->assertSame(4, self::$cache->getSize());
}
#[Override]
public function testCaseSensitivity(): void
{
if (self::foldsFilenameCase(self::$path)) {
$this->markTestSkipped('The host filesystem folds filename case.');
}
parent::testCaseSensitivity();
}
public function testLeaseFallbackForNonLeasableAdapter(): void
@@ -1,6 +1,8 @@
<?php
namespace Utopia\Tests\Unit\Redis;
declare(strict_types=1);
namespace Utopia\Tests\Unit;
use PHPUnit\Framework\TestCase;
use Utopia\Cache\Adapter\Redis\Client;
@@ -8,18 +10,18 @@ use Utopia\Cache\Adapter\Redis\ConnectionError;
use Utopia\Cache\Adapter\Redis\ConnectionException;
use Utopia\Cache\Adapter\Redis\RedisError;
class ClientTest extends TestCase
final class RedisClientTest extends TestCase
{
public function testEncodeBuildsRespArrayOfBulkStrings(): void
{
$this->assertSame(
"*1\r\n\$4\r\nPING\r\n",
Client::encode(['PING'])
Client::encode(['PING']),
);
$this->assertSame(
"*3\r\n\$3\r\nSET\r\n\$3\r\nfoo\r\n\$3\r\nbar\r\n",
Client::encode(['SET', 'foo', 'bar'])
Client::encode(['SET', 'foo', 'bar']),
);
}
@@ -27,7 +29,7 @@ class ClientTest extends TestCase
{
$this->assertSame(
"*2\r\n\$6\r\nSELECT\r\n\$1\r\n3\r\n",
Client::encode(['SELECT', 3])
Client::encode(['SELECT', 3]),
);
}
@@ -37,8 +39,8 @@ class ClientTest extends TestCase
$encoded = Client::encode(['SET', 'k', $payload]);
$this->assertSame(
"*3\r\n\$3\r\nSET\r\n\$1\r\nk\r\n\$".strlen($payload)."\r\n".$payload."\r\n",
$encoded
"*3\r\n\$3\r\nSET\r\n\$1\r\nk\r\n\$" . \strlen($payload) . "\r\n" . $payload . "\r\n",
$encoded,
);
}
@@ -75,7 +77,7 @@ class ClientTest extends TestCase
$payload = "line1\r\nline2";
$this->assertSame(
$payload,
Client::parse('$'.strlen($payload)."\r\n".$payload."\r\n", $offset)
Client::parse('$' . \strlen($payload) . "\r\n" . $payload . "\r\n", $offset),
);
}
@@ -98,7 +100,7 @@ class ClientTest extends TestCase
$offset = 0;
$buffer = "*3\r\n\$3\r\nfoo\r\n:42\r\n+OK\r\n";
$this->assertSame(['foo', 42, 'OK'], Client::parse($buffer, $offset));
$this->assertSame(strlen($buffer), $offset);
$this->assertSame(\strlen($buffer), $offset);
}
public function testParseEmptyArray(): void
@@ -118,7 +120,7 @@ class ClientTest extends TestCase
$offset = 0;
$buffer = "*2\r\n*2\r\n:1\r\n:2\r\n*1\r\n\$1\r\nx\r\n";
$this->assertSame([[1, 2], ['x']], Client::parse($buffer, $offset));
$this->assertSame(strlen($buffer), $offset);
$this->assertSame(\strlen($buffer), $offset);
}
public function testParseRedisErrorIsWrappedNotThrown(): void
@@ -168,7 +170,7 @@ class ClientTest extends TestCase
$this->assertSame('OK', Client::parse($buffer, $offset));
$this->assertSame(5, $offset);
$this->assertSame('SECOND', Client::parse($buffer, $offset));
$this->assertSame(strlen($buffer), $offset);
$this->assertSame(\strlen($buffer), $offset);
}
public function testParseUnknownTypeThrows(): void
@@ -188,7 +190,7 @@ class ClientTest extends TestCase
// And that encode produces what a real Redis would expect.
$this->assertSame(
"*4\r\n\$4\r\nHSET\r\n\$1\r\nk\r\n\$1\r\nf\r\n\$1\r\nv\r\n",
Client::encode(['HSET', 'k', 'f', 'v'])
Client::encode(['HSET', 'k', 'f', 'v']),
);
}
@@ -1,11 +1,13 @@
<?php
namespace Utopia\Tests\Unit\Redis;
declare(strict_types=1);
namespace Utopia\Tests\Unit;
use PHPUnit\Framework\TestCase;
use Utopia\Cache\Adapter\Redis\Envelope;
class EnvelopeTest extends TestCase
final class RedisEnvelopeTest extends TestCase
{
public function testEncodeWrapsDataAndTime(): void
{
@@ -1,11 +1,13 @@
<?php
namespace Utopia\Tests\Unit\Redis;
declare(strict_types=1);
namespace Utopia\Tests\Unit;
use PHPUnit\Framework\TestCase;
use Utopia\Cache\Adapter\Redis\NoScript;
class NoScriptTest extends TestCase
final class RedisNoScriptTest extends TestCase
{
public function testMatchesWhenNoScriptIsTheLeadingCodeToken(): void
{
@@ -36,6 +38,6 @@ class NoScriptTest extends TestCase
$signal = NoScript::from('NOSCRIPT No matching script. Please use EVAL.');
$this->assertSame('NOSCRIPT No matching script. Please use EVAL.', $signal->getMessage());
$this->assertNull($signal->getPrevious());
$this->assertNotInstanceOf(\Throwable::class, $signal->getPrevious());
}
}
@@ -1,6 +1,8 @@
<?php
namespace Utopia\Tests\E2E;
declare(strict_types=1);
namespace Utopia\Tests\Unit;
use PHPUnit\Framework\TestCase;
use Utopia\Cache\Adapter\Memory;
@@ -9,12 +11,11 @@ use Utopia\Cache\Feature;
use Utopia\Telemetry\Adapter as Telemetry;
use Utopia\Telemetry\Adapter\None;
class TelemetryTest extends TestCase
final class TelemetryTest extends TestCase
{
public function testCachePropagatesTelemetryToAdapter(): void
{
$adapter = new class extends Memory implements Feature\Telemetry
{
$adapter = new class extends Memory implements Feature\Telemetry {
public ?Telemetry $telemetry = null;
public int $calls = 0;
@@ -27,11 +28,11 @@ class TelemetryTest extends TestCase
};
$cache = new Cache($adapter);
$telemetry = new None;
$telemetry = new None();
$cache->setTelemetry($telemetry);
$this->assertSame($telemetry, $adapter->telemetry);
$this->assertEquals(1, $adapter->calls);
$this->assertSame(1, $adapter->calls);
}
}