Redis Service

Redis Service

Classname: be.securit.trustbuilder.service.RedisService

Requirements

To use this service you must install to following jar in your application servers library. For TrustBuilder appliance this location is /opt/trustbuilder/tomcat-core/lib/ext

Property

  • host Set the hostname of the redis server (only for single node)(this is for backwards compatibility and will be removed in the next major release)
  • timeOutInSec Default time to live for a key that gets added in seconds.
  • master Name of the master node.
  • sentinels ";" - separated list with addresses of the sentinels.
  • password Optional password for the redis cluster.

Invoking Redis in the scripts

Since the Redis Service is a wrapper around the jedis calls we have to threat the function somewhat different than other services. The Redis Service is using a Pool of connections with tha Apache Commons pooling library.

Creating a pool :

var redispool = tb.getService("Redis");

Invoke the pool :

redispool.invoke(function(jedis){
    // Your redis commands are place here. The jedis name can be anything you want
    but you need it to call the commands
    jedis.set("foo", "bar");
});

This wil set a key "foo" with value "bar" in the Redis Server.

Functions

Since redis has a lot of functions we will only explain the basics. Most functions are derived from the original redis commands fount on the Redis website.

Basic Functions

set(key,data)

Set key to hold the string value. If key already holds a value, it is overwritten, regardless of its type. Any previous time to live associated with the key is discarded on successful SET operation.

jedis.set("foo", "bar");

get(key)

Get the value of key. If the key does not exist the special value nil is returned. An error is returned if the value stored at key is not a string, because GET only handles string values.

var data = jedis.get("foo");   // bar 

incr(key) incrBy(key, increment)

Increments the number stored at key by one. If the key does not exist, it is set to 0 before performing the operation. An error is returned if the key contains a value of the wrong type or contains a string that can not be represented as integer. This operation is limited to 64 bit signed integers.

Note: this is a string operation because Redis does not have a dedicated integer type. The string stored at the key is interpreted as a base-10 64 bit signed integer to execute the operation.

Redis stores integers in their integer representation, so for string values that actually hold an integer, there is no overhead for storing the string representation of the integer.

jedis.incr("foo");  // increment 1 
jedis.incrBy("foo", 10);  // increment by 10

decr(key) decrBy(key)

Decrements the number stored at key by one. If the key does not exist, it is set to 0 before performing the operation. An error is returned if the key contains a value of the wrong type or contains a string that can not be represented as integer. This operation is limited to 64 bit signed integers.

jedis.decr("foo");
jedis.decrBy("foo", 10);

Expire(key, seconds)

Set a timeout on key. After the timeout has expired, the key will automatically be deleted. A key with an associated timeout is often said to be volatile in Redis terminology. The timeout is cleared only when the key is removed using the DEL command or overwritten using the SET or GETSET commands. This means that all the operations that conceptually alter the value stored at the key without replacing it with a new one will leave the timeout untouched. For instance, incrementing the value of a key with INCR, pushing a new value into a list with LPUSH, or altering the field value of a hash with HSET are all operations that will leave the timeout untouched. The timeout can also be cleared, turning the key back into a persistent key, using the PERSIST command.

Refreshing expires

It is possible to call EXPIRE using as argument a key that already has an existing expire set. In this case the time to live of a key is updated to the new value. There are many useful applications for this, an example is documented in the Navigation session pattern section below.

jedis.expire("foo", 300);   // Expire foo in 5 minutes

expireAt(key, timestamp)

EXPIREAT has the same effect and semantic as EXPIRE, but instead of specifying the number of seconds representing the TTL (time to live), it takes an absolute Unix timestamp (seconds since January 1, 1970).

Please for the specific semantics of the command refer to the documentation of EXPIRE. Background

EXPIREAT was introduced in order to convert relative timeouts to absolute timeouts for the AOF persistence mode. Of course, it can be used directly to specify that a given key should expire at a given time in the future.

jedis.expireAt("foo", "1414500547"");

persist(key)

Remove the existing timeout on key, turning the key from volatile (a key with an expire set) to persistent (a key that will never expire as no timeout is associated).

jedis.persist("foo");   // persist the foo key

setbit(key, bit, boolean)

Sets or clears the bit at offset in the string value stored at key. The bit is either set or cleared depending on value, which can be either 0 or 1. When key does not exist, a new string value is created. The string is grown to make sure it can hold a bit at offset. The offset argument is required to be greater than or equal to 0, and smaller than 232 (this limits bitmaps to 512MB). When the string at key is grown, added bits are set to 0.

jedis.set("foo", "0");  // Set foo to 0
jedis.setbit("foo", 3, true);
jedis.setbit("foo", 7, true);

/*
 * byte: 0 1 2 3 4 bit: 00010001 
 */

getbit(key, bit)

Returns the bit value at offset in the string value stored at key. When offset is beyond the string length, the string is assumed to be a contiguous space with 0 bits. When key does not exist it is assumed to be an empty string, so offset is always out of range and the value is also assumed to be a contiguous space with 0 bits.

jedis.getbit("foo",3);  // true
jedis.getbit("foo",5);  // false
Hashes

hset(key,field,data)

Sets field in the hash stored at key to value. If key does not exist, a new key holding a hash is created. If field already exists in the hash, it is overwritten.

jedis.hset("foo", "bar", "car");

hsetnx(key,field,data)

Sets field in the hash stored at key to value, only if field does not yet exist. If key does not exist, a new key holding a hash is created. If field already exists, this operation has no effect.

jedis.hsetnx("foo", "bar", "car");

hget(key,field)

Returns the value associated with field in the hash stored at key.

jedis.hget("foo", "bar");    // car 

hmset(key, key/value object)

Sets the specified fields to their respective values in the hash stored at key. This command overwrites any existing fields in the hash. If key does not exist, a new key holding a hash is created.

jedis.hmset("foo", { bar : "car", user : "John");

hgetAll(key)

Returns all fields and values of the hash stored at key. In the returned value, every field name is followed by its value, so the length of the reply is twice the size of the hash.

var data = jedis.hgetall("foo");   
 var user = data.get("user");      // John 

hmget(key, field, [field])

Returns the values associated with the specified fields in the hash stored at key. For every field that does not exist in the hash, a nil value is returned. Because a non-existing keys are treated as empty hashes, running HMGET against a non-existing key will return a list of nil values.

var data = jedis.hmget("foo", "bar", "user");
var user = data.get("user");      // John 

hexists(key, field)

Returns if field is an existing field in the hash stored at key.

var dataexists = jedis.hexists("bar", "foo");  // true 
var datanotexists = jedis.hexists("bar", "example");  // false

hdel(key, field, [field])

Removes the specified fields from the hash stored at key. Specified fields that do not exist within this hash are ignored. If key does not exist, it is treated as an empty hash and this command returns 0.

Remark : to remove a complete hash you have to use the delete command (del)

// deleting a field 
jedis.hdel("foo", "bar"); 

// deleting a complete hash
jedis.del("foo");

hlen(key)

Returns the number of fields contained in the hash stored at key.

var length = jedis.hlen("foo");    // 2

Expiring hashes

Only a full hash can be set to expire, not the fields on their own.

Was this article helpful?
0 out of 0 found this helpful
Have more questions? Submit a request

Comments

0 comments

Please sign in to leave a comment.