Skip to content

Commit e1797f0

Browse files
committed
cache interface
1 parent 0efe77e commit e1797f0

14 files changed

+285
-0
lines changed

.github/CODE_OF_CONDUCT.md

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# Contributor Covenant Code of Conduct
2+
3+
## Our Pledge
4+
5+
In the interest of fostering an open and welcoming environment, we as contributors and maintainers pledge to making participation in our project and our community a harassment-free experience for everyone, regardless of age, body size, disability, ethnicity, gender identity and expression, level of experience, nationality, personal appearance, race, religion, or sexual identity and orientation.
6+
7+
## Our Standards
8+
9+
Examples of behavior that contributes to creating a positive environment include:
10+
11+
* Using welcoming and inclusive language
12+
* Being respectful of differing viewpoints and experiences
13+
* Gracefully accepting constructive criticism
14+
* Focusing on what is best for the community
15+
* Showing empathy towards other community members
16+
17+
Examples of unacceptable behavior by participants include:
18+
19+
* The use of sexualized language or imagery and unwelcome sexual attention or advances
20+
* Trolling, insulting/derogatory comments, and personal or political attacks
21+
* Public or private harassment
22+
* Publishing others' private information, such as a physical or electronic address, without explicit permission
23+
* Other conduct which could reasonably be considered inappropriate in a professional setting
24+
25+
## Our Responsibilities
26+
27+
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.
28+
29+
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.
30+
31+
## Scope
32+
33+
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.
34+
35+
## Enforcement
36+
37+
Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by contacting the project team at ivan@ddrv.ru. The project team will review and investigate all complaints, and will respond in a way that it deems 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.
38+
39+
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.
40+
41+
## Attribution
42+
43+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4, available at [http://contributor-covenant.org/version/1/4][version]
44+
45+
[homepage]: http://contributor-covenant.org
46+
[version]: http://contributor-covenant.org/version/1/4/

.github/CONTRIBUTING.md

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# Contributing
2+
3+
## Report an issue
4+
5+
Please follow the guidelines below when creating an issue so that your issue can be more promptly resolved:
6+
* Provide information including: the version of PHP and This package, the type of operating system and Web server;
7+
* Provide the complete error call stack if available;
8+
* Describe the steps for reproducing the issue. It would be even better if you could provide code to reproduce the issue;
9+
10+
Do not report an issue if you are asking how to use some feature. You should use the `README.md` file.
11+
12+
Before you report an issue, please search through existing issues to see if your issue is already reported or fixed to make sure you are not reporting a duplicated issue. Also make sure you have the latest version of package and see if the issue still exists.

.github/FUNDING.yml

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
custom: "https://www.paypal.me/ddrv"
2+
patreon: ddrv

.github/ISSUE_TEMPLATE/bug_report.md

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
---
2+
name: Bug report
3+
about: Create a report to help us improve
4+
title: 'Bug'
5+
labels: bug
6+
assignees: ddrv
7+
8+
---
9+
10+
### What steps will reproduce the problem?
11+
12+
### What is the expected result?
13+
14+
### What do you get instead?
15+
16+
### Additional info
17+
18+
| Q | A
19+
| ---------------- | ---
20+
| Library version | 1.?
21+
| PHP version | ?
22+
| Operating system | ?

.github/ISSUE_TEMPLATE/custom.md

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
---
2+
name: Custom issue template
3+
about: Describe this issue template's purpose here.
4+
title: ''
5+
labels: ''
6+
assignees: ddrv
7+
8+
---
+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
---
2+
name: Feature request
3+
about: Suggest an idea for this project
4+
title: ''
5+
labels: feature
6+
assignees: ddrv
7+
8+
---
9+
10+
**Is your feature request related to a problem? Please describe.**
11+
A clear and concise description of what the problem is. Ex. I'm always frustrated when [...]
12+
13+
**Describe the solution you'd like**
14+
A clear and concise description of what you want to happen.
15+
16+
**Describe alternatives you've considered**
17+
A clear and concise description of any alternative solutions or features you've considered.
18+
19+
**Additional context**
20+
Add any other context or screenshots about the feature request here.

.github/PULL_REQUEST_TEMPLATE.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
| Q | A
2+
| ------------- | ---
3+
| Bug fix? | yes/no
4+
| New feature? | yes/no
5+
| Tickets | Fix #...

.github/workflows/ci.yml

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
name: CI
2+
3+
on: [push, pull_request]
4+
5+
jobs:
6+
tests:
7+
runs-on: ubuntu-latest
8+
9+
strategy:
10+
matrix:
11+
php: [7.4, 8.0, 8.1]
12+
13+
steps:
14+
- name: Checkout code
15+
uses: actions/checkout@v2
16+
17+
- name: Setup PHP
18+
uses: shivammathur/setup-php@v2
19+
with:
20+
php-version: ${{ matrix.php }}
21+
coverage: none
22+
23+
- name: Validate composer.json and composer.lock
24+
run: composer validate
25+
26+
- name: Install dependencies
27+
run: composer install --prefer-dist --no-progress --no-interaction --no-suggest
28+
29+
- name: Check code style
30+
run: vendor/bin/phpcs

.gitignore

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
vendor/
2+
composer.lock
3+
phpcs.xml
4+
phpunit.xml

README.md

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
[![Latest Stable Version](https://img.shields.io/packagist/v/webclient/cache-contract.svg?style=flat-square)](https://packagist.org/packages/webclient/cache-contract)
2+
[![Total Downloads](https://img.shields.io/packagist/dt/webclient/cache-contract.svg?style=flat-square)](https://packagist.org/packages/webclient/cache-contract/stats)
3+
[![License](https://img.shields.io/packagist/l/webclient/cache-contract.svg?style=flat-square)](https://github.com/phpwebclient/cache-contract/blob/master/LICENSE)
4+
[![PHP](https://img.shields.io/packagist/php-v/webclient/cache-contract.svg?style=flat-square)](https://php.net)
5+
6+
# webclient/cache-contract
7+
8+
Cache interface for [webclient/ext-cache](https://packagist.org/packages/webclient/ext-cache#v2.0.0)
9+
10+
# Install
11+
12+
```bash
13+
composer require webclient/cache-contract:^1.0
14+
```
15+
# Tips and tricks
16+
17+
## Split cache storage for settings and responses
18+
19+
You may split cache storage for settings and responses.
20+
21+
implements cache-contract, like it:
22+
23+
```php
24+
<?php
25+
26+
use Webclient\Cache\Contract\CacheInterface;
27+
28+
class SplitCache implements CacheInterface
29+
{
30+
private CacheInterface $settingsCache;
31+
private CacheInterface $responsesCache;
32+
public function __construct(CacheInterface $settingsCache, CacheInterface $responsesCache)
33+
{
34+
$this->settingsCache = $settingsCache;
35+
$this->responsesCache = $responsesCache;
36+
}
37+
38+
public function get(string $key) : ?string
39+
{
40+
$this->getStorage($key)->get($key);
41+
}
42+
43+
public function set(string $key,string $data,?int $ttl = null) : void
44+
{
45+
$this->getStorage($key)->get($key, $data, $ttl);
46+
}
47+
48+
private function getStorage(string $key): CacheInterface
49+
{
50+
if (strpos($key, 'http.settings.') === 0) {
51+
return $this->settingsCache;
52+
}
53+
if (strpos($key, 'http.response.') === 0) {
54+
return $this->responsesCache;
55+
}
56+
throw new InvalidArgumentException('can not define storage');
57+
}
58+
}
59+
60+
```

composer.json

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{
2+
"name": "webclient/cache-contract",
3+
"type": "library",
4+
"description": "Interface for caching",
5+
"keywords": ["http-client-extension", "cache"],
6+
"license": "MIT",
7+
"authors": [
8+
{
9+
"name": "Ivan Dudarev",
10+
"email": "ivan@ddrv.ru",
11+
"homepage": "https://ddrv.ru"
12+
}
13+
],
14+
"require": {
15+
"php": "^7.4 || ^8.0"
16+
},
17+
"require-dev": {
18+
"squizlabs/php_codesniffer": "^3.7"
19+
},
20+
"autoload": {
21+
"psr-4": {
22+
"Webclient\\Cache\\Contract\\": "src/"
23+
}
24+
}
25+
}

phpcs.xml.dist

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?xml version="1.0"?>
2+
<ruleset name="Coding standard">
3+
<description>Coding standard</description>
4+
5+
<!-- display progress -->
6+
<arg value="p"/>
7+
<!-- use colors in output -->
8+
<arg name="colors"/>
9+
10+
<!-- inherit rules from: -->
11+
<rule ref="PSR12"/>
12+
13+
<!-- Paths to check -->
14+
<file>src</file>
15+
</ruleset>

src/CacheInterface.php

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Webclient\Cache\Contract;
6+
7+
use Webclient\Cache\Contract\Exception\CacheError;
8+
9+
interface CacheInterface
10+
{
11+
/**
12+
* @param string $key
13+
* @return string|null
14+
*/
15+
public function get(string $key): ?string;
16+
17+
/**
18+
* @param string $key
19+
* @param string $data
20+
* @param int|null $ttl
21+
* @return void
22+
* @throws CacheError
23+
*/
24+
public function set(string $key, string $data, ?int $ttl = null): void;
25+
}

src/Exception/CacheError.php

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Webclient\Cache\Contract\Exception;
6+
7+
use RuntimeException;
8+
9+
final class CacheError extends RuntimeException
10+
{
11+
}

0 commit comments

Comments
 (0)