struct Redis::JSON(Runnable)
- Redis::JSON(Runnable)
- Struct
- Value
- Object
Overview
Redis::JSON wraps a Redis::Client or Redis::Cluster to execute
commands against keys storing JSON data using the
RedisJSON plugin for Redis.
require "redis"
require "redis/json"
require "uuid/json"
struct Person
  include JSON::Serializable
  getter id : UUID
  getter name : String
  getter email : String
  def initialize(@id, @name, @email)
  end
end
redis = Redis::Client.new
id = UUID.random
redis.json.set "person:#{id}", ".", Person.new(id, "Jamie", "[email protected]")
redis.json.get("person:#{id}", ".", as: Person)
# => Person(@email="[email protected]", @id=UUID(081af617-6b50-48d3-b53f-dbc17a1072f5), @name="Jamie")
redis.json.get("person:#{id}", ".name", as: String)
# => "Jamie"Defined in:
json.crConstructors
Instance Method Summary
- 
        #arrappend(key : String, path : String, value)
        
          Append valueas JSON to the array located at the JSONPath inkey
- 
        #arrappend(key : String, path : String, *, values : Array)
        
          Append valuesas JSON to the array located at the JSONPath inkey
- 
        #arrindex(key : String, path : String, value, between range : Range(Int, Int | Nil))
        
          Get the index of valuein the array located at the JSONPath inkeyif and only if it falls in the specifiedrange
- 
        #arrindex(key : String, path : String, value)
        
          Get the index of valuein the array located at the JSONPath inkey
- 
        #arrinsert(key : String, path : String, index : Int, value)
        
          Insert valueinto the array located at the JSONPath inkeyatindex
- 
        #arrinsert(key : String, path : String, index : Int, *, values : Array)
        
          Insert the elements of valuesinto the array located at the JSONPath inkeyatindex
- 
        #arrlen(key : String, path : String)
        
          Get the number of elements in the array located at the JSONPath in key
- 
        #arrpop(key : String, path : String | Nil = nil, *, index : Int = -1)
        
          Remove and return the value located at index(defaulting to the last element) in the array located at the JSONPath inkey
- 
        #arrpop(key : String, path : String, *, index : Int = -1, as : T.class) : T | Nil forall T
        
          Remove and return the value located at index(defaulting to the last element) in the array located at the JSONPath inkey
- 
        #get(key : String, path : String, as type : T.class) : T | Nil forall T
        
          Get the value stored at the given JSONPath inside the given key, deserializing the value into the typeT.
- 
        #get(key : String, path : String)
        
          Get the raw JSON string representation at the given keynested at the given JSONPath.
- 
        #get(key : String, paths : Array(String), as type : T.class = ::JSON::Any) forall T
        
          Get the values stored at the specifed JSONPaths, deserializing them into the specified type as values in a Hash.
- 
        #get(key : String)
        
          Get the raw JSON string at the specified key
- 
        #mget(keys : Array(String), path : String, as type : T.class) : Array(T | Nil) forall T
        
          Get the values for all keysat the specified JSONPaths.
- 
        #numincrby(key : String, path : String, count : String | Int, as type : T.class) : T forall T
        
          Increment the number at the specified JSONPath 
- 
        #numincrby(key : String, path : String, count : String | Int)
        
          Increment the number at the specified JSONPath 
- 
        #set(key : String, path : String, value, *, nx = false, xx = false) : Nil
        
          Set the given key to the given JSON value.
Constructor Detail
Instance Method Detail
Append value as JSON to the array located at the JSONPath in key
redis.json.arrappend "posts:#{id}", ".tags", "redis"Append values as JSON to the array located at the JSONPath in key
redis.json.arrappend "posts:#{id}", ".tags", %w[redis crystal]Get the index of value in the array located at the JSONPath in key if
and only if it falls in the specified range
redis.json.arrindex "posts:#{id}", ".tags", "redis", between: 1..3
# => 2Get the index of value in the array located at the JSONPath in key
redis.json.arrindex "posts:#{id}", ".tags", "redis" # => 2Insert value into the array located at the JSONPath in key at index
redis.json.arrinsert "posts:#{id}", ".tags", index: 1, value: "redis"
# => 3Insert the elements of values into the array located at the JSONPath in
key at index
redis.json.arrinsert "posts:#{id}", ".tags", index: 1, values: %w[
  redis
  crystal
]
# => 4Get the number of elements in the array located at the JSONPath in key
redis.json.arrlen "posts:#{id}", ".tags"
# => 4Remove and return the value located at index (defaulting to the last
element) in the array located at the JSONPath in key
redis.json.arrlen "posts:#{id}", ".tags"
# => 4Remove and return the value located at index (defaulting to the last
element) in the array located at the JSONPath in key
redis.json.arrlen "posts:#{id}", ".tags"
# => 4NOTE  This method cannot be invoked on a pipeline or the transaction
yielded to a Redis::Connection#multi block.
Get the value stored at the given JSONPath inside the given key,
deserializing the value into the type T. You can use this to deserialize
your custom JSON::Serializable objects.
struct Order
  include JSON::Serializable
  getter id : UUID
  getter customer_id : UUID
  getter line_items : Array(LineItem)
end
struct LineItem
  include JSON::Serializable
  getter product_id : UUID
  getter quantity : Int32
  getter unit_price_cents : Int32
end
# Get the Order at the root
redis.json.get("order:#{id}", ".", as: Order)
# Get the LineItems at the `.line_items` JSONPath
# Note the `$` at the beginning to make it an array
redis.json.get("order:#{id}", "$.line_items", as: Array(LineItem))
# Get only the `product_id` properties of all the `line_items`
redis.json.get("order:#{id}", "$.line_items..product_id", as: Array(UUID))NOTE  This method cannot be used in a pipeline or Redis::Connection#multi
block because it operates on the value returned from the Redis server
inside the method, which isn't possible when using pipelines or multi.
Get the raw JSON string representation at the given key nested at the
given JSONPath.
email = redis.json.get("user:#{id}", ".email").as(String)
# => "[email protected]"Get the values stored at the specifed JSONPaths, deserializing them into
the specified type as values in a Hash.
if result = redis.json.get("posts:#{id}", %w[title tags], as: String | Array(String))
  result         # => {"title" => "JSON with Redis", tags: ["redis", "json", "crystal"]}
  typeof(result) # => Hash(String, String | Array(String))
endNOTE  This method cannot be used in a pipeline or Redis::Connection#multi
block because it operates on the value returned from the Redis server,
which isn't a thing when using pipelines or multi.
Get the values for all keys at the specified JSONPaths.
NOTE  This method cannot be used in a pipeline or Redis::Connection#multi
block because it operates on the value returned from the Redis server
inside the method, which isn't possible when using pipelines or multi.
NOTE  If you are using a cluster, the keys must all be on the same
shard, or you may only get some of them back with this method. When using
#mget in a cluster, you'll most likely want to use subhashed keys (with
{} around the same part of the name) to ensure all keys are on the same
shard.
Increment the number at the specified JSONPath
redis.json.numincrby "product:#{id}", ".purchase_count", 1, as: Int64
# => 2If key exists and an incrementable number exists at the specified
JSONPath (including the possibility of incrementing multiple numbers if
the JSONPath resolves to multiple numeric values), then this method
returns a value of type T. If the JSONPath resolves to multiple values
(for example, it begins with "$" or is recursive), you will need to
specify that it can be an Array of that type.
Increment the number at the specified JSONPath
redis.json.numincrby "product:#{id}", ".purchase_count", 1Set the given key to the given JSON value. The value will have
to_json called on it automatically. The nx and xx arguments are
identical to Redis::Commands#set.
Store a user at the root JSONPath (.) of the specified key:
struct User
  include JSON::Serializable
  getter id : UUID
  getter name : String
  getter email : String
end
redis.json.set "user:#{user.id}", ".", userUpdate (with xx so we ensure the key exists) a user's email by setting
the new email value at the .email JSONPath:
redis.json.set "user:#{id}", ".email", new_email, xx: true