View Source LibRedis (lib_redis v0.1.0)

LibRedis

LibRedis is a Redis client written in Elixir. It is essentially a wrapper around the Redix project, with added support for connection pooling and Redis cluster functionality. The client exposes two main APIs: command/3 and pipeline/3.

Please refer to the detailed project documentation: hexdocs

note

NOTE:

The implementation of the Cluster mode in production requires careful consideration and attention, as it has yet to undergo complete and rigorous testing.

features

Features

  • [x] Connection pooling
  • [x] Standalone mode
  • [x] Cluster mode

installation

Installation

To use LibRedis in your Elixir project, simply add it as a dependency in your mix.exs file:

defp deps do
  [
    {:lib_redis, "~> 0.1"}
  ]
end

Then, run mix deps.get to install the dependency.

usage

Usage

standalone

Standalone

To use LibRedis in singleton mode, you have to set your client as :standalone mode:

standalone_options = [
  name: :redis,
  mode: :standalone,
  url: "redis://:pwd@localhost:6379",
  pool_size: 5
]

standalone = LibRedis.new(standalone_options)

Then, add the LibRedis application to your supervision tree:

children = [
  {LibRedis, redis: standalone}
]

The options parameter is a keyword list that contains the connection options for Redis. The pool_size option specifies the number of connections in the pool. Thanks to powerful nimble_pool, we can easily use a nimble pool to manage redis connections.

Once you have a instance, you can use the command/3 and pipeline/3 APIs to execute Redis commands:

{:ok, result} = LibRedis.command(standalone, ["GET", "mykey"])

The command/3 function takes a LibRedis object, a Redis command as a list of strings, and an optional timeout in milliseconds. It returns a tuple with the status of the command execution and the result.

{:ok, result} = LibRedis.pipeline(standalone, [
  ["SET", "mykey", "value1"],
  ["GET", "mykey"]
])

The pipeline/3 function takes a LibRedis object and a list of Redis commands, where each command is represented as a list of strings. It returns a tuple with the status of the pipeline execution and a list of results.

redis-cluster

Redis Cluster

To use LibRedis with Redis cluster, first create a cluster using the LibRedis.Cluster module:

cluster_options = [
  name: :redis,
  mode: :cluster,
  url: "redis://localhost:6381,redis://localhost:6382,redis://localhost:6383,redis://localhost:6384,redis://localhost:6385",
  password: "123456",
  pool_size: 5
]

cluster = LibRedis.new(cluster_options)

Then, add the LibRedis application to your supervision tree:

children = [
  {LibRedis, redis: cluster}
]

The cluster_options parameter is a keyword list that contains the connection options for Redis cluster.

Once you have a cluster, you can use the command/3 and pipeline/3 APIs to execute Redis commands:

{:ok, result} = LibRedis.command(cluster, ["GET", "mykey"])

The command/3 function takes a cluster object, a Redis command as a list of strings, and an optional timeout in milliseconds. It returns a tuple with the status of the command execution and the result.

{:ok, result} = LibRedis.pipeline(cluster, [
  ["SET", "mykey", "value1"],
  ["GET", "mykey"]
])

The pipeline/3 function takes a cluster object and a list of Redis commands, where each command is represented as a list of strings. It returns a tuple with the status of the pipeline execution and a list of results.

test

Test

To run unit tests, you can start a Redis service using Docker locally and then execute the mix test command.

The specific way to start the Redis service can be referred to in the run.md file under the docker-compose directory.

Cover compiling modules ...
............................
Finished in 3.3 seconds (0.00s async, 3.3s sync)
28 tests, 0 failures

Randomized with seed 28971

Generating cover results ...

Percentage | Module
-----------|--------------------------
    68.75% | LibRedis.Pool
    76.47% | LibRedis.SlotFinder
    80.00% | LibRedis.Cluster
    86.67% | LibRedis.ClientStore.Default
    96.43% | LibRedis
   100.00% | LibRedis.Client
   100.00% | LibRedis.ClientStore
   100.00% | LibRedis.SlotStore
   100.00% | LibRedis.SlotStore.Default
   100.00% | LibRedis.Standalone
   100.00% | LibRedis.Typespecs
-----------|--------------------------
    82.93% | Total

Coverage test failed, threshold not met:

    Coverage:   82.93%
    Threshold:  90.00%

license

License

LibRedis is released under the MIT License.

Link to this section Summary

Link to this section Types

@type options() :: [
  name: atom(),
  mode: term(),
  url: binary(),
  password: binary(),
  pool_size: non_neg_integer(),
  client_store_module: term(),
  slot_store_module: term()
]
@type t() :: %LibRedis{
  client: Client.client(),
  client_store_module: module(),
  mode: :cluster | :standalone,
  name: LibRedis.Typespecs.name(),
  password: LibRedis.Typespecs.password(),
  pool_size: non_neg_integer(),
  slot_store_module: module(),
  url: LibRedis.Typespecs.url()
}

Link to this section Functions

Link to this function

command(redis, command, opts \\ [])

View Source

execute a redis command

examples

Examples

iex> LibRedis.command(redis, ["SET", "key", "value"])
{:ok, "OK"}
@spec new(options()) :: t()

create a new redis client

options

Options

  • :name (atom/0) - The name of the redis client The default value is :redis.

  • :mode - The mode of the redis client, :standalone or :cluster The default value is :standalone.

  • :url (String.t/0) - Required. The url of the redis server, like redis://:123456@localhost:6379

  • :password (String.t/0) - The password of the redis server, this option is useful in cluster mode The default value is "".

  • :pool_size (non_neg_integer/0) - The pool size of the redis client's pool The default value is 10.

  • :client_store_module (term/0) - The implementation of the client store, see LibRedis.ClientStore for more details The default value is LibRedis.ClientStore.Default.

  • :slot_store_module (term/0) - The implementation of the slot store, see LibRedis.SlotStore for more details The default value is LibRedis.SlotStore.Default.

examples

Examples

iex> standalone_options = [
  name: :redis,
  mode: :standalone,
  url: "redis://:pwd@localhost:6379",
  pool_size: 5
]
iex> LibRedis.new(standalone_options)
Link to this function

pipeline(redis, commands, opts \\ [])

View Source

execute a redis pipeline

examples

Examples

iex> LibRedis.pipeline(redis, [["SET", "key", "value"], ["GET", "key"]])
{:ok, ["OK", "value"]}
@spec start_link(keyword()) :: GenServer.on_start()