Giter Site home page Giter Site logo

redis's Introduction

redis

A pure-Crystal implementation of the Redis protocol

Installation

  1. Add the dependency to your shard.yml:

    dependencies:
      redis:
        github: jgaskins/redis

    Note that this shard currently depends on a fork of crystal-db for its connection pool. I'm in the process of getting those changes merged upstream so it can depend on the mainline implementation of that shard.

  2. Run shards

Usage

require "redis"

redis = Redis::Client.new # Defaults to `localhost` port 6379

redis.set "foo", "bar"
redis.get "foo" # => "bar"

redis.incr "counter" # => 1
redis.incr "counter" # => 2
redis.decr "counter" # => 1

redis.del "foo", "counter" # => 2

Pipelined queries

To mitigate latency with multiple queries whose inputs and outputs are completely independent of each other, you can "pipeline" your queries by sending them all at once before reading them. To do this, you can use the pipeline method:

redis.pipeline do |pipe|
  pipe.incr "foo"
  pipe.set "bar", "baz"
  pipe.lpush "my-list", "my value"
end

The return value of pipeline will be an array containing the values of each of those calls in the order they were sent. So in this case, it might be [1, nil, 2] to match the return values of incr, set, and lpush, respectively.

Transactions

The Redis MULTI command begins a transaction, so you can use the multi method to execute a transaction against the server:

redis.multi do |txn|
  txn.set "foo", "bar"
  txn.incr "baz"
  txn.lpush "my-list", "my value"
end

The transaction is automatically committed with EXEC at the end of the block. If an exception occurs within the block, the transaction will be rolled back with DISCARD before exiting the block.

You may also call txn.discard, which will effectively disable the transaction (all further methods called on the transaction do nothing), but will not exit the block. You will need to exit the block explicitly with break if there are operations within the block that cannot be rolled back, such as sending an email or sending a request to a third-party API.

The reason for this is that the only way to exit a containing block from an inner method in Crystal is to raise an exception, and this library chooses not to use exceptions for flow control.

Beyond localhost

To use a Redis server that isn't at localhost:6379, pass a URI to the client. For example, if you store it in your shell environment:

redis = Redis::Client.new(URI.parse(ENV["REDIS_URL"]))

# ... or ...

redis = Redis::Client.from_env("REDIS_URL")

To connect via SSL, make sure you use the rediss:// URL scheme. If your Redis server requires a password or uses a different database slot than 0, make sure you include them in the URL:

redis = Redis::Client.new(URI.parse("rediss://:[email protected]/3"))

Connection Pool

The Redis::Client maintains its own connection pool, so there is no need to run your own within your application. When you execute a command on the Redis::Client, it is automatically being executed against a connection. When you execute a pipeline or transaction with multi, all commands within that block will automatically be routed to the same connection.

Development

Make sure you have a Redis or KeyDB server running locally on port 6379.

Contributing

  1. Fork it (https://github.com/jgaskins/redis/fork)
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create a new Pull Request

Contributors

redis's People

Contributors

jgaskins avatar mamantoha avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.