Skip to content
Permalink
Browse files

Add voku/simplecache as alternative to Cache/Lite and use it cache

Cache/Lite is abandoned
  • Loading branch information...
onli committed Feb 12, 2019
1 parent fc02b03 commit d52f0004a47425644ff6cd4195798933a5f9bec7
Showing with 3,086 additions and 30 deletions.
  1. +48 −0 bundled-libs/voku/simple-cache/CHANGELOG.md
  2. +19 −0 bundled-libs/voku/simple-cache/LICENSE
  3. +105 −0 bundled-libs/voku/simple-cache/README.md
  4. +35 −0 bundled-libs/voku/simple-cache/composer.json
  5. +158 −0 bundled-libs/voku/simple-cache/src/voku/cache/AdapterApc.php
  6. +153 −0 bundled-libs/voku/simple-cache/src/voku/cache/AdapterApcu.php
  7. +128 −0 bundled-libs/voku/simple-cache/src/voku/cache/AdapterArray.php
  8. +93 −0 bundled-libs/voku/simple-cache/src/voku/cache/AdapterFile.php
  9. +242 −0 bundled-libs/voku/simple-cache/src/voku/cache/AdapterFileAbstract.php
  10. +89 −0 bundled-libs/voku/simple-cache/src/voku/cache/AdapterFileSimple.php
  11. +144 −0 bundled-libs/voku/simple-cache/src/voku/cache/AdapterMemcache.php
  12. +128 −0 bundled-libs/voku/simple-cache/src/voku/cache/AdapterMemcached.php
  13. +115 −0 bundled-libs/voku/simple-cache/src/voku/cache/AdapterOpCache.php
  14. +98 −0 bundled-libs/voku/simple-cache/src/voku/cache/AdapterPredis.php
  15. +88 −0 bundled-libs/voku/simple-cache/src/voku/cache/AdapterXcache.php
  16. +798 −0 bundled-libs/voku/simple-cache/src/voku/cache/Cache.php
  17. +152 −0 bundled-libs/voku/simple-cache/src/voku/cache/CacheChain.php
  18. +177 −0 bundled-libs/voku/simple-cache/src/voku/cache/CachePsr16.php
  19. +9 −0 bundled-libs/voku/simple-cache/src/voku/cache/Exception/InvalidArgumentException.php
  20. +27 −0 bundled-libs/voku/simple-cache/src/voku/cache/SerializerDefault.php
  21. +56 −0 bundled-libs/voku/simple-cache/src/voku/cache/SerializerIgbinary.php
  22. +27 −0 bundled-libs/voku/simple-cache/src/voku/cache/SerializerNo.php
  23. +73 −0 bundled-libs/voku/simple-cache/src/voku/cache/iAdapter.php
  24. +67 −0 bundled-libs/voku/simple-cache/src/voku/cache/iCache.php
  25. +29 −0 bundled-libs/voku/simple-cache/src/voku/cache/iSerializer.php
  26. +2 −1 composer.json
  27. +26 −29 include/functions_entries.inc.php
@@ -0,0 +1,48 @@
# Changelog 3.2.2 (2018-12-21)

- fix APC(u) detection for CLI usage


# Changelog 3.2.1 (2018-12-20)

- use phpcs fixer


# Changelog 3.2.0 (2018-12-03)

- "AdapterOpCache" -> use "opcache_compile_file()"
- add "AdapterFileSimple" + tests
- "Cache" -> add the possibility to disable the cache behavior via constructor() -> "disableCacheGetParameter, useCheckForAdminSession, useCheckForServerIpIsClientIp, useCheckForDev"


# Changelog 3.1.1 (2018-01-07)

- "AdapterApcu" -> fixed php-warning from "apcu_clear_cache()"


# Changelog 3.1.0 (2018-01-07)

- "AdapterOpCache" -> added
- "AdapterFile" -> remove duplicate file-get code


# Changelog 3.0.2 (2017-12-14)

- "AdapterFile" -> fix php warning


# Chabgelog 3.0.1 (2017-12-01)

- fix return from the "CacheChain"-class
- update phpunit-config


# Changelog 3.0.0 (2017-11-25)

- drop support for PHP < 7.0
- use "strict_types"


# Changelog 2.4.0 (2017-10-06)

- add support for PSR-16
@@ -0,0 +1,19 @@
Copyright (c) 2015 Lars Moelleken

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is furnished
to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
@@ -0,0 +1,105 @@
[![Build Status](https://travis-ci.org/voku/simple-cache.svg?branch=master)](https://travis-ci.org/voku/simple-cache)
[![FOSSA Status](https://app.fossa.io/api/projects/git%2Bgithub.com%2Fvoku%2Fsimple-cache.svg?type=shield)](https://app.fossa.io/projects/git%2Bgithub.com%2Fvoku%2Fsimple-cache?ref=badge_shield)
[![Coverage Status](https://coveralls.io/repos/github/voku/simple-cache/badge.svg?branch=master)](https://coveralls.io/github/voku/simple-cache?branch=master)
[![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/voku/simple-cache/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/voku/simple-cache/?branch=master)
[![Codacy Badge](https://api.codacy.com/project/badge/Grade/5846d2a46599486486b3956c0ce11a18)](https://www.codacy.com/app/voku/simple-cache)
[![Latest Stable Version](https://poser.pugx.org/voku/simple-cache/v/stable)](https://packagist.org/packages/voku/simple-cache)
[![Total Downloads](https://poser.pugx.org/voku/simple-cache/downloads)](https://packagist.org/packages/voku/simple-cache)
[![License](https://poser.pugx.org/voku/simple-cache/license)](https://packagist.org/packages/voku/simple-cache)


:zap: Simple Cache Class
===================

This is a simple Cache Abstraction Layer for PHP >= 7.0 that provides a simple interaction
with your cache-server. You can define the Adapter / Serializer in the "constructor" or the class will auto-detect you server-cache in this order:

1. Memcached / Memcache
2. Redis
3. Xcache
4. APC / APCu
5. OpCache (via PHP-files)
6. Static-PHP-Cache

## Get "Simple Cache"

You can download it from here, or require it using [composer](https://packagist.org/packages/voku/simple-cache).
```json
{
"require": {
"voku/simple-cache": "3.*"
}
}
```


## Install via "composer require"

```shell
composer require voku/simple-cache
composer require predis/predis # if you will use redis as cache, then add predis
```


## Quick Start

```php
use voku\cache\Cache;
require_once 'composer/autoload.php';
$cache = new Cache();
$ttl = 3600; // 60s * 60 = 1h
$cache->setItem('foo', 'bar', $ttl);
$bar = $cache->getItem('foo');
```


## Usage

```php
use voku\cache\Cache;
$cache = new Cache();
if ($cache->getCacheIsReady() === true && $cache->existsItem('foo')) {
return $cache->getItem('foo');
} else {
$bar = someSpecialFunctionsWithAReturnValue();
$cache->setItem('foo', $bar);
return $bar;
}
```

If you have an heavy task e.g. a really-big-loop, then you can also use static-cache.
But keep in mind, that this will be stored into PHP (it needs more memory).

```php
use voku\cache\Cache;
$cache = new Cache();
if ($cache->getCacheIsReady() === true && $cache->existsItem('foo')) {
for ($i = 0; $i <= 100000; $i++) {
echo $this->cache->getItem('foo', 3); // use also static-php-cache, when we hit the cache 3-times
}
return $cache->getItem('foo');
} else {
$bar = someSpecialFunctionsWithAReturnValue();
$cache->setItem('foo', $bar);
return $bar;
}
```

PS: By default, the static cache is also used by >= 10 cache hits. But you can configure
this behavior via $cache->setStaticCacheHitCounter(INT).

## No-Cache for the admin or a specific ip-address

If you use the parameter "$checkForUser" (=== true) in the constructor, then the cache isn't used for the admin-session.

-> You can also overwrite the check for the user, if you add a global function named "checkForDev()".


## License
[![FOSSA Status](https://app.fossa.io/api/projects/git%2Bgithub.com%2Fvoku%2Fsimple-cache.svg?type=large)](https://app.fossa.io/projects/git%2Bgithub.com%2Fvoku%2Fsimple-cache?ref=badge_large)
@@ -0,0 +1,35 @@
{
"name": "voku/simple-cache",
"description": "Simple Cache library",
"keywords": [
"php",
"cache",
"simple cache",
"caching"
],
"type": "library",
"homepage": "https://github.com/voku/simple-cache",
"license": "MIT",
"authors": [
{
"name": "Lars Moelleken",
"homepage": "http://www.moelleken.org/",
"role": "Developer"
}
],
"provide": {
"psr/simple-cache-implementation": "1.0"
},
"require": {
"php": ">=7.0.0",
"psr/simple-cache": "~1.0"
},
"require-dev": {
"phpunit/phpunit": "~6.0"
},
"autoload": {
"psr-4": {
"voku\\cache\\": "src/voku/cache/"
}
}
}
@@ -0,0 +1,158 @@
<?php
declare(strict_types=1);
namespace voku\cache;
/**
* AdapterApc: a APC-Cache adapter
*
* @see http://php.net/manual/de/book.apc.php
*/
class AdapterApc implements iAdapter
{
/**
* @var bool
*/
public $installed = false;
/**
* @var bool
*/
public $debug = false;
/**
* __construct()
*/
public function __construct()
{
if (
\PHP_SAPI !== 'cli'
&&
\function_exists('apc_store') === true
&&
\ini_get('apc.enabled')
) {
$this->installed = true;
}
if (
\PHP_SAPI === 'cli'
&&
\function_exists('apc_store') === true
&&
\ini_get('apc.enable_cli')
) {
$this->installed = true;
}
}
/**
* Check if apc-cache exists.
*
* WARNING: use $this->exists($key) instead
*
* @param string $key
*
* @return bool
*
* @internal
*/
public function apc_cache_exists($key): bool
{
return (bool) \apc_fetch($key);
}
/**
* Clears the APC cache by type.
*
* @param string $type - If $type is "user", the user cache will be cleared; otherwise,
* the system cache (cached files) will be cleared
*
* @return bool
*
* @internal
*/
public function cacheClear(string $type): bool
{
return (bool) \apc_clear_cache($type);
}
/**
* Retrieves cached information from APC's data store
*
* @param string $type - If $type is "user", information about the user cache will be returned
* @param bool $limited - If $limited is TRUE, the return value will exclude the individual list of cache
* entries. This is useful when trying to optimize calls for statistics gathering
*
* @return array|bool <p>Array of cached data (and meta-data) or FALSE on failure.</p>
*/
public function cacheInfo(string $type = '', bool $limited = false): array
{
return \apc_cache_info($type, $limited);
}
/**
* {@inheritdoc}
*/
public function exists(string $key): bool
{
if (\function_exists('apc_exists')) {
return (bool) \apc_exists($key);
}
return $this->apc_cache_exists($key);
}
/**
* {@inheritdoc}
*/
public function get(string $key)
{
if ($this->exists($key)) {
return \apc_fetch($key);
}
return null;
}
/**
* {@inheritdoc}
*/
public function installed(): bool
{
return $this->installed;
}
/**
* {@inheritdoc}
*/
public function remove(string $key): bool
{
return (bool) \apc_delete($key);
}
/**
* {@inheritdoc}
*/
public function removeAll(): bool
{
return (bool) ($this->cacheClear('system') && $this->cacheClear('user'));
}
/**
* {@inheritdoc}
*/
public function set(string $key, $value): bool
{
return (bool) \apc_store($key, $value);
}
/**
* {@inheritdoc}
*/
public function setExpired(string $key, $data, int $ttl = 0): bool
{
return (bool) \apc_store($key, $data, $ttl);
}
}
Oops, something went wrong.

0 comments on commit d52f000

Please sign in to comment.
You can’t perform that action at this time.