mabslabs/casper-cache
最新稳定版本:v1.0.0
Composer 安装命令:
composer create-project mabslabs/casper-cache
包简介
In-memory key/value cache server
README 文档
README
A simple in-memory cache server written in PHP that supports SET, GET, DEL, and ALL commands via TCP connections. This project allows clients to connect to the server and store key-value pairs in memory, with optional TTL (Time-To-Live) for each entry.
Features
- Supports
SET,GET,DEL, andALLcommands. - Cache values can be stored with optional TTL (default 30 minutes).
- Simple key-value storage in memory with automatic cleanup when TTL expires.
- Supports different data types for values (e.g., strings, integers, arrays, etc.).
Requirements
- PHP 8.1 or higher.
- A TCP client capable of sending JSON-based requests.
Installation
-
Clone this repository:
git clone https://github.com/kernel64/casper-cache.git cd casper-cache -
Install the dependencies: This project does not require any additional dependencies. (for the moment)
-
Start the cache server:
Run the
serverscript:php run.php
The server will start listening on
127.0.0.1:100824by default. Adjust the address and port as needed.
Example of use
-
** Using CacheClient.php:**
A client that connects to the Casper Cache server and sends requests.
<?php $client = new \Casper\CacheClient(); // Get a value $value = $client->get('key1'); echo $value; // Output: value1 // Get a value with a callback if it doesn't exist $newValue = $client->get('key2', function($key) { return 'generated_value'; }); echo $newValue; // Output: generated_value // Delete a value $response = $client->delete('key1'); print_r($response); // Response from server
-
More exemples in the test file :
php test.php
The client will connect to the server, send a SET request, and display the server response.
supported Commands
SET
- Command:
SET - Description: Store a value in the cache with a specified TTL (optional).
- Request Format (JSON):
{ "command": "SET", "key": "your_key", "value": "your_value", "ttl": 3600 }
GET
- Command:
GET - Description: Retrieve the value of a key from the cache.
- Request Format (JSON):
{ "command": "GET", "key": "your_key" }
DEL
- Command:
DEL - Description: Delete a key from the cache.
- Request Format (JSON):
{ "command": "DEL", "key": "your_key" }
ALL
- Command:
ALL - Description: Retrieve all keys and their values from the cache.
- Request Format (JSON):
{ "command": "ALL" }
TODO
- Add logging functionality.
- Support TLS connections.
License
This project is licensed under the MIT License.
统计信息
- 总下载量: 2
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 0
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-01-10