Kredis
Kredis (Keyed Redis) encapsulates higher-level types and data structures around a single key, so you can interact with them as coherent objects rather than isolated procedural commands. These higher-level structures can be configured as attributes within Active Models and Active Records using a declarative DSL.
Kredis is configured using env-aware YAML files, using Rails.application.config_for
, so you can locate the data structures on separate Redis instances, if you've reached a scale where a single shared instance is no longer sufficient.
Kredis provides namespacing support for keys such that you can safely run parallel testing against the data structures without different tests trampling each others data.
Examples
Kredis provides typed scalars for strings, integers, decimals, floats, booleans, datetimes, and JSON hashes:
string = Kredis.string "mystring"
string.value = "hello world!"
"hello world!" == string.value
integer = Kredis.integer "myinteger"
integer.value = 5
5 == integer.value
decimal = Kredis.decimal "mydecimal"
decimal.value = "%.47f" % (1.0 / 10)
BigDecimal("0.10000000000000000555111512312578270211815834045e0") == decimal.value
float = Kredis.float "myfloat"
float.value = 1.0 / 10
0.1 == float.value
boolean = Kredis.boolean "myboolean"
boolean.value = true
true == boolean.value
datetime = Kredis.datetime "mydatetime"
memoized_midnight = Time.zone.now.midnight
datetime.value = memoized_midnight
memoized_midnight == datetime.value
json = Kredis.json "myjson"
json.value = { "one" => 1, "two" => "2" }
{ "one" => 1, "two" => "2" } == json.value
There are data structures for counters, enums, flags, lists, unique lists, sets, and slots:
list = Kredis.list "mylist"
list << "hello world!"
[ "hello world!" ] == list.elements
list.clear
integer_list = Kredis.list "myintegerlist", typed: :integer, default: [ 1, 2, 3 ]
integer_list.append([ 4, 5, 6 ])
integer_list << 7
[ 1, 2, 3, 4, 5, 6, 7 ] == integer_list.elements
integer_list.clear
unique_list = Kredis.unique_list "myuniquelist"
unique_list.append(%w[ 2 3 4 ])
unique_list.prepend(%w[ 1 2 3 4 ])
unique_list.append([])
unique_list << "5"
unique_list.remove(3)
[ "4", "2", "1", "5" ] == unique_list.elements
unique_list.clear
ordered_set = Kredis.ordered_set "myorderedset"
ordered_set.append(%w[ 2 3 4 ])
ordered_set.prepend(%w[ 1 2 3 4 ])
ordered_set.append([])
ordered_set << "5"
ordered_set.remove(3)
[ "4", "2", "1", "5" ] == ordered_set.elements
set = Kredis.set "myset", typed: :datetime
set.add(DateTime.tomorrow, DateTime.yesterday)
set << DateTime.tomorrow
2 == set.size
[ DateTime.tomorrow, DateTime.yesterday ] == set.members
set.clear
hash = Kredis.hash "myhash"
hash.update("key" => "value", "key2" => "value2")
{ "key" => "value", "key2" => "value2" } == hash.to_h
"value2" == hash["key2"]
%w[ key key2 ] == hash.keys
%w[ value value2 ] == hash.values
hash.remove
high_scores = Kredis.hash "high_scores", typed: :integer
high_scores.update(space_invaders: 100, pong: 42)
%w[ space_invaders pong ] == high_scores.keys
[ 100, 42 ] == high_scores.values
{ "space_invaders" => 100, "pong" => 42 } == high_scores.to_h
head_count = Kredis.counter "headcount"
0 == head_count.value
head_count.increment
head_count.increment
head_count.decrement
1 == head_count.value
counter = Kredis.counter "mycounter", expires_in: 5.seconds
counter.increment by: 2
2 == counter.value
sleep 6.seconds
0 == counter.value
counter.reset
cycle = Kredis.cycle "mycycle", values: %i[ one two three ]
:one == cycle.value
cycle.next
:two == cycle.value
cycle.next
:three == cycle.value
cycle.next
:one == cycle.value
cycle.reset
enum = Kredis.enum "myenum", values: %w[ one two three ], default: "one"
"one" == enum.value
true == enum.one?
enum.value = "two"
"two" == enum.value
enum.three!
"three" == enum.value
enum.value = "four"
"three" == enum.value
enum.reset
"one" == enum.value
slots = Kredis.slots "myslots", available: 3
true == slots.available?
slots.reserve
true == slots.available?
slots.reserve
true == slots.available?
slots.reserve
false == slots.available?
slots.reserve
false == slots.available?
slots.release
true == slots.available?
slots.reset
slot = Kredis.slot "myslot"
true == slot.available?
slot.reserve
false == slot.available?
slot.release
true == slot.available?
slot.reset
flag = Kredis.flag "myflag"
false == flag.marked?
flag.mark
true == flag.marked?
flag.remove
false == flag.marked?
true == flag.mark(expires_in: 1.second, force: false)
false == flag.mark(expires_in: 10.seconds, force: false)
true == flag.marked?
sleep 0.5.seconds
true == flag.marked?
sleep 0.6.seconds
false == flag.marked?
limiter = Kredis.limiter "mylimit", limit: 3, expires_in: 5.seconds
0 == limiter.value
limiter.poke
limiter.poke
false == limiter.exceeded?
limiter.poke
true == limiter.exceeded?
sleep 6
limiter.poke
limiter.poke
false == limiter.exceeded?
Models
You can use all these structures in models:
class Person < ApplicationRecord
kredis_list :names
kredis_list :names_with_custom_key_via_lambda, key: ->(p) { "person:#{p.id}:names_customized" }
kredis_list :names_with_custom_key_via_method, key: :generate_names_key
kredis_unique_list :skills, limit: 2
kredis_enum :morning, values: %w[ bright blue black ], default: "bright"
kredis_counter :steps, expires_in: 1.hour
private
def generate_names_key
"key-generated-from-private-method"
end
end
person = Person.find(5)
person.names.append "David", "Heinemeier", "Hansson"
true == person.morning.bright?
person.morning.value = "blue"
true == person.morning.blue?
Default values
You can set a default value for all types. For example:
list = Kredis.list "favorite_colors", default: [ "red", "green", "blue" ]
class Person < ApplicationRecord
kredis_string :name, default: "Unknown"
kredis_list :favorite_colors, default: [ "red", "green", "blue" ]
end
There's a performance overhead to consider though. When you first read or write an attribute in a model, Kredis will
check if the underlying Redis key exists, while watching for concurrent changes, and if it does not,
write the specified default value.
This means that using default values in a typical Rails app additional Redis calls (WATCH, EXISTS, UNWATCH) will be
executed for each Kredis attribute with a default value read or written during a request.
Callbacks
You can also define after_change
callbacks that trigger on mutations:
class Person < ApplicationRecord
kredis_list :names, after_change: ->(p) { }
kredis_unique_list :skills, limit: 2, after_change: :skillset_changed
def skillset_changed
end
end
Multiple Redis servers
And using structures on a different than the default shared
redis instance, relying on config/redis/secondary.yml
:
one_string = Kredis.string "mystring"
two_string = Kredis.string "mystring", config: :secondary
one_string.value = "just on shared"
two_string.value != one_string.value
Installation
- Run
./bin/bundle add kredis
- Run
./bin/rails kredis:install
to add a default configuration at config/redis/shared.yml
Additional configurations can be added under config/redis/*.yml
and referenced when a type is created. For example, Kredis.string("mystring", config: :strings)
would lookup config/redis/strings.yml
.
Kredis passes the configuration to Redis.new
to establish the connection. See the Redis documentation for other configuration options.
If you don't have config/redis/shared.yml
(or use another named configuration), Kredis will default to look in env for REDIS_URL
, then fallback to a default URL of redis://127.0.0.1:6379/0
.
Redis support
Kredis works with Redis server 4.0+, with the Redis Ruby client version 4.2+. Redis Cluster is not supported.
Setting SSL options on Redis Connections
If you need to connect to Redis with SSL, the recommended approach is to set your Redis instance manually by adding an entry to the Kredis::Connections.connections
hash. Below an example showing how to connect to Redis using Client Authentication:
Kredis::Connections.connections[:shared] = Redis.new(
url: ENV["REDIS_URL"],
ssl_params: {
cert_store: OpenSSL::X509::Store.new.tap { |store|
store.add_file(Rails.root.join("config", "ca_cert.pem").to_s)
},
cert: OpenSSL::X509::Certificate.new(File.read(
Rails.root.join("config", "client.crt")
)),
key: OpenSSL::PKey::RSA.new(
Rails.application.credentials.redis[:client_key]
),
verify_mode: OpenSSL::SSL::VERIFY_PEER
}
)
The above code could be added to either config/environments/production.rb
or an initializer. Please ensure that your client private key, if used, is stored your credentials file or another secure location.
Configure how the redis client is created
You can configure how the redis client is created by setting config.kredis.connector
in your application.rb
:
config.kredis.connector = ->(config) { SomeRedisProxy.new(config) }
By default Kredis will use Redis.new(config)
.
Development
A development console is available by running bin/console
.
From there, you can experiment with Kredis. e.g.
>> str = Kredis.string "mystring"
Kredis (0.1ms) Connected to shared
=>
#<Kredis::Types::Scalar:0x0000000134c7d938
...
>> str.value = "hello, world"
Kredis Proxy (2.4ms) SET mystring ["hello, world"]
=> "hello, world"
>> str.value
Run tests with bin/test
.
debug
can be used in the development console and in the test suite by inserting a
breakpoint, e.g. debugger
.
License
Kredis is released under the MIT License.