From 69dea8029437529615510fcbfeb80f7a0070d179 Mon Sep 17 00:00:00 2001 From: Jamie Gaskins Date: Mon, 1 May 2023 18:32:09 -0500 Subject: [PATCH] Add the abilty to get a binary value for a key --- spec/redis_spec.cr | 5 +++++ src/connection.cr | 11 +++++++++++ 2 files changed, 16 insertions(+) diff --git a/spec/redis_spec.cr b/spec/redis_spec.cr index fdb7037..cdecc6b 100644 --- a/spec/redis_spec.cr +++ b/spec/redis_spec.cr @@ -34,6 +34,11 @@ describe Redis::Client do redis.get(key).should eq nil end + test "can get a string key as Bytes" do + redis.set key, "bar" + redis.get_bytes(key).should eq "bar".to_slice + end + test "can set expiration timestamps on keys" do redis.set key, "foo", ex: 10.milliseconds.from_now redis.get(key).should eq "foo" diff --git a/src/connection.cr b/src/connection.cr index a31b40a..96bf13b 100644 --- a/src/connection.cr +++ b/src/connection.cr @@ -339,6 +339,17 @@ module Redis xrevrange: Array, xtrim: Int64, }) + + + # Get the value for the specified key + # + # ``` + # redis.set "foo", "bar" + # redis.get("foo", as: Bytes) # => "bar" + # ``` + def get_bytes(key : String) + get(key).try(&.to_slice) + end end set_return_types!