Skip to content

Commit b8d8a7b

Browse files
committed
First commit
1 parent 88b50ba commit b8d8a7b

File tree

2 files changed

+67
-6
lines changed

2 files changed

+67
-6
lines changed

README.md

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,58 @@
11
## InitPHP Redis Management
22

3+
This library was born to facilitate and customize the use of getter and setter of PHP and Redis.
4+
5+
## Requirements
6+
7+
- PHP 7.4 or later
8+
- PHP Redis Extension
9+
10+
## Installation
11+
312
```
413
composer require initphp/redis
514
```
615

16+
## Usage
17+
18+
```php
19+
require_once "vendor/autoload.php";
20+
use \InitPHP\Redis\Redis;
21+
22+
// Provide your connection information;
23+
$redis = new Redis([
24+
'prefix' => 'i_',
25+
'host' => '127.0.0.1',
26+
'password' => null,
27+
'port' => 6379,
28+
'timeout' => 0,
29+
'database' => 0,
30+
]);
31+
32+
// Use Setter and Getter;
33+
$redis->set('name', 'muhammet');
34+
if($redis->has('name')){
35+
echo $redis->get('name'); // "muhammet"
36+
}
37+
38+
/**
39+
* or tell the get method what it will
40+
* do if it can't find it,
41+
* or a default value it will return;
42+
*/
43+
echo $redis->get('username', 'Undefined'); // "Undefined"
44+
45+
echo $redis->get('surname', function () use ($redis) {
46+
$value = 'ŞAFAK';
47+
$redis->set('surname', $value);
48+
return $value;
49+
}); // "ŞAFAK"
50+
```
51+
52+
## Credits
53+
54+
- [Muhammet ŞAFAK](https://www.muhammetsafak.com.tr) <<[email protected]>>
55+
756
## License
857

958
Copyright &copy; 2022 [MIT License](./LICENSE)

src/Redis.php

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,15 @@
11
<?php
2+
/**
3+
* InitPHP/Redis
4+
*
5+
* This file is part of InitPHP Redis.
6+
*
7+
* @author Muhammet ŞAFAK <[email protected]>
8+
* @copyright Copyright © 2022 Muhammet ŞAFAK
9+
* @license ./LICENSE MIT
10+
* @version 1.0
11+
* @link https://www.muhammetsafak.com.tr
12+
*/
213

314
namespace InitPHP\Redis;
415

@@ -13,8 +24,9 @@
1324

1425
class Redis
1526
{
27+
1628
protected array $_Options = [
17-
'prefix' => 'cache_',
29+
'prefix' => 'i_',
1830
'host' => '127.0.0.1',
1931
'password' => null,
2032
'port' => 6379,
@@ -27,7 +39,7 @@ class Redis
2739
public function __construct(array $options = [])
2840
{
2941
if(!extension_loaded('redis')){
30-
throw new \RuntimeException();
42+
throw new \RuntimeException("The process has been terminated because the PHP Redis extension is not installed or enabled on your server.");
3143
}
3244
$this->_Options = array_merge($this->_Options, $options);
3345
}
@@ -42,7 +54,7 @@ public function __destruct()
4254
/**
4355
* @return \Redis
4456
*/
45-
public function getRedis(): \Redis
57+
public final function getRedis(): \Redis
4658
{
4759
if(isset($this->redis)){
4860
return $this->redis;
@@ -81,8 +93,8 @@ public function get(string $key, $default = null)
8193
$this->validationName($name);
8294
if(($data = $this->getRedis()->get($name)) !== FALSE){
8395
$data = unserialize($data);
84-
if(isset($data['__cache_type'], $data['__cache_value'])){
85-
return $data['__cache_value'];
96+
if(isset($data['__type'], $data['__value'])){
97+
return $data['__value'];
8698
}
8799
}
88100
return $this->reDefault($default);
@@ -116,7 +128,7 @@ public function set(string $key, $value, $ttl = null): bool
116128
default:
117129
return false;
118130
}
119-
if(!($this->getRedis()->set($name, serialize(['__cache_type' => $type, '__cache_value' => $value])))){
131+
if(!($this->getRedis()->set($name, serialize(['__type' => $type, '__value' => $value])))){
120132
return false;
121133
}
122134
if($ttl !== null){

0 commit comments

Comments
 (0)