Skip to content

Commit cc8f806

Browse files
author
=
committed
First commit
1 parent fac031a commit cc8f806

13 files changed

+1614
-1
lines changed

.gitignore

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
/.idea/
2+
/.vs/
3+
/.vscode/
4+
/vendor/
5+
/composer.lock

README.md

+124-1
Original file line numberDiff line numberDiff line change
@@ -1 +1,124 @@
1-
# Cache
1+
# InitPHP Cache
2+
3+
PSR-16 is a simple caching library that uses the Simple Cache interface.
4+
5+
[![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)
6+
7+
## Requirements
8+
9+
- PHP 5.6 or higher
10+
- [PSR-16 Simple Cache Interface](https://www.php-fig.org/psr/psr-16/)
11+
12+
Depending on the handler you will use, it may need PHP extensions.
13+
14+
- Redis
15+
- PDO
16+
- Wincache
17+
- Memcache or Memcached
18+
19+
## Installation
20+
21+
```
22+
composer require initphp/cache
23+
```
24+
25+
## Usage
26+
27+
```php
28+
require_once "../vendor/autoload.php";
29+
use \InitPHP\Cache\Cache;
30+
31+
$cache = Cache::create(\InitPHP\Cache\Handler\File::class, [
32+
'path' => __DIR__ . '/Cache/';
33+
]);
34+
35+
if(($posts = $cache->get('posts', null)) === null){
36+
$posts = [
37+
['id' => '12', 'title' => 'Post 12 Title', 'content' => 'Post 12 Content'],
38+
['id' => '15', 'title' => 'Post 15 Title', 'content' => 'Post 15 Content'],
39+
['id' => '18', 'title' => 'Post 18 Title', 'content' => 'Post 18 Content']
40+
];
41+
$cache->set('posts', $posts, 120);
42+
}
43+
44+
echo '<pre>'; print_r($posts) echo '</pre>';
45+
```
46+
47+
## Configuration and Options
48+
49+
### `\InitPHP\Cache\Handler\File::class`
50+
51+
```php
52+
$options = [
53+
'prefix' => 'cache_',
54+
'mode' => 0640,
55+
];
56+
```
57+
58+
### `\InitPHP\Cache\Handler\Memcache::class`
59+
60+
```php
61+
$options = [
62+
'prefix' => 'cache_',
63+
'host' => '127.0.0.1',
64+
'port' => 11211,
65+
'weight' => 1,
66+
'raw' => false,
67+
'default_ttl' => 60,
68+
];
69+
```
70+
71+
### `\InitPHP\Cache\Handler\PDO::class`
72+
73+
```php
74+
$options = [
75+
'prefix' => 'cache_',
76+
'dsn' => 'mysql:host=localhost;dbname=test',
77+
'username' => null,
78+
'password' => null,
79+
'charset' => 'utf8mb4',
80+
'collation' => 'utf8mb4_general_ci',
81+
'table' => 'cache'
82+
];
83+
```
84+
85+
_Below is a sample cache table creation query for MySQL._
86+
87+
```sql
88+
CREATE TABLE `cache` (
89+
`name` VARCHAR(255) NOT NULL,
90+
`ttl` INT(11) NULL DEFAULT NULL,
91+
`data` TEXT NOT NULL,
92+
UNIQUE (`name`)
93+
) ENGINE = InnoDB CHARSET=utf8mb4 COLLATE utf8mb4_general_ci;
94+
```
95+
96+
### `\InitPHP\Cache\Handler\Redis::class`
97+
98+
```php
99+
$options = [
100+
'prefix' => 'cache_',
101+
'host' => '127.0.0.1',
102+
'password' => null,
103+
'port' => 6379,
104+
'timeout' => 0,
105+
'database' => 0
106+
];
107+
```
108+
109+
### `\InitPHP\Cache\Handler\Wincache::class`
110+
111+
```php
112+
$options = [
113+
'prefix' => 'cache_', // Cache Name Prefix
114+
'default_ttl' => 60, // Used if ttl is NULL or not specified.
115+
];
116+
```
117+
118+
## Credits
119+
120+
- [Muhammet ŞAFAK](https://www.muhammetsafak.com.tr) <<[email protected]>>
121+
122+
## License
123+
124+
Copyright &copy; 2022 [MIT License](./LICENSE)

composer.json

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{
2+
"name": "initphp/cache",
3+
"description": "PSR-16 is a simple caching library that uses the Simple Cache interface.",
4+
"keywords": ["psr-16", "simple-cache", "php", "cache", "caching"],
5+
"type": "library",
6+
"license": "MIT",
7+
"autoload": {
8+
"psr-4": {
9+
"InitPHP\\Cache\\": "src/"
10+
}
11+
},
12+
"authors": [
13+
{
14+
"name": "Muhammet ŞAFAK",
15+
"email": "[email protected]",
16+
"role": "Developer",
17+
"homepage": "https://www.muhammetsafak.com.tr"
18+
}
19+
],
20+
"minimum-stability": "stable",
21+
"require": {
22+
"php": ">=5.6",
23+
"psr/simple-cache": "^1.0"
24+
}
25+
}

src/BaseHandler.php

+190
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,190 @@
1+
<?php
2+
/**
3+
* BaseHandler.php
4+
*
5+
* This file is part of InitPHP.
6+
*
7+
* @author Muhammet ŞAFAK <[email protected]>
8+
* @copyright Copyright © 2022 InitPHP
9+
* @license http://initphp.github.io/license.txt MIT
10+
* @version 0.1
11+
* @link https://www.muhammetsafak.com.tr
12+
*/
13+
14+
namespace InitPHP\Cache;
15+
16+
use InitPHP\Cache\Exception\CacheException;
17+
use InitPHP\Cache\Exception\InvalidArgumentException;
18+
19+
use const CASE_LOWER;
20+
21+
use function array_merge;
22+
use function array_change_key_case;
23+
use function is_callable;
24+
use function call_user_func_array;
25+
use function is_int;
26+
use function time;
27+
28+
abstract class BaseHandler implements CacheInterface
29+
{
30+
31+
protected $_Options = [
32+
'prefix' => 'cache_',
33+
];
34+
35+
public function __construct(array $options = [])
36+
{
37+
if($this->isSupported() === FALSE){
38+
throw new CacheException('In order to use this caching method, the necessary plugins must be installed/active.');
39+
}
40+
$this->_Options = array_merge(array_change_key_case($this->_HandlerOption, CASE_LOWER), array_change_key_case($options, CASE_LOWER));
41+
}
42+
43+
public function setOptions($options = [])
44+
{
45+
if(!empty($options)){
46+
$this->_Options = array_merge($this->_Options, array_change_key_case($options, CASE_LOWER));
47+
}
48+
return $this;
49+
}
50+
51+
public function getOption($key, $default = null)
52+
{
53+
$key = strtolower($key);
54+
return isset($this->_Options[$key]) ? $this->_Options[$key] : $default;
55+
}
56+
57+
public function options(array $options = [])
58+
{
59+
return empty($options) ? $this->_Options : array_merge($this->_Options, array_change_key_case($options, CASE_LOWER));
60+
}
61+
62+
/**
63+
* @inheritDoc
64+
*/
65+
abstract public function get($key, $default = null);
66+
67+
/**
68+
* @inheritDoc
69+
*/
70+
abstract public function set($key, $value, $ttl = null);
71+
72+
/**
73+
* @inheritDoc
74+
*/
75+
abstract public function delete($key);
76+
77+
/**
78+
* @inheritDoc
79+
*/
80+
abstract public function clear();
81+
82+
/**
83+
* @inheritDoc
84+
*/
85+
abstract public function has($key);
86+
87+
/**
88+
* @inheritDoc
89+
*/
90+
abstract public function increment($name, $offset = 1);
91+
92+
/**
93+
* @inheritDoc
94+
*/
95+
abstract public function decrement($name, $offset = 1);
96+
97+
/**
98+
* @inheritDoc
99+
*/
100+
public function getMultiple($keys, $default = null)
101+
{
102+
if(!is_array($keys)){
103+
throw new InvalidArgumentException("\$keys must be an array.");
104+
}
105+
$data = [];
106+
foreach ($keys as $key) {
107+
$data[$key] = $this->get($key, $default);
108+
}
109+
return $data;
110+
}
111+
112+
/**
113+
* @inheritDoc
114+
*/
115+
public function setMultiple($values, $ttl = null)
116+
{
117+
if(!is_array($values)){
118+
throw new InvalidArgumentException("\$values must be an array.");
119+
}
120+
if(!empty($values)){
121+
foreach ($values as $key => $data) {
122+
$this->set($key, $data, $ttl);
123+
}
124+
return true;
125+
}
126+
return false;
127+
}
128+
129+
/**
130+
* @inheritDoc
131+
*/
132+
public function deleteMultiple($keys)
133+
{
134+
if(!is_array($keys)){
135+
throw new InvalidArgumentException("\$keys must be an array.");
136+
}
137+
if(!empty($keys)){
138+
foreach ($keys as $key) {
139+
$this->delete($key);
140+
}
141+
return true;
142+
}
143+
return false;
144+
}
145+
146+
/**
147+
* @return bool
148+
*/
149+
abstract public function isSupported();
150+
151+
/**
152+
* @param string $name
153+
* @param string $chars
154+
* @return void
155+
* @throws InvalidArgumentException
156+
*/
157+
protected function validationName($name, $chars = '{}()/\\@:')
158+
{
159+
if(strpbrk($name, $chars) !== FALSE){
160+
throw new InvalidArgumentException('Cache name cannot contain "' . $chars . '" characters.');
161+
}
162+
}
163+
164+
/**
165+
* @param null|int|\DateInterval $ttl
166+
* @return null|int|false
167+
*/
168+
protected function ttlCalc($ttl = null)
169+
{
170+
if($ttl === null){
171+
return $ttl;
172+
}
173+
if($ttl instanceof \DateInterval){
174+
$ttl = $ttl->format('U') - time();
175+
}
176+
if(!is_int($ttl)){
177+
throw new InvalidArgumentException("\$ttl can be an integer, NULL, or a \DateInterval object.");
178+
}
179+
if($ttl < 0){
180+
return false;
181+
}
182+
return $ttl;
183+
}
184+
185+
protected function reDefault($default = null)
186+
{
187+
return is_callable($default) ? call_user_func_array($default, []) : $default;
188+
}
189+
190+
}

src/Cache.php

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
/**
3+
* Cache.php
4+
*
5+
* This file is part of InitPHP.
6+
*
7+
* @author Muhammet ŞAFAK <[email protected]>
8+
* @copyright Copyright © 2022 InitPHP
9+
* @license http://initphp.github.io/license.txt MIT
10+
* @version 0.1
11+
* @link https://www.muhammetsafak.com.tr
12+
*/
13+
14+
namespace InitPHP\Cache;
15+
16+
use InitPHP\Cache\Exception\CacheException;
17+
18+
use function is_string;
19+
use function class_exists;
20+
use function is_array;
21+
22+
class Cache
23+
{
24+
25+
/**
26+
* @param string|object $handler
27+
* @param array $options
28+
* @return CacheInterface
29+
* @throws CacheException
30+
*/
31+
public static function create($handler, $options = [])
32+
{
33+
if(!is_array($options)){
34+
throw new CacheException("\$options must be an associative array.");
35+
}
36+
if(is_string($handler) && class_exists($handler)){
37+
$handler = new $handler();
38+
}
39+
if($handler instanceof CacheInterface){
40+
return $handler->setOptions($options);
41+
}
42+
throw new CacheException('The handler must be an object or class that uses a \\InitPHP\\Cache\\CacheInterface interface.');
43+
}
44+
45+
}

0 commit comments

Comments
 (0)