Skip to content

Commit

Permalink
Cleanup (#30)
Browse files Browse the repository at this point in the history
* Update workflow actions

* Update dependencies to composer.json

* Update psalm.xml

* Add memcached extension to mutation action

* Make exception classes final

* Add bootstrap to phpunit.xml.dist

* Add Memcached class suppression for Psalm

* Add configuration

* Cleanup Memcached class

* Cleanup tests

* Fix constructor docBlock to Memcached

* Add description to readme

* Fix code style for StyleCI

* Add memcached service in actions

* Remove windows from build action

* Fix memcached service

* Remove PHP 8.0 from build action

* Return PHP 8.0 to build action

* Fix tests for PHP 8.0

* Return the tests back

* Remove "--ignore-platform-reqs" flag for PHP 8.0

* Remove split versions composer update dependencies

* Add memcached service to .scrutinizer.yml

* Fix .scrutinizer.yml

* Replace memcached on service in actions

* Fix memcached service in build action

* Fix memcached service port

* Add self-hosted memcached service

* Return memcached on service in actions

* Change runner to memcached on service

* Return action- memcached

* Fix tests for PHP 8.0

* Replace `createMock` on `createPartialMock` for PHP 8.0

* Refactoring Memcached::deleteMultiple()

* Fix code for StyleCI

* Return Memcached::deleteMultiple() behavior

* Fix tests
  • Loading branch information
devanych committed Dec 29, 2020
1 parent 0fb03ca commit 0faa8fd
Show file tree
Hide file tree
Showing 18 changed files with 668 additions and 537 deletions.
10 changes: 4 additions & 6 deletions .github/workflows/build.yml
Expand Up @@ -24,6 +24,9 @@ jobs:
- "8.0"

steps:
- name: Install service memcached
uses: niden/actions-memcached@v7

- name: Checkout
uses: actions/checkout@v2.3.4

Expand Down Expand Up @@ -55,13 +58,8 @@ jobs:
- name: Update composer
run: composer self-update

- name: Install dependencies with composer php 7.4
if: matrix.php == '7.4'
- name: Install dependencies with composer
run: composer update --prefer-dist --no-interaction --no-progress --optimize-autoloader --ansi

- name: Install dependencies with composer php 8.0
if: matrix.php == '8.0'
run: composer update --ignore-platform-reqs --prefer-dist --no-interaction --no-progress --optimize-autoloader --ansi

- name: Run tests with phpunit
run: vendor/bin/phpunit --colors=always
63 changes: 63 additions & 0 deletions .github/workflows/mutation.yml
@@ -0,0 +1,63 @@
on:
pull_request:
push:
branches:
- "master"

name: mutation test

jobs:
mutation:
name: PHP ${{ matrix.php }}-${{ matrix.os }}

runs-on: ${{ matrix.os }}

env:
extensions: memcached

strategy:
matrix:
os:
- ubuntu-latest

php:
- "7.4"

steps:
- name: Install service memcached
uses: niden/actions-memcached@v7

- name: Checkout
uses: actions/checkout@v2.3.4

- name: Install PHP
uses: shivammathur/setup-php@v2
with:
php-version: "${{ matrix.php }}"
ini-values: memory_limit=-1
coverage: "pcov"
tools: composer:v2, pecl

- name: Determine composer cache directory
run: echo "COMPOSER_CACHE_DIR=$(composer config cache-dir)" >> $GITHUB_ENV

- name: Cache dependencies installed with composer
uses: actions/cache@v2
with:
path: ${{ env.COMPOSER_CACHE_DIR }}
key: php${{ matrix.php }}-composer-${{ hashFiles('**/composer.json') }}
restore-keys: |
php${{ matrix.php }}-composer-
- name: Update composer
run: composer self-update

- name: Install dependencies with composer
run: composer update --prefer-dist --no-interaction --no-progress --optimize-autoloader --ansi

- name: Run infection
run: |
git fetch --depth=1 origin $GITHUB_BASE_REF
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
env:
STRYKER_DASHBOARD_API_KEY: ${{ secrets.STRYKER_DASHBOARD_API_KEY }}
14 changes: 12 additions & 2 deletions .scrutinizer.yml
Expand Up @@ -8,6 +8,12 @@ filter:
build:
nodes:
analysis:
services:
memcached:
image: memcached:latest
ports:
- 11211

environment:
php: 7.4.12

Expand All @@ -20,6 +26,12 @@ build:
- php-scrutinizer-run

tests-and-coverage:
services:
memcached:
image: memcached:latest
ports:
- 11211

environment:
php: 7.4.12

Expand All @@ -37,5 +49,3 @@ build:
coverage:
file: coverage.xml
format: php-clover


2 changes: 1 addition & 1 deletion CHANGELOG.md
@@ -1,4 +1,4 @@
# Memcached Change Log
# Yii Memcached Change Log

## 1.0.0 under development

Expand Down
108 changes: 103 additions & 5 deletions README.md
@@ -1,26 +1,124 @@
<p align="center">
<a href="https://github.com/yiisoft" target="_blank">
<img src="https://avatars0.githubusercontent.com/u/993323" height="100px">
<img src="https://yiisoft.github.io/docs/images/yii_logo.svg" height="100px">
</a>
<h1 align="center">Yii Cache Library - Memcached Handler</h1>
<br>
</p>

This library provides the Memcached handler for the [yiisoft/cache] library.

[yiisoft/cache]: https://github.com/yiisoft/cache

[![Latest Stable Version](https://poser.pugx.org/yiisoft/cache-memcached/v/stable.png)](https://packagist.org/packages/yiisoft/cache-memcached)
[![Total Downloads](https://poser.pugx.org/yiisoft/cache-memcached/downloads.png)](https://packagist.org/packages/yiisoft/cache-memcached)
[![Build status](https://github.com/yiisoft/cache-memcached/workflows/build/badge.svg)](https://github.com/yiisoft/cache-memcached/actions?query=workflow%3Abuild)
[![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)
[![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)
[![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)
[![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)
[![type-coverage](https://shepherd.dev/github/yiisoft/cache-memcached/coverage.svg)](https://shepherd.dev/github/yiisoft/cache-memcached)

This package provides the [Memcached](https://www.php.net/manual/book.memcached.php)
handler and implements [PSR-16](https://www.php-fig.org/psr/psr-16/) cache.

This option can be considered as the fastest one when dealing with a cache in
a distributed applications (e.g. with several servers, load balancers, etc.).

## Installation

The package could be installed with composer:

```
composer install yiisoft/cache-memcached
```

## Configuration

Creating an instance:

```php
$cache = new \Yiisoft\Cache\Memcached\Memcached($persistentId, $servers);
```

`$persistentId (string)` - The ID that identifies the Memcached instance is an empty string by default.
By default, the Memcached instances are destroyed at the end of the request.
To create an instance that persists between requests, use persistent_id to specify a unique ID for the instance.
All instances created with the same `$persistentId` will share the same connection.

For more information, see the description of the
[`\Memcached::__construct()`](https://www.php.net/manual/memcached.construct.php).

`$servers (array)` - List of memcached servers that will be added to the server pool.

List has the following structure:

```php
$servers => [
[
'host' => 'server-1',
'port' => 11211,
'weight' => 100,
],
[
'host' => 'server-2',
'port' => 11211,
'weight' => 50,
],
];
```

The default value:

```php
$servers => [
[
'host' => Memcached::DEFAULT_SERVER_HOST, // '127.0.0.1'
'port' => Memcached::DEFAULT_SERVER_PORT, // 11211
'weight' => Memcached::DEFAULT_SERVER_WEIGHT, // 1
],
];
```

For more information, see the description of the
[`\Memcached::addServers()`](https://www.php.net/manual/memcached.addservers.php).

## General usage

The package does not contain any additional functionality for interacting with the cache,
except those defined in the [PSR-16](https://www.php-fig.org/psr/psr-16/) interface.

```php
$cache = new \Yiisoft\Cache\Memcached\Memcached();
$parameters = ['user_id' => 42];
$key = 'demo';

// try retrieving $data from cache
$data = $cache->get($key);

if ($data === null) {
// $data is not found in cache, calculate it from scratch
$data = calculateData($parameters);

// store $data in cache for an hour so that it can be retrieved next time
$cache->set($key, $data, 3600);
}

// $data is available here
```

In order to delete value you can use:

```php
$cache->delete($key);
// Or all cache
$cache->clear();
```

To work with values in a more efficient manner, batch operations should be used:

- `getMultiple()`
- `setMultiple()`
- `deleteMultiple()`

This package can be used as a cache handler for the [Yii Caching Library](https://github.com/yiisoft/cache).

### Unit testing

The package is tested with [PHPUnit](https://phpunit.de/). To run tests:
Expand Down
4 changes: 4 additions & 0 deletions composer.json
Expand Up @@ -42,6 +42,10 @@
"extra": {
"branch-alias": {
"dev-master": "3.0.x-dev"
},
"config-plugin": {
"common": "config/common.php",
"params": "config/params.php"
}
},
"config": {
Expand Down
17 changes: 17 additions & 0 deletions config/common.php
@@ -0,0 +1,17 @@
<?php

declare(strict_types=1);

use Yiisoft\Cache\Memcached\Memcached;

/* @var $params array */

return [
Memcached::class => [
'__class' => Memcached::class,
'__construct()' => [
$params['yiisoft/cache-memcached']['memcached']['persistentId'],
$params['yiisoft/cache-memcached']['memcached']['servers'],
],
],
];
20 changes: 20 additions & 0 deletions config/params.php
@@ -0,0 +1,20 @@
<?php

declare(strict_types=1);

use Yiisoft\Cache\Memcached\Memcached;

return [
'yiisoft/cache-memcached' => [
'memcached' => [
'persistentId' => '',
'servers' => [
[
'host' => Memcached::DEFAULT_SERVER_HOST,
'port' => Memcached::DEFAULT_SERVER_PORT,
'weight' => Memcached::DEFAULT_SERVER_WEIGHT,
],
],
],
],
];
7 changes: 4 additions & 3 deletions phpunit.xml.dist
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>

<phpunit bootstrap="vendor/autoload.php"
<phpunit bootstrap="tests/bootstrap.php"
colors="true"
verbose="true"
failOnRisky="true"
Expand All @@ -10,13 +10,14 @@
convertWarningsToExceptions="true"
stopOnFailure="false"
executionOrder="default"
resolveDependencies="true">
resolveDependencies="true"
>
<php>
<ini name="error_reporting" value="-1"/>
</php>

<testsuites>
<testsuite name="Cache Memcached Tests">
<testsuite name="Yii Memcached Tests">
<directory>./tests</directory>
</testsuite>
</testsuites>
Expand Down
10 changes: 9 additions & 1 deletion psalm.xml
@@ -1,6 +1,6 @@
<?xml version="1.0"?>
<psalm
errorLevel="3"
errorLevel="2"
resolveFromConfigFile="true"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="https://getpsalm.org/schema/config"
Expand All @@ -12,4 +12,12 @@
<directory name="vendor" />
</ignoreFiles>
</projectFiles>

<issueHandlers>
<UndefinedClass>
<errorLevel type="suppress">
<referencedClass name="Memcached" />
</errorLevel>
</UndefinedClass>
</issueHandlers>
</psalm>
2 changes: 1 addition & 1 deletion src/CacheException.php
Expand Up @@ -4,6 +4,6 @@

namespace Yiisoft\Cache\Memcached;

class CacheException extends \RuntimeException implements \Psr\SimpleCache\CacheException
final class CacheException extends \RuntimeException implements \Psr\SimpleCache\CacheException
{
}
2 changes: 1 addition & 1 deletion src/InvalidArgumentException.php
Expand Up @@ -4,6 +4,6 @@

namespace Yiisoft\Cache\Memcached;

class InvalidArgumentException extends \RuntimeException implements \Psr\SimpleCache\InvalidArgumentException
final class InvalidArgumentException extends \RuntimeException implements \Psr\SimpleCache\InvalidArgumentException
{
}

0 comments on commit 0faa8fd

Please sign in to comment.