Skip to content

Commit aad3453

Browse files
authored
Merge dbdab7b into 0fb03ca
2 parents 0fb03ca + dbdab7b commit aad3453

17 files changed

+671
-523
lines changed

.github/workflows/build.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,14 @@ jobs:
2323
- "7.4"
2424
- "8.0"
2525

26+
services:
27+
memcached:
28+
image: memcached:latest
29+
ports:
30+
- 11211/tcp
31+
# needed because the memcached container does not provide a healthcheck
32+
options: --health-cmd "timeout 5 bash -c 'cat < /dev/null > /dev/tcp/127.0.0.1/11211'" --health-interval 10s --health-timeout 5s --health-retries 5
33+
2634
steps:
2735
- name: Checkout
2836
uses: actions/checkout@v2.3.4

.github/workflows/mutation.yml

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
on:
2+
pull_request:
3+
push:
4+
branches:
5+
- "master"
6+
7+
name: mutation test
8+
9+
jobs:
10+
mutation:
11+
name: PHP ${{ matrix.php }}-${{ matrix.os }}
12+
13+
runs-on: ${{ matrix.os }}
14+
15+
env:
16+
extensions: memcached
17+
18+
strategy:
19+
matrix:
20+
os:
21+
- ubuntu-latest
22+
23+
php:
24+
- "7.4"
25+
26+
services:
27+
memcached:
28+
image: memcached:latest
29+
ports:
30+
- 11211/tcp
31+
# needed because the memcached container does not provide a healthcheck
32+
options: --health-cmd "timeout 5 bash -c 'cat < /dev/null > /dev/tcp/127.0.0.1/11211'" --health-interval 10s --health-timeout 5s --health-retries 5
33+
34+
steps:
35+
- name: Checkout
36+
uses: actions/checkout@v2.3.4
37+
38+
- name: Install PHP
39+
uses: shivammathur/setup-php@v2
40+
with:
41+
php-version: "${{ matrix.php }}"
42+
ini-values: memory_limit=-1
43+
coverage: "pcov"
44+
tools: composer:v2, pecl
45+
46+
- name: Determine composer cache directory
47+
run: echo "COMPOSER_CACHE_DIR=$(composer config cache-dir)" >> $GITHUB_ENV
48+
49+
- name: Cache dependencies installed with composer
50+
uses: actions/cache@v2
51+
with:
52+
path: ${{ env.COMPOSER_CACHE_DIR }}
53+
key: php${{ matrix.php }}-composer-${{ hashFiles('**/composer.json') }}
54+
restore-keys: |
55+
php${{ matrix.php }}-composer-
56+
57+
- name: Update composer
58+
run: composer self-update
59+
60+
- name: Install dependencies with composer
61+
run: composer update --prefer-dist --no-interaction --no-progress --optimize-autoloader --ansi
62+
63+
- name: Run infection
64+
run: |
65+
git fetch --depth=1 origin $GITHUB_BASE_REF
66+
vendor/bin/roave-infection-static-analysis-plugin -j2 --git-diff-filter=A --git-diff-base=origin/$GITHUB_BASE_REF --logger-github --ignore-msi-with-no-mutations --only-covered
67+
env:
68+
STRYKER_DASHBOARD_API_KEY: ${{ secrets.STRYKER_DASHBOARD_API_KEY }}

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Memcached Change Log
1+
# Yii Memcached Change Log
22

33
## 1.0.0 under development
44

README.md

Lines changed: 103 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,124 @@
11
<p align="center">
22
<a href="https://github.com/yiisoft" target="_blank">
3-
<img src="https://avatars0.githubusercontent.com/u/993323" height="100px">
3+
<img src="https://yiisoft.github.io/docs/images/yii_logo.svg" height="100px">
44
</a>
55
<h1 align="center">Yii Cache Library - Memcached Handler</h1>
66
<br>
77
</p>
88

9-
This library provides the Memcached handler for the [yiisoft/cache] library.
10-
11-
[yiisoft/cache]: https://github.com/yiisoft/cache
12-
139
[![Latest Stable Version](https://poser.pugx.org/yiisoft/cache-memcached/v/stable.png)](https://packagist.org/packages/yiisoft/cache-memcached)
1410
[![Total Downloads](https://poser.pugx.org/yiisoft/cache-memcached/downloads.png)](https://packagist.org/packages/yiisoft/cache-memcached)
1511
[![Build status](https://github.com/yiisoft/cache-memcached/workflows/build/badge.svg)](https://github.com/yiisoft/cache-memcached/actions?query=workflow%3Abuild)
1612
[![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/yiisoft/cache-memcached/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/yiisoft/cache-memcached/?branch=master)
1713
[![Code Coverage](https://scrutinizer-ci.com/g/yiisoft/cache-memcached/badges/coverage.png?b=master)](https://scrutinizer-ci.com/g/yiisoft/cache-memcached/?branch=master)
14+
[![Mutation testing badge](https://img.shields.io/endpoint?style=flat&url=https%3A%2F%2Fbadge-api.stryker-mutator.io%2Fgithub.com%2Fyiisoft%2Fcache-memcached%2Fmaster)](https://dashboard.stryker-mutator.io/reports/github.com/yiisoft/cache-memcached/master)
1815
[![static analysis](https://github.com/yiisoft/cache-memcached/workflows/static%20analysis/badge.svg)](https://github.com/yiisoft/cache-memcached/actions?query=workflow%3A%22static+analysis%22)
1916
[![type-coverage](https://shepherd.dev/github/yiisoft/cache-memcached/coverage.svg)](https://shepherd.dev/github/yiisoft/cache-memcached)
2017

18+
This package provides the [Memcached](https://www.php.net/manual/book.memcached.php)
19+
handler and implements [PSR-16](https://www.php-fig.org/psr/psr-16/) cache.
20+
21+
This option can be considered as the fastest one when dealing with a cache in
22+
a distributed applications (e.g. with several servers, load balancers, etc.).
23+
24+
## Installation
25+
26+
The package could be installed with composer:
27+
28+
```
29+
composer install yiisoft/cache-memcached
30+
```
31+
32+
## Configuration
33+
34+
Creating an instance:
35+
36+
```php
37+
$cache = new \Yiisoft\Cache\Memcached\Memcached($persistentId, $servers);
38+
```
39+
40+
`$persistentId (string)` - The ID that identifies the Memcached instance is an empty string by default.
41+
By default, the Memcached instances are destroyed at the end of the request.
42+
To create an instance that persists between requests, use persistent_id to specify a unique ID for the instance.
43+
All instances created with the same `$persistentId` will share the same connection.
44+
45+
For more information, see the description of the
46+
[`\Memcached::__construct()`](https://www.php.net/manual/memcached.construct.php).
47+
48+
`$servers (array)` - List of memcached servers that will be added to the server pool.
49+
50+
List has the following structure:
51+
52+
```php
53+
$servers => [
54+
[
55+
'host' => 'server-1',
56+
'port' => 11211,
57+
'weight' => 100,
58+
],
59+
[
60+
'host' => 'server-2',
61+
'port' => 11211,
62+
'weight' => 50,
63+
],
64+
];
65+
```
66+
67+
The default value:
68+
69+
```php
70+
$servers => [
71+
[
72+
'host' => Memcached::DEFAULT_SERVER_HOST, // '127.0.0.1'
73+
'port' => Memcached::DEFAULT_SERVER_PORT, // 11211
74+
'weight' => Memcached::DEFAULT_SERVER_WEIGHT, // 1
75+
],
76+
];
77+
```
78+
79+
For more information, see the description of the
80+
[`\Memcached::addServers()`](https://www.php.net/manual/memcached.addservers.php).
2181

2282
## General usage
2383

84+
The package does not contain any additional functionality for interacting with the cache,
85+
except those defined in the [PSR-16](https://www.php-fig.org/psr/psr-16/) interface.
86+
87+
```php
88+
$cache = new \Yiisoft\Cache\Memcached\Memcached();
89+
$parameters = ['user_id' => 42];
90+
$key = 'demo';
91+
92+
// try retrieving $data from cache
93+
$data = $cache->get($key);
94+
95+
if ($data === null) {
96+
// $data is not found in cache, calculate it from scratch
97+
$data = calculateData($parameters);
98+
99+
// store $data in cache for an hour so that it can be retrieved next time
100+
$cache->set($key, $data, 3600);
101+
}
102+
103+
// $data is available here
104+
```
105+
106+
In order to delete value you can use:
107+
108+
```php
109+
$cache->delete($key);
110+
// Or all cache
111+
$cache->clear();
112+
```
113+
114+
To work with values in a more efficient manner, batch operations should be used:
115+
116+
- `getMultiple()`
117+
- `setMultiple()`
118+
- `deleteMultiple()`
119+
120+
This package can be used as a cache handler for the [Yii Caching Library](https://github.com/yiisoft/cache).
121+
24122
### Unit testing
25123

26124
The package is tested with [PHPUnit](https://phpunit.de/). To run tests:

composer.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,10 @@
4242
"extra": {
4343
"branch-alias": {
4444
"dev-master": "3.0.x-dev"
45+
},
46+
"config-plugin": {
47+
"common": "config/common.php",
48+
"params": "config/params.php"
4549
}
4650
},
4751
"config": {

config/common.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use Yiisoft\Cache\Memcached\Memcached;
6+
7+
/* @var $params array */
8+
9+
return [
10+
Memcached::class => [
11+
'__class' => Memcached::class,
12+
'__construct()' => [
13+
$params['yiisoft/cache-memcached']['memcached']['persistentId'],
14+
$params['yiisoft/cache-memcached']['memcached']['servers'],
15+
],
16+
],
17+
];

config/params.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use Yiisoft\Cache\Memcached\Memcached;
6+
7+
return [
8+
'yiisoft/cache-memcached' => [
9+
'memcached' => [
10+
'persistentId' => '',
11+
'servers' => [
12+
[
13+
'host' => Memcached::DEFAULT_SERVER_HOST,
14+
'port' => Memcached::DEFAULT_SERVER_PORT,
15+
'weight' => Memcached::DEFAULT_SERVER_WEIGHT,
16+
],
17+
],
18+
],
19+
],
20+
];

phpunit.xml.dist

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22

3-
<phpunit bootstrap="vendor/autoload.php"
3+
<phpunit bootstrap="tests/bootstrap.php"
44
colors="true"
55
verbose="true"
66
failOnRisky="true"
@@ -10,13 +10,14 @@
1010
convertWarningsToExceptions="true"
1111
stopOnFailure="false"
1212
executionOrder="default"
13-
resolveDependencies="true">
13+
resolveDependencies="true"
14+
>
1415
<php>
1516
<ini name="error_reporting" value="-1"/>
1617
</php>
1718

1819
<testsuites>
19-
<testsuite name="Cache Memcached Tests">
20+
<testsuite name="Yii Memcached Tests">
2021
<directory>./tests</directory>
2122
</testsuite>
2223
</testsuites>

psalm.xml

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?xml version="1.0"?>
22
<psalm
3-
errorLevel="3"
3+
errorLevel="2"
44
resolveFromConfigFile="true"
55
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
66
xmlns="https://getpsalm.org/schema/config"
@@ -12,4 +12,12 @@
1212
<directory name="vendor" />
1313
</ignoreFiles>
1414
</projectFiles>
15+
16+
<issueHandlers>
17+
<UndefinedClass>
18+
<errorLevel type="suppress">
19+
<referencedClass name="Memcached" />
20+
</errorLevel>
21+
</UndefinedClass>
22+
</issueHandlers>
1523
</psalm>

src/CacheException.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@
44

55
namespace Yiisoft\Cache\Memcached;
66

7-
class CacheException extends \RuntimeException implements \Psr\SimpleCache\CacheException
7+
final class CacheException extends \RuntimeException implements \Psr\SimpleCache\CacheException
88
{
99
}

src/InvalidArgumentException.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@
44

55
namespace Yiisoft\Cache\Memcached;
66

7-
class InvalidArgumentException extends \RuntimeException implements \Psr\SimpleCache\InvalidArgumentException
7+
final class InvalidArgumentException extends \RuntimeException implements \Psr\SimpleCache\InvalidArgumentException
88
{
99
}

0 commit comments

Comments
 (0)