Crate fred

source ·
Expand description

Fred

License License CircleCI Crates.io API docs

An async Redis client for Rust.

Example

use fred::prelude::*;

#[tokio::main]
async fn main() -> Result<(), RedisError> {
  let config = RedisConfig::default();
  let perf = PerformanceConfig::default();
  let policy = ReconnectPolicy::default();
  let client = RedisClient::new(config, Some(perf), Some(policy));
  
  // connect to the server, returning a handle to the task that drives the connection
  let _ = client.connect();
  let _ = client.wait_for_connect().await?;
 
  // convert responses to many common Rust types
  let foo: Option<String> = client.get("foo").await?;
  assert!(foo.is_none());
  
  let _: () = client.set("foo", "bar", None, None, false).await?;
  // or use turbofish to declare response types
  println!("Foo: {:?}", client.get::<String, _>("foo").await?);
  
  // or use a lower level interface for responses to defer parsing, etc
  let foo: RedisValue = client.get("foo").await?;
  assert_eq!(foo.as_str().unwrap(), "bar");
  
  let _ = client.quit().await?;
  Ok(())
}

See the examples for more.

Features

  • Supports RESP2 and RESP3 protocol modes.
  • Supports clustered, centralized, and sentinel Redis deployments.
  • Optional built-in reconnection logic with multiple backoff policies.
  • Publish-Subscribe and keyspace events interfaces.
  • Supports transactions.
  • Supports Lua scripts.
  • Supports streaming results from the MONITOR command.
  • Supports custom commands provided by third party modules.
  • Supports TLS connections via native-tls and/or rustls.
  • Supports streaming interfaces for scanning functions.
  • Supports pipelining.
  • Automatically retry requests under bad network conditions.
  • Optional built-in tracking for network latency and payload size metrics.
  • An optional client pooling interface to round-robin requests among a pool of clients.
  • An optional sentinel client for interacting directly with sentinel nodes to manually fail over servers, etc.
  • An optional pubsub subscriber client that will automatically manage channel subscriptions.
  • An optional interface to override DNS resolution logic.
  • Optional support for JSON values.

Build Time Features

NameDefaultDescription
enable-native-tlsEnable TLS support via native-tls.
enable-rustlsEnable TLS support via rustls.
vendored-opensslEnable the native-tls/vendored feature, if possible.
ignore-auth-errorxIgnore auth errors that occur when a password is supplied but not required.
metricsEnable the metrics interface to track overall latency, network latency, and request/response sizes.
reconnect-on-auth-errorA NOAUTH error is treated the same as a general connection failure and the client will reconnect based on the reconnection policy. This is recommended if callers are using ElastiCache.
pool-prefer-activexPrefer connected clients over clients in a disconnected state when using the RedisPool interface.
full-tracingEnable full tracing support. This can emit a lot of data.
partial-tracingEnable partial tracing support, only emitting traces for top level commands and network latency.
blocking-encodingUse a blocking task for encoding or decoding frames. This can be useful for clients that send or receive large payloads, but will only work when used with a multi-thread Tokio runtime.
network-logsEnable TRACE level logging statements that will print out all data sent to or received from the server. These are the only logging statements that can ever contain potentially sensitive user data.
custom-reconnect-errorsEnable an interface for callers to customize the types of errors that should automatically trigger reconnection logic.
monitorEnable an interface for running the MONITOR command.
sentinel-clientEnable an interface for communicating directly with Sentinel nodes. This is not necessary to use normal Redis clients behind a sentinel layer.
sentinel-authEnable an interface for using different authentication credentials to sentinel nodes.
subscriber-clientEnable an optional subscriber client that manages channel subscription state for callers.
serde-jsonEnable an interface to automatically convert Redis types to JSON.
no-client-setnameDisable the automatic CLIENT SETNAME command used to associate server logs with client logs.
mocksEnable a mocking layer interface that can be used to intercept and process commands in tests.
dnsEnable an interface that allows callers to override the DNS lookup logic.
check-unresponsiveEnable additional monitoring to detect unresponsive connections.
replicasEnable an interface that routes commands to replica nodes.
client-trackingEnable a client tracking interface.

Re-exports

Modules

  • Redis client implementations.
  • Error structs returned by Redis commands.
  • Utility functions for reading or changing global config values.
  • Traits that implement portions of the Redis interface.
  • mocksmocks
    An interface for mocking Redis commands.
  • monitormonitor
    An interface to run the MONITOR command.
  • Client pooling structs.
  • Convenience module to import a RedisClient, all possible interfaces, error types, and common argument types or return value types.
  • The structs and enums used by the Redis client.
  • Utility functions used by the client that may also be useful to callers.

Macros

  • Public macro to create a Bytes from a static byte slice without copying.
  • Public macro to create a Str from a static str slice without copying.