Skip to content

caomengxuan666/Astra

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

68 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Astra - High-performance Redis-compatible Cache Middleware

Build Status License

Project Overview

Astra is a high-performance Redis-compatible cache middleware based on C++17, featuring modular design with the following core values:

  • Provides thread-safe LRU cache implementation
  • Supports Redis protocol network communication
  • Implements command pattern client SDK, including C++ SDK, C SDK, and LabVIEW SDK, compatible with hiredis library
  • Supports TTL expiration mechanism and background cleanup
  • Supports service mode startup on Windows
  • Provides publish/subscribe mode and Lua script execution
  • Supports RDB snapshot saving and restoration
  • Supports persistence with LevelDB for high-performance data storage

Multilingual Documentation

Please select your preferred language version for detailed information.

Project Screenshots

alt text

Project Statistics

Core Modules

Module Function Technical Implementation
Core Basic type definitions and macros C++17 features
Utils Logging system/scope protection RAII pattern
DataStructures Lock-free queue/LRU cache CAS atomic operations
concurrent Concurrency support framework Thread pool/task queue/task flow
CacheServer Redis protocol parsing Asio asynchronous IO
Client SDK Command pattern encapsulation Polymorphic design

Supported Redis Commands

Key-Value Commands

  • GET, SET, DEL, EXISTS, KEYS, TTL, MGET, MSET

Numeric Commands

  • INCR, DECR

Connection Commands

  • PING

Server Commands

  • COMMAND, INFO

Publish/Subscribe Commands

  • SUBSCRIBE, UNSUBSCRIBE, PUBLISH, PSUBSCRIBE, PUNSUBSCRIBE

Lua Script Commands

  • EVAL, EVALSHA

Concurrent Module Design

concurrent module provides a complete concurrency solution:

  1. ThreadPool Thread Pool

    • Priority scheduling support (lower value first)
    • Work stealing algorithm for load balancing optimization
    • Full lifecycle management (Stop/Join/Resume)
    • Supports mixed mode of local task queues and global task queues
  2. TaskQueue Task Queue

    • Supports task submission with callbacks
    • Provides Post/Submit interfaces
    • Implements asynchronous task scheduling based on thread pool
  3. Task Flow Orchestration

    • SeriesWork: Serial task group (similar to Promise chaining)
    • ParallelWork: Parallel task group
    • TaskPipeline: Stateful task chain with shared context
// Example: Concurrent task orchestration
auto queue = TaskQueue::Create();

// Serial task
SeriesWork::Create(queue)
    ->Then([]{ std::cout << "Step 1"; })
    ->Then([]{ std::cout << "Step 2"; })
    ->Run();

// Parallel task
ParallelWork::Create(queue)
    ->Add([]{ /* Task A */ })
    ->Add([]{ /* Task B */ })
    ->Run();

Technical Highlights

  • Six Major Technical Features:
    • Asynchronous network model (based on Asio + custom thread pool)
    • Sharding lock mechanism to enhance concurrency performance
    • Lock-free data structures to optimize access efficiency
    • Full protocol support (Redis RESP compatible)
    • Extensible command pattern design
    • Multi-level task queue to optimize resource scheduling

Client SDK

Astra provides client SDKs in multiple languages, making it easy for developers to integrate into their applications:

C++ SDK

For C++ developers, it provides a complete interface to access Astra features, supporting all Redis-compatible commands.

C SDK

For C developers, it provides C language interfaces to access Astra features, compatible with standard C language specifications.

LabVIEW SDK

For LabVIEW developers, it provides Astra access interfaces in the LabVIEW environment, facilitating cache integration in LabVIEW.

Windows Service Mode

Astra supports running in service mode on Windows systems, providing background persistent operation capabilities:

  • Supports installation as a Windows service
  • Supports starting and stopping the service
  • Supports setting automatic startup
  • Stable operation in service mode, supports automatic startup after system restart

Quick Start

Build Requirements

  • C++17 compatible compiler (GCC 9+/Clang 10+)
  • CMake 3.14+
  • System dependencies: libfmt-dev libssl-dev

Build Steps

# Clone project
$ git clone https://github.com/caomengxuan666/Astra.git
$ cd Astra

# Initialize submodules (LevelDB etc.)
$ git submodule update --init --recursive

# Build project
$ mkdir build && cd build
$ cmake ..
$ make -j$(nproc)

# Install project
$ sudo make install

Command Line Parameters

Astra-CacheServer supports the following command line parameters:

Parameter Description Default
-p, --port Specify listening port 6380
--bind Specify bind address 127.0.0.1
-l, --loglevel Set log level(trace/debug/info/warn/error/fatal) info
-c, --coredump Persistence file path None
-m, --maxsize Maximum LRU cache size No limit
--file Enable file logging false
--cluster Enable cluster mode false
--cluster-port Cluster communication port 16380
--persistence-type Persistence type(file/leveldb) leveldb
--leveldb-path LevelDB storage path ./astra_leveldb

Start Service

# Start cache server with LevelDB persistence
$ Astra-CacheServer -p 6380 --bind 0.0.0.0 --persistence-type leveldb --leveldb-path ./astra_leveldb

# Start cache server with file persistence
$ Astra-CacheServer -p 6380 --persistence-type file -c dump.rdb

# Install as Windows service
$ Astra-CacheServer.exe install

# Cluster mode example
$ Astra-CacheServer --cluster --cluster-port 16380 -p 6380

Feature Demonstration

#include "sdk/astra_client.hpp"

int main() {
    Astra::Client::AstraClient client("127.0.0.1", 8080);
    
    // Basic operations
    client.Set("key", "value");
    auto val = client.Get("key");
    
    // Cache with TTL
    client.Set("temp_key", "value", std::chrono::seconds(10));
    auto ttl = client.TTL("temp_key");
    
    // Atomic operations
    client.Incr("counter");
    auto count = client.Get("counter");
    
    // Batch operations
    std::vector<std::pair<std::string, std::string>> kvs = {
        {"key1", "value1"},
        {"key2", "value2"}
    };
    client.MSet(kvs);
    
    std::vector<std::string> keys = {"key1", "key2"};
    auto values = client.MGet(keys);
    
    // Publish/Subscribe
    // Subscription requires a separate client instance
    // Publish message
    client.Publish("channel", "Hello, Astra!");
}

Directory Structure

Astra/
├── Astra-CacheServer/    # Redis-compatible cache service
│   ├── platform/         # Platform-specific code
│   │   └── windows/      # Windows service mode implementation
│   ├── sdk/              # Client SDKs (multiple language implementations)
│   └── ...               # Server core code
├── core/                 # Core type definitions
├── utils/                # Utility classes (logging/ScopeGuard)
├── concurrent/           # Concurrency support (thread pool/TaskQueue)
├── datastructures/       # Data structures (LRU/lock-free queue)
├── tests/                # GTest unit tests
├── third-party/          # Third-party dependencies (Asio/fmt)
└── benchmark/            # Performance tests

Common Issues

Q: Why does "Failed to send response: 远程主机强迫关闭了一个现有的连接" appear?

A: This occurs because the client exits immediately after calling the DEL method, causing the server's message to be considered "lost," as 'DEL' itself returns 'OK'. However, this does not affect the program's normal operation. The client does not need to block indefinitely to wait for the server to confirm the successful deletion of data. Normally, the delete operation will not fail.

Contribution Guide

Please refer to CONTRIBUTING.md for the complete contribution guide, including code style, submission requirements, and review process.

Issue Reporting

Please submit bug reports or feature requests through GitHub Issues

License

This project is licensed under the MIT License - see LICENSE file

About

A high-performance resp server for both windows and linux

Topics

Resources

License

Contributing

Stars

Watchers

Forks

Packages

No packages published