rust-cache (Redis-like cache in Rust)
English | ไทย
English
Redis-like cache in Rust
In-memory key-value cache server written in Rust, designed as a lightweight Redis-like alternative.
It speaks the Redis RESP protocol sufficiently for basic commands and can be used with ioredis
from Node.js.
This project is developed by SIAMINNOVATOR CO., LTD. (Thailand) as an open source alternative for those who need a Redis-like cache server without worrying about Redis licensing issues.
Quick Install
Option 1: One-line install (requires Rust)
|
Option 2: From source
Option 3: Using Cargo (if published to crates.io)
Features
- Basic commands:
PING,GET,SET,DEL,EXPIRE,TTL,KEYS,INCR,DECR,INCRBY,DECRBY,FLUSHALL - Optional TTL support for
SETviaEXoption - In-memory store using sharded concurrent map (
dashmap) - Background cleanup task for expired keys
- Async TCP server based on Tokio, multiple concurrent clients
- Authentication support (username/password)
- Rate limiting for failed login attempts
- Configurable memory limits and buffer sizes
- JSON data storage support
Build (Linux)
The resulting binary will be at:
Run & Config
The server searches for config files in this order (first found wins):
RUST_CACHE_CONFIGenvironment variable (explicit override)/etc/rust-cache/rust-cache.conf(system-wide config)~/.config/rust-cache/rust-cache.toml(user config)./rust-cache.toml(local, current working directory)
If no config file is found, defaults are used:
- Listen on
127.0.0.1:7777 - Auto-detect total RAM on Linux and use ~50% as max memory
- Use 4 KB socket read buffer per connection
Example:
# Use system config
# Or use user config
# Or override with env
RUST_CACHE_CONFIG=/path/to/custom.toml
See rust-cache.toml for all available options.
Storing JSON Data
Yes! rust-cache stores data as binary (bytes), so you can store JSON strings directly.
Just serialize your JSON on the client side before SET, and deserialize after GET:
Node.js example:
const Redis = require;
// Without authentication
const redis = ;
// With password
const redis = ;
// With username and password
const redis = ;
// Store JSON
const data = ;
await redis.;
// Retrieve JSON
const jsonStr = await redis.;
const parsed = JSON.;
CLI example:
}
Use from Node.js with ioredis
Install ioredis in your Node.js project:
Example client (see examples/ioredis-client.js):
# Without authentication
# With password (configure in examples/.env)
# Create examples/.env file with: REDIS_PASSWORD=your-password
# With username and password
# Create examples/.env file with:
# REDIS_USERNAME=admin
# REDIS_PASSWORD=your-password
The example will connect to 127.0.0.1:7777, run PING, SET, GET, EXPIRE, TTL, and log results.
Systemd Service
Install as a systemd service:
After changing config, restart:
License
This software is provided under the MIT License. You are free to use, modify, and
distribute it without restriction, including for commercial purposes. See the LICENSE
file for the full text.
ไทย
Redis-like cache ใน Rust
เซิร์ฟเวอร์ cache แบบ key-value ในหน่วยความจำที่เขียนด้วย Rust ออกแบบมาเป็นทางเลือก
ที่เบาและเร็วกว่า Redis รองรับโปรโตคอล Redis RESP สำหรับคำสั่งพื้นฐานและใช้งานร่วมกับ
ioredis จาก Node.js ได้
โปรเจกต์นี้พัฒนาโดย SIAMINNOVATOR CO., LTD. (ประเทศไทย) เพื่อเป็นทางเลือก open source ที่ไม่มีปัญหาลิขสิทธิ์สำหรับผู้ที่ต้องการใช้ Redis-like cache server โดยไม่ต้องกังวลเรื่องลิขสิทธิ์ของ Redis
ติดตั้งแบบรวดเร็ว
วิธีที่ 1: ติดตั้งด้วยคำสั่งเดียว (ต้องมี Rust)
|
วิธีที่ 2: จาก source code
วิธีที่ 3: ใช้ Cargo (ถ้า publish ไป crates.io แล้ว)
ฟีเจอร์
- คำสั่งพื้นฐาน:
PING,GET,SET,DEL,EXPIRE,TTL,KEYS,INCR,DECR,INCRBY,DECRBY,FLUSHALL - รองรับ TTL สำหรับ
SETผ่าน optionEX - เก็บข้อมูลในหน่วยความจำด้วย sharded concurrent map (
dashmap) - มี background task ทำความสะอาด key ที่หมดอายุ
- Async TCP server ใช้ Tokio รองรับหลาย client พร้อมกัน
- รองรับการยืนยันตัวตน (username/password)
- Rate limiting สำหรับการ login ที่ล้มเหลว
- กำหนดขีดจำกัดหน่วยความจำและขนาด buffer ได้
- รองรับการเก็บข้อมูล JSON
Build (Linux)
Binary ที่ได้จะอยู่ที่:
รันและตั้งค่า
เซิร์ฟเวอร์จะค้นหาไฟล์ config ตามลำดับนี้ (ใช้ตัวแรกที่เจอ):
- ตัวแปร environment
RUST_CACHE_CONFIG(override) /etc/rust-cache/rust-cache.conf(config ระดับระบบ)~/.config/rust-cache/rust-cache.toml(config ของผู้ใช้)./rust-cache.toml(config ในโฟลเดอร์ปัจจุบัน)
ถ้าไม่เจอไฟล์ config จะใช้ค่า default:
- ฟังที่
127.0.0.1:7777 - ตรวจจับ RAM ทั้งหมดบน Linux อัตโนมัติและใช้ ~50% เป็น max memory
- ใช้ socket read buffer 4 KB ต่อ connection
ตัวอย่าง:
# ใช้ config ระดับระบบ
# หรือใช้ config ของผู้ใช้
# หรือ override ด้วย env
RUST_CACHE_CONFIG=/path/to/custom.toml
ดูตัวเลือกทั้งหมดได้ใน rust-cache.toml
เก็บข้อมูล JSON
ได้เลย! rust-cache เก็บข้อมูลเป็น binary (bytes) ดังนั้นสามารถเก็บ JSON string ได้โดยตรง
แค่ serialize JSON ฝั่ง client ก่อน SET และ deserialize หลัง GET:
ตัวอย่าง Node.js:
const Redis = require;
// ไม่มี authentication
const redis = ;
// มี password
const redis = ;
// มี username และ password
const redis = ;
// เก็บ JSON
const data = ;
await redis.;
// ดึง JSON
const jsonStr = await redis.;
const parsed = JSON.;
ตัวอย่าง CLI:
}
ใช้งานจาก Node.js กับ ioredis
ติดตั้ง ioredis ในโปรเจกต์ Node.js:
ตัวอย่าง client (ดู examples/ioredis-client.js):
# ไม่มี authentication
# มี password (ตั้งค่าใน examples/.env)
# สร้างไฟล์ examples/.env แล้วใส่: REDIS_PASSWORD=your-password
# มี username และ password
# สร้างไฟล์ examples/.env แล้วใส่:
# REDIS_USERNAME=admin
# REDIS_PASSWORD=your-password
ตัวอย่างจะเชื่อมต่อไปที่ 127.0.0.1:7777 แล้วรัน PING, SET, GET, EXPIRE, TTL และแสดงผลลัพธ์
Systemd Service
ติดตั้งเป็น systemd service:
หลังจากแก้ config แล้ว restart:
สิทธิ์การใช้งาน
ซอฟต์แวร์นี้เปิดให้ใช้งานได้อย่างเสรี ไม่มีค่าใช้จ่าย และไม่มีข้อจำกัดในการใช้งาน
เชิงพาณิชย์ ดูรายละเอียดเพิ่มเติมในไฟล์ LICENSE
พัฒนาโดย SIAMINNOVATOR CO., LTD. (ประเทศไทย)