Skip to content
This repository was archived by the owner on Jan 24, 2024. It is now read-only.

Commit e239ff9

Browse files
committed
v0.1.0
0 parents  commit e239ff9

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+1886
-0
lines changed

.coveralls.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
coverage_clover: clover.xml
2+
json_path: upload.json
3+
service_name: travis-ci

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.idea
2+
composer.phar
3+
vendor/

.scrutinizer.yml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
build:
2+
environment:
3+
php: "7.0.8"
4+
5+
tests:
6+
before:
7+
- "phpunit"
8+
9+
before_commands:
10+
- "composer install --no-interaction --prefer-source"

.travis.yml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
language: php
2+
3+
php:
4+
- '7.0'
5+
- '7.1'
6+
7+
before_install:
8+
- composer require satooshi/php-coveralls:dev-master
9+
- composer install --dev
10+
11+
install:
12+
- composer install --no-interaction --prefer-source
13+
14+
after_script:
15+
- php vendor/bin/coveralls -v
16+
17+
script: phpunit --coverage-clover clover.xml

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2017 Spiral Framework Modules
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# phpfastcache
2+
Spiral cache bridges module.
3+
4+
Spiral utilizes PSR-16 (ORM) and PSR-6 (psr7-middlewares) protocols to allow your application to communicate with cache engines.
5+
6+
You are able to choose any caching library which support this mechanisms. To enable cache support in application create PSR interface binding to your implementation or factory:
7+
8+
```php
9+
use Psr\SimpleCache\CacheInterface;
10+
11+
class CacheBootloader extends Bootloader {
12+
const SINGLETONS = [
13+
CacheInterface::class => [self::class, 'makeCache']
14+
];
15+
16+
protected function makeCache(): CacheItemPoolInterface
17+
{
18+
return new MyCache();
19+
}
20+
}
21+
```
22+
23+
## Installation
24+
```
25+
composer require spiral/phpfastcache
26+
spiral register spiral/phpfastcache
27+
```
28+
29+
## Existed Bridges
30+
Take a look at existed module `spiral/phpfastcache` which creates bridge to [https://github.com/PHPSocialNetwork/phpfastcache](https://github.com/PHPSocialNetwork/phpfastcache) library and adds cache configuration into your application.
31+
32+
`composer require spiral/phpfastcache`
33+
`spiral register spiral/phpfastcache`
34+
35+
Add bootloader `Spiral/PhpFastCache/CacheBootloader` to enable caching.

composer.json

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
{
2+
"name": "spiral/fastcache",
3+
"description": "Spiral simple cache adapter and phpFastCache bootloaders.",
4+
"authors": [
5+
{
6+
"name": "Valentin V / vvval",
7+
"email": "vintsukevich@gmail.com"
8+
}
9+
],
10+
"require": {
11+
"php": ">=7.0",
12+
"spiral/framework": "^1.0",
13+
"phpFastCache/phpFastCache": "^5.0"
14+
},
15+
"require-dev": {
16+
"phpunit/phpunit": "~6.0",
17+
"mockery/mockery": "dev-master"
18+
},
19+
"autoload": {
20+
"psr-4": {
21+
"Spiral\\": "source/"
22+
}
23+
},
24+
"autoload-dev": {
25+
"psr-4": {
26+
"Spiral\\Tests\\": "tests/",
27+
"TestApplication\\": "tests/-app-/classes/"
28+
}
29+
}
30+
}

phpunit.xml

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
3+
<phpunit bootstrap="tests/bootstrap.php"
4+
backupGlobals="false"
5+
backupStaticAttributes="false"
6+
colors="true"
7+
verbose="true"
8+
convertErrorsToExceptions="true"
9+
convertNoticesToExceptions="true"
10+
convertWarningsToExceptions="true"
11+
processIsolation="false"
12+
stopOnFailure="false"
13+
stopOnError="false"
14+
syntaxCheck="true">
15+
16+
<testsuites>
17+
<testsuite name="Spiral Statistics module">
18+
<directory>./tests/</directory>
19+
</testsuite>
20+
</testsuites>
21+
22+
<filter>
23+
<whitelist>
24+
<directory>source/Statistics/</directory>
25+
</whitelist>
26+
</filter>
27+
</phpunit>
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
namespace Spiral\PhpFastCache;
4+
5+
use phpFastCache\CacheManager;
6+
use Psr\Cache\CacheItemPoolInterface;
7+
use Psr\SimpleCache\CacheInterface;
8+
use Spiral\Core\Bootloaders\Bootloader;
9+
10+
class CacheBootloader extends Bootloader
11+
{
12+
const BINDINGS = [
13+
'cache' => CacheInterface::class,
14+
CacheInterface::class => SimpleCacheAdapter::class
15+
];
16+
17+
const SINGLETONS = [
18+
CacheItemPoolInterface::class => [self::class, 'makeCache']
19+
];
20+
21+
protected function makeCache(Config $config): CacheItemPoolInterface
22+
{
23+
return CacheManager::getInstance($config->getAdapter(), $config->getAdapterOptions());
24+
}
25+
}

source/PhpFastCache/Config.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
namespace Spiral\PhpFastCache;
4+
5+
use Spiral\Core\InjectableConfig;
6+
7+
class Config extends InjectableConfig
8+
{
9+
const CONFIG = 'modules/spiralPhpFastCache';
10+
11+
/** @var array */
12+
protected $config = [
13+
'adapter' => '',
14+
'adapters' => []
15+
];
16+
17+
/**
18+
* @return string
19+
*/
20+
public function getAdapter(): string
21+
{
22+
return $this->config['adapter'];
23+
}
24+
25+
/**
26+
* @return array
27+
*/
28+
public function getAdapterOptions(): array
29+
{
30+
return $this->config['adapters'][$this->getAdapter()];
31+
}
32+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
namespace Spiral\PhpFastCache\Exceptions;
4+
5+
class SpiralFastCacheException extends \Exception
6+
{
7+
8+
}
Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
<?php
2+
3+
namespace Spiral\PhpFastCache;
4+
5+
use phpFastCache\Cache\ExtendedCacheItemInterface;
6+
use Psr\Cache\CacheItemPoolInterface;
7+
use Psr\SimpleCache\CacheInterface;
8+
use Spiral\PhpFastCache\Exceptions\SpiralFastCacheException;
9+
10+
class SimpleCacheAdapter implements CacheInterface
11+
{
12+
/**
13+
* @var CacheItemPoolInterface
14+
*/
15+
protected $pool;
16+
17+
/**
18+
* PSR16Adapter constructor.
19+
*
20+
* @param CacheItemPoolInterface $pool
21+
*/
22+
public function __construct(CacheItemPoolInterface $pool)
23+
{
24+
$this->pool = $pool;
25+
}
26+
27+
/**
28+
* {@inheritdoc}
29+
*/
30+
public function get($key, $default = null)
31+
{
32+
try {
33+
$value = $this->pool->getItem($key)->get();
34+
if ($value !== null) {
35+
return $value;
36+
} else {
37+
return $default;
38+
}
39+
} catch (\Throwable $e) {
40+
throw new SpiralFastCacheException($e->getMessage(), $e->getMessage(), $e);
41+
}
42+
}
43+
44+
/**
45+
* {@inheritdoc}
46+
*/
47+
public function set($key, $value, $ttl = null)
48+
{
49+
try {
50+
$item = $this->pool
51+
->getItem($key)
52+
->set($value);
53+
if (is_int($ttl) || $ttl instanceof \DateInterval) {
54+
$item->expiresAfter($ttl);
55+
}
56+
57+
return $this->pool->save($item);
58+
} catch (\Throwable $e) {
59+
throw new SpiralFastCacheException($e->getMessage(), $e->getMessage(), $e);
60+
}
61+
}
62+
63+
/**
64+
* {@inheritdoc}
65+
*/
66+
public function delete($key)
67+
{
68+
try {
69+
return $this->pool->deleteItem($key);
70+
} catch (\Throwable $e) {
71+
throw new SpiralFastCacheException($e->getMessage(), $e->getMessage(), $e);
72+
}
73+
}
74+
75+
/**
76+
* {@inheritdoc}
77+
*/
78+
public function clear()
79+
{
80+
try {
81+
return $this->pool->clear();
82+
} catch (\Throwable $e) {
83+
throw new SpiralFastCacheException($e->getMessage(), $e->getMessage(), $e);
84+
}
85+
}
86+
87+
/**
88+
* {@inheritdoc}
89+
*/
90+
public function getMultiple($keys, $default = null)
91+
{
92+
try {
93+
return array_map(function (ExtendedCacheItemInterface $item) {
94+
return $item->get();
95+
}, $this->pool->getItems($keys));
96+
} catch (\Throwable $e) {
97+
throw new SpiralFastCacheException($e->getMessage(), $e->getMessage(), $e);
98+
}
99+
}
100+
101+
/**
102+
* {@inheritdoc}
103+
*/
104+
public function setMultiple($values, $ttl = null)
105+
{
106+
try {
107+
foreach ($values as $key => $value) {
108+
$item = $this->pool->getItem($key)->set($value);
109+
$this->pool->saveDeferred($item);
110+
unset($item);
111+
}
112+
113+
return $this->pool->commit();
114+
} catch (\Throwable $e) {
115+
throw new SpiralFastCacheException($e->getMessage(), $e->getMessage(), $e);
116+
}
117+
}
118+
119+
/**
120+
* {@inheritdoc}
121+
*/
122+
public function deleteMultiple($keys)
123+
{
124+
try {
125+
return $this->pool->deleteItems($keys);
126+
} catch (\Throwable $e) {
127+
throw new SpiralFastCacheException($e->getMessage(), $e->getMessage(), $e);
128+
}
129+
}
130+
131+
/**
132+
* {@inheritdoc}
133+
*/
134+
public function has($key)
135+
{
136+
try {
137+
return $this->pool->getItem($key)->isHit();
138+
} catch (\Throwable $e) {
139+
throw new SpiralFastCacheException($e->getMessage(), $e->getMessage(), $e);
140+
}
141+
}
142+
}

0 commit comments

Comments
 (0)