Skip to content

Commit 931cad9

Browse files
ragboyjrNyholm
authored andcommitted
Added Cache Util Package (#206)
* Added Cache Util Package - Created new Util repo - Add `remember` function - Added src/inc.php for including static files Signed-off-by: RJ Garcia <[email protected]> * Removing snake case in variable parameter Signed-off-by: RJ Garcia <[email protected]>
0 parents  commit 931cad9

File tree

11 files changed

+253
-0
lines changed

11 files changed

+253
-0
lines changed

.gitattributes

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
.gitattributes export-ignore
2+
.github/ export-ignore
3+
.gitignore export-ignore
4+
.travis.yml export-ignore
5+
/phpunit.xml.dist export-ignore
6+
/Tests/ export-ignore

.github/PULL_REQUEST_TEMPLATE.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
This is a READ ONLY repository.
2+
3+
Please make your pull request to https://github.com/php-cache/cache
4+
5+
Thank you for contributing.

.gitignore

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

.travis.yml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
language: php
2+
sudo: false
3+
4+
matrix:
5+
include:
6+
- php: 7.0
7+
8+
cache:
9+
directories:
10+
- "$HOME/.composer/cache"
11+
12+
install:
13+
- composer update --prefer-dist --prefer-stable
14+
15+
script:
16+
- ./vendor/bin/phpunit --coverage-clover=coverage.xml
17+
18+
after_success:
19+
- pip install --user codecov && codecov
20+
21+
notifications:
22+
email: false

Changelog.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# Changelog
2+
3+
The change log describes what is "Added", "Removed", "Changed" or "Fixed" between each release.
4+
5+
## UNRELEASED
6+
7+
### Added
8+
9+
- New utility repo
10+
- `remember` function

LICENSE

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2015 Tobias Nyholm, RJ Garcia
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.
22+

README.md

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# Cache Utilities
2+
[![Gitter](https://badges.gitter.im/php-cache/cache.svg)](https://gitter.im/php-cache/cache?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge)
3+
[![Latest Stable Version](https://poser.pugx.org/cache/util/v/stable)](https://packagist.org/packages/cache/util)
4+
[![codecov.io](https://codecov.io/github/php-cache/util/coverage.svg?branch=master)](https://codecov.io/github/php-cache/util?branch=master)
5+
[![Total Downloads](https://poser.pugx.org/cache/util/downloads)](https://packagist.org/packages/cache/util)
6+
[![Monthly Downloads](https://poser.pugx.org/cache/util/d/monthly.png)](https://packagist.org/packages/cache/util)
7+
[![Software License](https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square)](LICENSE)
8+
9+
This is a collection of utilities for the PSR-16 and PSR-6 caching standards.
10+
11+
12+
### Install
13+
14+
```bash
15+
composer require cache/util
16+
```
17+
18+
### Use
19+
20+
```php
21+
use function Cache\Util\SimpleCache\remember;
22+
23+
$cache = new SimpleCache(); // some simple cache interface
24+
25+
// if the result exists at the key, it'll return from cache, else it'll execute the callback and store in cache and return.
26+
$res = remember($cache, 'key', 3600, function() {
27+
return someExpensiveOperation();
28+
});
29+
```
30+
31+
### Contribute
32+
33+
Contributions are very welcome! Send a pull request to the [main repository](https://github.com/php-cache/cache) or
34+
report any issues you find on the [issue tracker](http://issues.php-cache.com).
35+

SimpleCache/util.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
/*
4+
* This file is part of php-cache organization.
5+
*
6+
* (c) 2015 Aaron Scherer <[email protected]>, Tobias Nyholm <[email protected]>
7+
*
8+
* This source file is subject to the MIT license that is bundled
9+
* with this source code in the file LICENSE.
10+
*/
11+
12+
namespace Cache\Util\SimpleCache;
13+
14+
use Psr\SimpleCache\CacheInterface;
15+
16+
function remember(CacheInterface $cache, $key, $ttl, callable $createResult)
17+
{
18+
$res = $cache->get($key);
19+
if ($res) {
20+
return $res;
21+
}
22+
23+
$res = $createResult();
24+
$cache->set($key, $res, $ttl);
25+
26+
return $res;
27+
}

Tests/SimpleCacheUtilTest.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
/*
4+
* This file is part of php-cache organization.
5+
*
6+
* (c) 2015 Aaron Scherer <[email protected]>, Tobias Nyholm <[email protected]>
7+
*
8+
* This source file is subject to the MIT license that is bundled
9+
* with this source code in the file LICENSE.
10+
*/
11+
12+
namespace Cache\Util\Tests;
13+
14+
use Cache\Adapter\PHPArray\ArrayCachePool;
15+
use Cache\Util;
16+
17+
class SimpleCacheUtilTest extends \PHPUnit_Framework_TestCase
18+
{
19+
public function testRememberCacheHit()
20+
{
21+
$cache = new ArrayCachePool();
22+
$cache->set('foo', 'bar');
23+
$res = Util\SimpleCache\remember($cache, 'foo', null, function () {
24+
throw new \Exception('bad');
25+
});
26+
$this->assertEquals('bar', $res);
27+
}
28+
29+
public function testRememberCacheMiss()
30+
{
31+
$cache = new ArrayCachePool();
32+
$res = Util\SimpleCache\remember($cache, 'foo', null, function () {
33+
return 'bar';
34+
});
35+
$this->assertEquals('bar', $res);
36+
$this->assertEquals('bar', $cache->get('foo'));
37+
}
38+
}

composer.json

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
{
2+
"name": "cache/util",
3+
"type": "library",
4+
"description": "PSR-6 and PSR-16 Caching Utilities",
5+
"keywords": [
6+
"cache",
7+
"psr6",
8+
"psr16",
9+
"utilities",
10+
"util"
11+
],
12+
"homepage": "http://www.php-cache.com/en/latest/",
13+
"license": "MIT",
14+
"minimum-stability": "dev",
15+
"prefer-stable": true,
16+
"authors": [
17+
{
18+
"name": "Tobias Nyholm",
19+
"email": "[email protected]",
20+
"homepage": "https://github.com/nyholm"
21+
},
22+
{
23+
"name": "RJ Garcia",
24+
"email": "[email protected]",
25+
"homepage": "https://github.com/ragboyjr"
26+
}
27+
],
28+
"require": {
29+
"php": "^5.6 || ^7.0",
30+
"psr/cache": "^1.0"
31+
},
32+
"require-dev": {
33+
"phpunit/phpunit": "^5.7.21",
34+
"cache/array-adapter": "^1.0"
35+
},
36+
"autoload": {
37+
"psr-4": {
38+
"Cache\\Util\\": ""
39+
},
40+
"files": ["SimpleCache/util.php"],
41+
"exclude-from-classmap": [
42+
"/Tests/"
43+
]
44+
},
45+
"extra": {
46+
"branch-alias": {
47+
"dev-master": "1.0-dev"
48+
}
49+
}
50+
}

phpunit.xml.dist

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
3+
<phpunit backupGlobals="false"
4+
backupStaticAttributes="false"
5+
colors="true"
6+
convertErrorsToExceptions="true"
7+
convertNoticesToExceptions="true"
8+
convertWarningsToExceptions="true"
9+
processIsolation="false"
10+
stopOnFailure="false"
11+
syntaxCheck="false"
12+
bootstrap="vendor/autoload.php"
13+
>
14+
<testsuites>
15+
<testsuite name="Main Test Suite">
16+
<directory>./Tests/</directory>
17+
</testsuite>
18+
</testsuites>
19+
20+
<groups>
21+
<exclude>
22+
<group>benchmark</group>
23+
</exclude>
24+
</groups>
25+
26+
<filter>
27+
<whitelist>
28+
<directory>./</directory>
29+
<exclude>
30+
<directory>./Resources</directory>
31+
<directory>./Tests</directory>
32+
<directory>./vendor</directory>
33+
</exclude>
34+
</whitelist>
35+
</filter>
36+
</phpunit>

0 commit comments

Comments
 (0)