crystal-redis

repository·master·Indexed 18 days ago

https://github.com/stefanwille/crystal-redis

A high-performance Redis client for the Crystal programming language. It supports pipelining, transactions, LUA scripting, and various Redis data types. Includes Redis::PooledClient for safe connection pooling across multiple fibers.

Tokens
543
Snippets
5
Records
5
Agent score
14%

What's inside crystal-redis

  1. Install crystal-redis via shards

    master

    To add crystal-redis to your project, add the following dependency to your shard.yml file:

    dependencies:
      redis:
        github: stefanwille/crystal-redis

    Then, run the following command in your terminal to install the library:

    $ shards install
  2. Configure Redis Unix socket for development

    master

    To run the project's test suite (crystal spec), you must have a local Redis server running with a Unix socket enabled at /tmp/redis.sock.

    In your redis.conf file, ensure the following line is uncommented:

    nunixsocket /tmp/redis.sock

    WARNING: Running the test suite will delete all data in database 0.

  3. Troubleshoot OpenSSL installation on MacOS X

    master

    If you encounter linker errors like ld: library not found for -lssl or warnings that libssl and libcrypto are not found in the pkg-config search path, it is because OpenSSL is not installed by default on MacOS X.

    To fix this:

    1. Install OpenSSL using Homebrew.
    2. Set the PKG_CONFIG_PATH environment variable to point to the OpenSSL installation.
    $ brew install openssl
    $ export PKG_CONFIG_PATH=/usr/local/opt/openssl/lib/pkgconfig
  4. Basic usage of the Redis client

    master

    To use the client, require the redis package and instantiate a new Redis object. You can then call standard Redis commands directly on the instance.

    require "redis"
    
    redis = Redis.new
    redis.set("foo", "bar")
    redis.get("foo") # => "bar"
  5. Use connection pooling with Redis::PooledClient

    master

    For applications that share a Redis instance across multiple fibers (such as web frameworks like Kemal), use Redis::PooledClient. This class provides a built-in connection pool that automatically allocates and frees connections per command, making it safe to share across fibers.

    redis = Redis::PooledClient.new
    
    10.times do |i|
      spawn do
        redis.set("foo#{i}", "bar")
        redis.get("foo#{i}") # => "bar"
      end
    end