-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
=
committed
Mar 17, 2022
1 parent
fac031a
commit cc8f806
Showing
13 changed files
with
1,614 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
/.idea/ | ||
/.vs/ | ||
/.vscode/ | ||
/vendor/ | ||
/composer.lock |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,124 @@ | ||
# Cache | ||
# InitPHP Cache | ||
|
||
PSR-16 is a simple caching library that uses the Simple Cache interface. | ||
|
||
[![Latest Stable Version](http://poser.pugx.org/initphp/cache/v)](https://packagist.org/packages/initphp/cache) [![Total Downloads](http://poser.pugx.org/initphp/cache/downloads)](https://packagist.org/packages/initphp/cache) [![Latest Unstable Version](http://poser.pugx.org/initphp/cache/v/unstable)](https://packagist.org/packages/initphp/cache) [![License](http://poser.pugx.org/initphp/cache/license)](https://packagist.org/packages/initphp/cache) [![PHP Version Require](http://poser.pugx.org/initphp/cache/require/php)](https://packagist.org/packages/initphp/cache) | ||
|
||
## Requirements | ||
|
||
- PHP 5.6 or higher | ||
- [PSR-16 Simple Cache Interface](https://www.php-fig.org/psr/psr-16/) | ||
|
||
Depending on the handler you will use, it may need PHP extensions. | ||
|
||
- Redis | ||
- PDO | ||
- Wincache | ||
- Memcache or Memcached | ||
|
||
## Installation | ||
|
||
``` | ||
composer require initphp/cache | ||
``` | ||
|
||
## Usage | ||
|
||
```php | ||
require_once "../vendor/autoload.php"; | ||
use \InitPHP\Cache\Cache; | ||
|
||
$cache = Cache::create(\InitPHP\Cache\Handler\File::class, [ | ||
'path' => __DIR__ . '/Cache/'; | ||
]); | ||
|
||
if(($posts = $cache->get('posts', null)) === null){ | ||
$posts = [ | ||
['id' => '12', 'title' => 'Post 12 Title', 'content' => 'Post 12 Content'], | ||
['id' => '15', 'title' => 'Post 15 Title', 'content' => 'Post 15 Content'], | ||
['id' => '18', 'title' => 'Post 18 Title', 'content' => 'Post 18 Content'] | ||
]; | ||
$cache->set('posts', $posts, 120); | ||
} | ||
|
||
echo '<pre>'; print_r($posts) echo '</pre>'; | ||
``` | ||
|
||
## Configuration and Options | ||
|
||
### `\InitPHP\Cache\Handler\File::class` | ||
|
||
```php | ||
$options = [ | ||
'prefix' => 'cache_', | ||
'mode' => 0640, | ||
]; | ||
``` | ||
|
||
### `\InitPHP\Cache\Handler\Memcache::class` | ||
|
||
```php | ||
$options = [ | ||
'prefix' => 'cache_', | ||
'host' => '127.0.0.1', | ||
'port' => 11211, | ||
'weight' => 1, | ||
'raw' => false, | ||
'default_ttl' => 60, | ||
]; | ||
``` | ||
|
||
### `\InitPHP\Cache\Handler\PDO::class` | ||
|
||
```php | ||
$options = [ | ||
'prefix' => 'cache_', | ||
'dsn' => 'mysql:host=localhost;dbname=test', | ||
'username' => null, | ||
'password' => null, | ||
'charset' => 'utf8mb4', | ||
'collation' => 'utf8mb4_general_ci', | ||
'table' => 'cache' | ||
]; | ||
``` | ||
|
||
_Below is a sample cache table creation query for MySQL._ | ||
|
||
```sql | ||
CREATE TABLE `cache` ( | ||
`name` VARCHAR(255) NOT NULL, | ||
`ttl` INT(11) NULL DEFAULT NULL, | ||
`data` TEXT NOT NULL, | ||
UNIQUE (`name`) | ||
) ENGINE = InnoDB CHARSET=utf8mb4 COLLATE utf8mb4_general_ci; | ||
``` | ||
|
||
### `\InitPHP\Cache\Handler\Redis::class` | ||
|
||
```php | ||
$options = [ | ||
'prefix' => 'cache_', | ||
'host' => '127.0.0.1', | ||
'password' => null, | ||
'port' => 6379, | ||
'timeout' => 0, | ||
'database' => 0 | ||
]; | ||
``` | ||
|
||
### `\InitPHP\Cache\Handler\Wincache::class` | ||
|
||
```php | ||
$options = [ | ||
'prefix' => 'cache_', // Cache Name Prefix | ||
'default_ttl' => 60, // Used if ttl is NULL or not specified. | ||
]; | ||
``` | ||
|
||
## Credits | ||
|
||
- [Muhammet ŞAFAK](https://www.muhammetsafak.com.tr) <<[email protected]>> | ||
|
||
## License | ||
|
||
Copyright © 2022 [MIT License](./LICENSE) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
{ | ||
"name": "initphp/cache", | ||
"description": "PSR-16 is a simple caching library that uses the Simple Cache interface.", | ||
"keywords": ["psr-16", "simple-cache", "php", "cache", "caching"], | ||
"type": "library", | ||
"license": "MIT", | ||
"autoload": { | ||
"psr-4": { | ||
"InitPHP\\Cache\\": "src/" | ||
} | ||
}, | ||
"authors": [ | ||
{ | ||
"name": "Muhammet ŞAFAK", | ||
"email": "[email protected]", | ||
"role": "Developer", | ||
"homepage": "https://www.muhammetsafak.com.tr" | ||
} | ||
], | ||
"minimum-stability": "stable", | ||
"require": { | ||
"php": ">=5.6", | ||
"psr/simple-cache": "^1.0" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,190 @@ | ||
<?php | ||
/** | ||
* BaseHandler.php | ||
* | ||
* This file is part of InitPHP. | ||
* | ||
* @author Muhammet ŞAFAK <[email protected]> | ||
* @copyright Copyright © 2022 InitPHP | ||
* @license http://initphp.github.io/license.txt MIT | ||
* @version 0.1 | ||
* @link https://www.muhammetsafak.com.tr | ||
*/ | ||
|
||
namespace InitPHP\Cache; | ||
|
||
use InitPHP\Cache\Exception\CacheException; | ||
use InitPHP\Cache\Exception\InvalidArgumentException; | ||
|
||
use const CASE_LOWER; | ||
|
||
use function array_merge; | ||
use function array_change_key_case; | ||
use function is_callable; | ||
use function call_user_func_array; | ||
use function is_int; | ||
use function time; | ||
|
||
abstract class BaseHandler implements CacheInterface | ||
{ | ||
|
||
protected $_Options = [ | ||
'prefix' => 'cache_', | ||
]; | ||
|
||
public function __construct(array $options = []) | ||
{ | ||
if($this->isSupported() === FALSE){ | ||
throw new CacheException('In order to use this caching method, the necessary plugins must be installed/active.'); | ||
} | ||
$this->_Options = array_merge(array_change_key_case($this->_HandlerOption, CASE_LOWER), array_change_key_case($options, CASE_LOWER)); | ||
} | ||
|
||
public function setOptions($options = []) | ||
{ | ||
if(!empty($options)){ | ||
$this->_Options = array_merge($this->_Options, array_change_key_case($options, CASE_LOWER)); | ||
} | ||
return $this; | ||
} | ||
|
||
public function getOption($key, $default = null) | ||
{ | ||
$key = strtolower($key); | ||
return isset($this->_Options[$key]) ? $this->_Options[$key] : $default; | ||
} | ||
|
||
public function options(array $options = []) | ||
{ | ||
return empty($options) ? $this->_Options : array_merge($this->_Options, array_change_key_case($options, CASE_LOWER)); | ||
} | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
abstract public function get($key, $default = null); | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
abstract public function set($key, $value, $ttl = null); | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
abstract public function delete($key); | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
abstract public function clear(); | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
abstract public function has($key); | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
abstract public function increment($name, $offset = 1); | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
abstract public function decrement($name, $offset = 1); | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
public function getMultiple($keys, $default = null) | ||
{ | ||
if(!is_array($keys)){ | ||
throw new InvalidArgumentException("\$keys must be an array."); | ||
} | ||
$data = []; | ||
foreach ($keys as $key) { | ||
$data[$key] = $this->get($key, $default); | ||
} | ||
return $data; | ||
} | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
public function setMultiple($values, $ttl = null) | ||
{ | ||
if(!is_array($values)){ | ||
throw new InvalidArgumentException("\$values must be an array."); | ||
} | ||
if(!empty($values)){ | ||
foreach ($values as $key => $data) { | ||
$this->set($key, $data, $ttl); | ||
} | ||
return true; | ||
} | ||
return false; | ||
} | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
public function deleteMultiple($keys) | ||
{ | ||
if(!is_array($keys)){ | ||
throw new InvalidArgumentException("\$keys must be an array."); | ||
} | ||
if(!empty($keys)){ | ||
foreach ($keys as $key) { | ||
$this->delete($key); | ||
} | ||
return true; | ||
} | ||
return false; | ||
} | ||
|
||
/** | ||
* @return bool | ||
*/ | ||
abstract public function isSupported(); | ||
|
||
/** | ||
* @param string $name | ||
* @param string $chars | ||
* @return void | ||
* @throws InvalidArgumentException | ||
*/ | ||
protected function validationName($name, $chars = '{}()/\\@:') | ||
{ | ||
if(strpbrk($name, $chars) !== FALSE){ | ||
throw new InvalidArgumentException('Cache name cannot contain "' . $chars . '" characters.'); | ||
} | ||
} | ||
|
||
/** | ||
* @param null|int|\DateInterval $ttl | ||
* @return null|int|false | ||
*/ | ||
protected function ttlCalc($ttl = null) | ||
{ | ||
if($ttl === null){ | ||
return $ttl; | ||
} | ||
if($ttl instanceof \DateInterval){ | ||
$ttl = $ttl->format('U') - time(); | ||
} | ||
if(!is_int($ttl)){ | ||
throw new InvalidArgumentException("\$ttl can be an integer, NULL, or a \DateInterval object."); | ||
} | ||
if($ttl < 0){ | ||
return false; | ||
} | ||
return $ttl; | ||
} | ||
|
||
protected function reDefault($default = null) | ||
{ | ||
return is_callable($default) ? call_user_func_array($default, []) : $default; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
<?php | ||
/** | ||
* Cache.php | ||
* | ||
* This file is part of InitPHP. | ||
* | ||
* @author Muhammet ŞAFAK <[email protected]> | ||
* @copyright Copyright © 2022 InitPHP | ||
* @license http://initphp.github.io/license.txt MIT | ||
* @version 0.1 | ||
* @link https://www.muhammetsafak.com.tr | ||
*/ | ||
|
||
namespace InitPHP\Cache; | ||
|
||
use InitPHP\Cache\Exception\CacheException; | ||
|
||
use function is_string; | ||
use function class_exists; | ||
use function is_array; | ||
|
||
class Cache | ||
{ | ||
|
||
/** | ||
* @param string|object $handler | ||
* @param array $options | ||
* @return CacheInterface | ||
* @throws CacheException | ||
*/ | ||
public static function create($handler, $options = []) | ||
{ | ||
if(!is_array($options)){ | ||
throw new CacheException("\$options must be an associative array."); | ||
} | ||
if(is_string($handler) && class_exists($handler)){ | ||
$handler = new $handler(); | ||
} | ||
if($handler instanceof CacheInterface){ | ||
return $handler->setOptions($options); | ||
} | ||
throw new CacheException('The handler must be an object or class that uses a \\InitPHP\\Cache\\CacheInterface interface.'); | ||
} | ||
|
||
} |
Oops, something went wrong.