搜尋套件、儲存庫與 APP 分類

套件詳情v1.2.0

resp_client

Dart 的 RESP(Redis 序列化協議)用戶端。它提供低階與高階 API 來執行 Redis 命令。

AndroidiOSLinuxmacOSWindows
近 30 天下載
11.5萬
喜歡數
7
Pub 分數
140

安裝套件

flutter pub add resp_client

套件介紹

根據套件公開資料產生的選型評估,僅供參考。

一個 Dart 套件,實作了 Redis 序列化協定(RESP),用於與 Redis 資料庫互動。它提供低階與高階 API 來傳送 Redis 命令,實現與 Redis 伺服器的高效通訊。專為需要直接控制 RESP 訊息,或希望使用簡化介面進行常見作業的開發者設計,支援所有主要平台,包括 Android、iOS、Linux、macOS 及 Windows。此套件輕量,適用於需要快速、可靠 Redis 整合的應用程式。

適用情境

  • 從 Flutter 應用連接到 Redis
  • 以程式化方式執行 Redis 命令
  • 建構即時資料服務
  • 在後端系統中整合 Redis
  • 處理 Redis 發布/訂閱訊息

主要特色

  • 底層 RESP 訊息處理
  • 高階命令 API
  • 跨平台支援
  • 輕量且高效
  • 支援 Redis 連接池

適合對象

  • 使用 Redis 的 Dart 開發人員
  • 需要 Redis 集成的 Flutter 應用
  • 使用 Redis 的後端服務
  • 即時應用開發者
  • 需要高效率資料儲存的系統

使用建議

  • 需要手動處理 RESP 回應中的錯誤
  • 與高階函式庫相比,文件較少
  • 最適合熟悉 RESP 協議的開發者
  • 在沒有額外抽象的情況下,不適合複雜的 Redis 使用案例
  • 可能需要自定義包裝器以實現進階 Redis 功能

原始 README

以下為關聯原始碼儲存庫的 README 原文快照,最新內容請造訪原始碼儲存庫。

resp_client

A RESP (REdis Serialization Protocol) client for Dart. This package contains 3 libraries. resp_server implements the connectionn to the server, resp_client implements the Redis serialization protocol. resp_commands provides implementations of Redis commands in differents levels of convenience from low-level to easy-to-use.

3 Building blocks

There 3 fundamental building block:

  • RespServerConnection - A connection to a RESP server
  • RespClient - A RESP client connected to a RESP server implementing the request/response model and the RESP types
  • RespCommandsTierN - Implementation of Redis commands

Usage

Initialization and cleanup


import 'package:resp_client/resp_client.dart';
import 'package:resp_client/resp_commands.dart';
import 'package:resp_client/resp_server.dart';

void main(List<String> args) async {
  // create a server connection using sockets
  final server = await connectSocket('localhost');

  // create a client using the server connection
  final client = RespClient(server);

  // create commands of tier 2 using the client
  final commands = RespCommandsTier2(client);

  // execute some commands...

  // close connection to the server
  await server.close();
}

RESP Types

The RESP types are implemented by the classes:

  • RespSimpleString - payload type String
  • RespBulkString - payload type String?
  • RespInteger - payload type int
  • RespArray - payload type List<RespType>?
  • RespError - payload type String

All classes extends the class RestType. You can access the payload of a type using the getter payload.

The class RespType provides methods to convert to a concrete type and getters to check if it is a concrete type.

Use commands

Not all Redis commands are implemented in all tiers yet. Nevertheless, it is possible to execute very Redis command using tier 0. You can create a command object of every tier using a RespClient. It is allowed to create multiple command objects on the same client. Command objects of tier 1 and 2 provides a getter to the underlying tier.

Tier 0

This is the most basic and flexible tier. You can execute every Redis command using this tier. You have to provide a list of elements. All elements are converted to bulk strings using toString(). The result is a RespType. It is up to the user to convert the result to a concrete RESP type.


import 'package:resp_client/resp_server.dart';
import 'package:resp_client/resp_client.dart';
import 'package:resp_client/resp_commands.dart';

void main(List<String> args) async {

  // ... setup connection and client
  
  // create commands using the client
  final commands = RespCommandsTier0(client);

  // execute a command
  final result = await commands.execute(['SET', 'myKey', 'myValue', 'NX']);
  print(result.payload);

  // ... close connection
}

Tier 1

Commands of tier 1 provide some convenience on the parameter side. The user no longer has to build the commands from ground up. Instead, the commands are calle in a more Dart-like style. The result is always a RespType like in tier 0.


import 'package:resp_client/resp_server.dart';
import 'package:resp_client/resp_client.dart';
import 'package:resp_client/resp_commands.dart';

void main(List<String> args) async {

  // ... setup connection and client
  
  // create commands using the client
  final commands = RespCommandsTier1(client);

  // Execute the GET command. You don't have to build the command from ground up.
  // The result is a RespType. This is flexible but you have to convert to the
  // concrete type yourself.
  final result = await commands.get('mKey', 'myValue', mode: SetMode.onlyIfNotExists);
  print(result.toBulkString().payload);

  // ... close connection
}

Tier 2

Commands of tier 2 provide the same convenience on the parameter side as tier 1 commands. Additionally, the result of a command is converted for easy handling.

To convert the result, assumptions are made. As an example the GET command returns a bulk string. Thus, the implementation tries to convert the result from a RespType to a RespBulkString and then return the payload of type String?. But, if a GET command is executed as part of a transaction, the result is a RespSimpleString and the payload has different meaning. Thus, the command will fail. To get out of this, use the same command of the underlying tier.


void main(List<String> args) async {

  // ... setup connection and client
  
  // create commands using the client
  final commands = RespCommandsTier2(client);

  // Like tier 1 commands but the result is already convert.
  // In this case, result is of type String?.
  final result = await commands.get('mKey', 'myValue', mode: SetMode.onlyIfNotExists);
  print(result)

  // ... close connection
}

所屬分類

原始碼儲存庫

使用此套件的專案

暫無相關資料。

所在榜單