Skip to content

Commit cac1d47

Browse files
committed
發布 1.0.0
0 parents  commit cac1d47

File tree

5 files changed

+231
-0
lines changed

5 files changed

+231
-0
lines changed

.gitignore

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# 忽略 VSCode 的工作台設定檔案
2+
*.code-workspace
3+
4+
# 忽略 macOS 系統自動生成的 .DS_Store 檔案與資料夾圖標
5+
.DS_Store
6+
Icon?
7+
8+
# 忽略其他
9+
memo.md

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2025 邱敬幃 Pardn Chiu
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.

README.md

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
# PD\Redis
2+
3+
> A lightweight Redis client wrapper for PHP, providing simplified Redis operations with automatic connection management. Built on top of Predis.
4+
5+
![tag](https://img.shields.io/badge/tag-PHP%20Library-bb4444)
6+
![size](https://img.shields.io/github/size/pardnchiu/PHP-Redis/src/SQL.php)<br>
7+
![version](https://img.shields.io/packagist/v/pardnchiu/redis)
8+
![download](https://img.shields.io/packagist/dm/pardnchiu/redis)
9+
10+
## Features
11+
12+
- Automatic connection management
13+
- Environment-based configuration
14+
- Persistent connection support
15+
- Automatic database selection
16+
- Built-in error handling
17+
- Connection state monitoring
18+
- Automatic cleanup on destruction
19+
20+
## Required Environment Variables
21+
22+
```
23+
REDIS_HOST=localhost # Required: Redis server host
24+
REDIS_PORT=6379 # Required: Redis server port
25+
REDIS_PASSWORD=secret # Optional: Redis server password
26+
```
27+
28+
## How to Use
29+
30+
### Install
31+
32+
```SHELL
33+
composer require pardnchiu/redis
34+
```
35+
36+
```PHP
37+
// Initialize Redis client
38+
$redis = new PD\Redis();
39+
40+
// Set value with expiration
41+
$redis->set(0, "user:123", "user_data", 3600); // db 0, expires in 1 hour
42+
43+
// Get value
44+
$data = $redis->get(0, "user:123"); // from db 0
45+
46+
// Check connection status
47+
if ($redis->isConnected()) {
48+
// Redis is connected and ready
49+
}
50+
```
51+
52+
## License
53+
54+
This source code project is licensed under the [MIT](https://github.com/pardnchiu/PHP-Redis/blob/main/LICENSE) license.
55+
56+
## Creator
57+
58+
<img src="https://avatars.githubusercontent.com/u/25631760" align="left" width="96" height="96" style="margin-right: 0.5rem;">
59+
60+
<h4 style="padding-top: 0">邱敬幃 Pardn Chiu</h4>
61+
62+
<a href="mailto:[email protected]" target="_blank">
63+
<img src="https://pardn.io/image/email.svg" width="48" height="48">
64+
</a> <a href="https://linkedin.com/in/pardnchiu" target="_blank">
65+
<img src="https://pardn.io/image/linkedin.svg" width="48" height="48">
66+
</a>
67+
68+
***
69+
70+
©️ 2024 [邱敬幃 Pardn Chiu](https://pardn.io)

composer.json

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
{
2+
"name": "pardnchiu/redis",
3+
"description": " A lightweight Redis client wrapper for PHP, providing simplified Redis operations with automatic connection management. Built on top of Predis.",
4+
"type": "library",
5+
"license": "MIT",
6+
"authors": [
7+
{
8+
"name": "邱敬幃 Pardn Chiu",
9+
"email": "[email protected]"
10+
}
11+
],
12+
"minimum-stability": "stable",
13+
"require": {
14+
"php": ">=8.0",
15+
"predis/predis": "^2.0"
16+
},
17+
"require-dev": {},
18+
"autoload": {
19+
"psr-4": {
20+
"PD\\": "src/"
21+
},
22+
"exclude-from-classmap": [
23+
".gitignore",
24+
"*.md",
25+
"composer.json"
26+
]
27+
},
28+
"autoload-dev": {
29+
"psr-4": {}
30+
},
31+
"scripts": {},
32+
"extra": {
33+
"branch-alias": {}
34+
},
35+
"config": {
36+
"sort-packages": true
37+
}
38+
}

src/Redis.php

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
<?php
2+
3+
namespace PD;
4+
5+
class Redis
6+
{
7+
private $client;
8+
9+
// 建構函式,初始化 Redis 連線
10+
public function __construct()
11+
{
12+
$this->getConnection(); // 確保連線初始化
13+
}
14+
15+
// 檢查 Redis 是否已連線
16+
public function isConnected()
17+
{
18+
return $this->client !== null;
19+
}
20+
21+
// 連接 Redis 伺服器
22+
private function getConnection()
23+
{
24+
// 如果已經連線,則不再進行連線
25+
if ($this->client !== null) {
26+
return;
27+
};
28+
29+
try {
30+
// 從環境變數中讀取 Redis 配置,若無則使用預設值
31+
$host = (string) $_ENV["REDIS_HOST"] ?? "localhost";
32+
$port = (int) $_ENV["REDIS_PORT"] ?? 6379;
33+
$password = (string) $_ENV["REDIS_PASSWORD"] ?? '';
34+
$options = [
35+
"host" => $host,
36+
"port" => $port,
37+
"persistent" => true
38+
];
39+
40+
// 如果有密碼則添加至選項中
41+
if (!empty($password)) {
42+
$options["password"] = $password;
43+
};
44+
45+
// 使用 Predis 來建立 Redis 連線
46+
$this->client = new \Predis\Client($options);
47+
$this->client->select(0); // 選擇資料庫 0
48+
$this->client->connect(); // 建立連線
49+
} catch (\Exception $err) {
50+
// 若無法連線,輸出錯誤並設為 null
51+
PrintError("Redis 無法連線: " . $err->getMessage());
52+
http_response_code(500);
53+
$this->client = null;
54+
};
55+
}
56+
57+
// 從 Redis 中取得資料
58+
public function get($db, $key)
59+
{
60+
$this->getConnection(); // 確保連線有效
61+
62+
// 若 Redis 已連線,則從指定的資料庫取得指定鍵值的資料
63+
if ($this->client !== null) {
64+
$this->client->select($db);
65+
$result = $this->client->get($key);
66+
return $result;
67+
};
68+
69+
return null;
70+
}
71+
72+
// 設定資料到 Redis 中
73+
public function set($db, $key, $content, $expire)
74+
{
75+
$this->getConnection(); // 確保連線有效
76+
77+
// 若 Redis 已連線,則將資料寫入指定的資料庫,並設置過期時間
78+
if ($this->client !== null) {
79+
$this->client->select($db);
80+
$this->client->set($key, $content);
81+
$this->client->expire($key, $expire);
82+
};
83+
}
84+
85+
// 在物件銷毀時,斷開 Redis 連線
86+
public function __destruct()
87+
{
88+
if ($this->client !== null && $this->client->ping()) {
89+
$this->client->disconnect(); // 斷開連線
90+
$this->client = null;
91+
};
92+
}
93+
}

0 commit comments

Comments
 (0)