We can use the Redis client to perform direct operations on Redis database or to get instances of RediSearch and RedisML extensions. The Redis client is based on Jedis, a Redis java client.
1 Using redis client
The Redis client provides and api
to perform operations on Redis database
while handling the Jedis objects pool. This way, we don't need to take care from
taking and returning redis clients to pool.
The simplest operation to Redis is to check if it's running using the ping
command.
<script> var client = Ax.ext.db.getRedisClient(); if (client.isReady()) { console.log(client.ping()); } else { console.log("No REDIS available"); } </script>
PONG
2 Redis commands
Using Redis client we can access the following built in methods. For more information on Redis commands check redis.io/commands.
2.1 Hash
HASHes in Redis allow you to store groups of key-value pairs in a single higher-level Redis key.
Command | Description | Signature | Return |
---|---|---|---|
HSET | Set the string value of a hash field. Returns the number of fields that were added. |
hset(String key, String field, String value); | long |
HGET | Get the value of a hash field. Returns the value associated with field; nil when field is not present in the hash or key does not exist. |
hget(String key, String field); | String |
HGETALL | Get all the fields and values in a hash. Returns list of fields and their values stored in the hash, or an empty list when key does not exist. |
hgetAll(String key); | Map |
<script> const client = Ax.ext.db.getRedisClient(); /* Create new Hash 'example_hash' and Set field 'user_code' with value 'u0001' Set field 'user_name' with value 'Pepe' */ var hsetUserCode = client.hset('example_hash', 'user_code', 'u0001'); var hsetUserName = client.hset('example_hash', 'user_name', 'Pepe'); /* Get the value of the field 'user_code' */ var hget = client.hget('example_hash', 'user_code'); /* Get all the fields with their values */ var hgetAll = client.hgetAll('example_hash'); /* Print results */ console.log('HSET (UserCode):'+hsetUserCode); console.log('HSET (UserName):'+hsetUserName); console.log('HGET:'+hget); console.log('HGETALL:'+hgetAll); /* Iterate through hgetAll and retrieve fields and values */ for (var field in hgetAll) { console.log('Field:'+field); console.log('Value:'+hgetAll[field]); } </script>
HSET (UserCode):1
HSET (UserName):1
HGET:u0001
HGETALL:{user_code=u0001, user_name=Pepe}
Field:user_code
Value:u0001
Field:user_name
Value:Pepe
2.2 Set
SETs hold unique items in an unordered fashion.
Command | Description | Signature | Return |
---|---|---|---|
SADD | Add one or more members to a set. Returns the number of elements that were added to the set, not including all the elements already present into the set. |
sadd(String key, String[] members);
sadd(String key, String member1, String member2, ...); |
long |
SISMEMBER | Determine if a given value is a member of a set. Returns: 1 if the element is a member of the set; 0 if the element is not a member of the set, or if key does not exist. |
sismember(String key, String member); | boolean |
SMEMBERS | Get all the members in a set. Returns all elements of the set. |
smembers(String key); | String[] |
SUNIONSTORE | Add multiple sets and store the resulting set in a key. Returns the number of elements in the resulting set. |
sunionstore(String store_key, String[] sets);
sunionstore(String store_key, String set1, String set2, ...); |
long |
<script> const client = Ax.ext.db.getRedisClient(); /* Create new set 'example_set' and Add member 'Pepe' Add member 'Maria' */ var sadd = client.sadd('example_set', 'Pepe', 'Maria'); /* Check if 'Pepe' is a member of the set Check if 'Quim' is a member of the set */ var sismemberPepe = client.sismember('example_set', 'Pepe'); var sismemberQuim = client.sismember('example_set', 'Quim'); /* Get all members of the set */ var smembers = client.smembers('example_set'); /* Create an auxiliar set 'auxiliar_set' and Compute the union between the two sets Store the result in a third set 'store_set' */ var auxiliarSet = client.sadd('auxiliar_set', 'Quim'); var sunionstore = client.sunionstore('store_set', 'example_set', 'auxiliar_set'); /* Print results */ console.log('SADD:'+sadd); console.log('SISMEMBER (Pepe):'+sismemberPepe); console.log('SISMEMBER (Quim):'+sismemberQuim); console.log('SMEMBERS:'); console.log(smembers) console.log('SUNIONSTORE:'+sunionstore); </script>
SADD:2
SISMEMBER (Pepe):true
SISMEMBER (Quim):false
SMEMBERS:
[Maria, Pepe]
SUNIONSTORE:3
2.3 Sorted Set
ZSETs offer the ability to store a mapping of members to scores (similar to the keys and values of HASHes).
Command | Description | Signature | Return |
---|---|---|---|
ZADD | Add one or more members to a sorted set, or update its score if it already exists. Returns the number of elements added to the sorted set, not including elements already existing for which the score was updated. |
zadd(String key, String score, String member);
zadd(String key, double score, String member); |
long |
ZINCRBY | Increment the score of a member in a sorted set. Returns the new score of member. |
zincrby(String key, double increment, String member); | double |
ZRANGE | Return a range of members in a sorted set, by score, with scores ordered from low to high. Returns list of elements in the specified range. |
zrange(String key, long start, long stop); | String[] |
ZREVRANGEBYSCORE | Return a range of members in a sorted set, by score, with scores ordered from high to low. Returns list of elements in the specified score range |
zrevrangeByScore(String key, double max, double min);
zrevrangeByScore(String key, double max, double min, int offset, int count); |
String[] |
ZREVRANK | Determine the index of a member in a sorted set, with scores ordered from high to low. Returns: the rank of member, if member exists in the sorted set; nil, if member does not exist in the sorted set or key does not exist. |
zrevrank(String key, String member); | long |
ZUNIONSTORE | Add multiple sorted sets and store the resulting sorted set in a new key. Returns the number of elements in the resulting sorted set at destination. |
zunionstore(String store_key, String[] sorted_sets);
zunionstore(String store_key, String sorted_set1, String sorted_set2, ...); |
long |
<script> const client = Ax.ext.db.getRedisClient(); /* Create new sorted set 'example_sorted_set' and Add member 'Pepe' with score 1 Add member 'Maria' with score 1 */ var zaddPepe = client.zadd('example_sorted_set', 1 , 'Pepe'); var zaddMaria = client.zadd('example_sorted_set', 1 , 'Maria'); /* Increment the score of 'Maria' by 2 */ var zincrby = client.zincrby('example_sorted_set', 2 , 'Maria'); /* Get all members of 'example_sorted_set', by score, ordered from low to high by setting stop parameter to -1 */ var zrange = client.zrange('example_sorted_set', 0 , -1); /* Get all members of 'example_sorted_set', by score, ordered from high to low by setting max parameter to 1/0 */ var zrevrangeByScore = client.zrevrangeByScore('example_sorted_set', 1/0 , 0); /* Get the index of 'Pepe' in 'example_sorted_set' with scores ordered from high to low Get the index of 'Maria' in 'example_sorted_set' with scores ordered from high to low */ var zrevrankPepe = client.zrevrank('example_sorted_set', 'Pepe'); var zrevrankMaria = client.zrevrank('example_sorted_set', 'Maria'); /* Create an auxiliar sorted set 'auxiliar_sorted_set' and Compute the union between the two sorted sets Store the result in a third sorted set 'store_sorted_set' */ var auxiliarSortedSet = client.zadd('auxiliar_sorted_set', 5 , 'Quim'); var zunionstore = client.zunionstore('store_sorted_set', 'example_sorted_set', 'auxiliar_sorted_set'); /* Print results */ console.log('ZADD (Pepe):'+zaddPepe); console.log('ZADD (Maria):'+zaddMaria); console.log('ZINCRBY:'+zincrby); console.log('ZRANGE:'); console.log(zrange); console.log('ZREVRANGEBYSCORE:'); console.log(zrevrangeByScore); console.log('ZREVRANK (Pepe):'+zrevrankPepe); console.log('ZREVRANK (Maria):'+zrevrankMaria); console.log('ZUNIONSTORE:'+zunionstore); </script>
ZADD (Pepe):1
ZADD (Maria):1
ZINCRBY:3
ZRANGE:
[Pepe, Maria]
ZREVRANGEBYSCORE:
[Maria, Pepe]
ZREVRANK (Pepe):1
ZREVRANK (Maria):0
ZUNIONSTORE:3
2.4 Delete
Command | Description | Signature | Return |
---|---|---|---|
DEL | Delete a key. Returns the number of keys that were removed. |
del(String[] keys);
del(String key1, String key2, ...); |
long |
<script> const client = Ax.ext.db.getRedisClient(); /* Delete all the keys created in the previous examples. */ var keysToDelete = [ 'example_hash', 'example_set', 'auxiliar_set', 'store_set', 'example_sorted_set', 'auxiliar_sorted_set', 'store_sorted_set' ]; var del = client.del(keysToDelete); /* Print results */ console.log('DEL:'+del); </script>
DEL:7