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!