Redis::Stream
Sugar coating Redis Streams
TODO: add documentation
Installation
Add this line to your application's Gemfile:
gem 'redis-stream'
And then execute:
$ bundle
Or install it yourself as:
$ gem install redis-stream
Usage
Load the stream library
require 'redis/stream'
Available objects
Redis::Stream::Config
Simple way to read and manage a config file. It looks for a config.yml file in the current and './config' directory.
name
name defaults to config.yml
include Redis::Stream
puts Config.name
Config.name = "test.yml"
path
path to the config file
include Redis::Stream
puts Config.path
Config.path = "./configDEV"
[key]
reads and writes the key or key/value from/to the config file
include Redis::Stream
puts Config[:cache]
Config[:cache] = "./cache"
include?(key)
check if key exists in the config file
file_exists?()
check if the config file exists
init
This function is called implicitly. You do not need to call it
Redis::Stream::Client
Redis::Stream::Inspect
Redis::Stream::Type
Redis::Stream::DataCache
A simple non-blocking example
require 'redis/stream'
s1 = Redis::Stream::Client.new("test", "LIST", 't1')
s2 = Redis::Stream::Client.new("test", "MANIFEST", 't2')
s2.on_message do |message|
m = message['payload']
puts "Hello #{m}"
s1.stop
s2.stop
end
s1.start(false)
s2.start(false)
id = s1.add("World!", "to" => "*", "group" => "MANIFEST", "type" => Redis::Stream::Type::ACTION)
Timeout::timeout(10) do
loop do
break unless s1.running? || s2.running?
sleep 1
puts "checkin if still active #{s1.running?}, #{s2.running?}"
end
end
Microservices example
- Sinatra as a point of entry
http://127.0.0.1:4567?reverse=word
- Microservice for processing
http.rb
require 'sinatra'
require 'redis/stream'
class GreetingsApp < Sinatra::Base
configure do
set :inline_templates, true
set :redis_stream, Redis::Stream::Client.new("greetings", "HTTP", "http_client", "sync_start" => true, "caching" => false)
end
get '/' do
halt 500, 'reverse parameter not found' unless params.include?(:reverse)
result = settings.redis_stream.sync_add(params[:reverse], "group" => "GREETER", "time_out" => 60)
@reverse = params[:reverse]
@reversed = ''
@reversed = result['payload'] if result && result.include?('payload')
erb :index
end
end
GreetingsApp.run!
__END__
@@index
<!DOCTYPE html>
<head><title>Reverse Greeter</title></head>
<body>
<p><%= @reverse %> <=> <%= @reversed %></p>
</body>
</html>
reverse_greeter.rb
require 'redis/stream'
reverse_greeter = Redis::Stream::Client.new("greetings", "GREETER", "reverse_greeter")
reverse_greeter.on_message do |message|
begin
greeting = message['payload']
reverse_greeter.add(greeting.reverse, "to" => message['from'])
rescue Exception => e
end
end
reverse_greeter.start(true, false)
Development
After checking out the repo, run bin/setup
to install dependencies. Then, run rake test
to run the tests. You can also run bin/console
for an interactive prompt that will allow you to experiment.
To install this gem onto your local machine, run bundle exec rake install
. To release a new version, update the version number in version.rb
, and then run bundle exec rake release
, which will create a git tag for the version, push git commits and tags, and push the .gem
file to rubygems.org.
Contributing
Bug reports and pull requests are welcome on GitHub at https://github.com/mehmetc/redis-stream. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the Contributor Covenant code of conduct.
License
The gem is available as open source under the terms of the MIT License.
Code of Conduct
Everyone interacting in the Redis::Stream project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct.