Skip to content

Script Development / Connector Object DFF.CONN / Redis

The Redis Connector operation object mainly provides Redis operation methods.

The parameters of DFF.CONN(...) are as follows:

Parameter Type Required / Default Description
connector_id str Required Connector ID
database str None Overrides the database number in the Connector configuration
client_name str None Sets the client name for this connection

Redis Cluster does not support selecting a logical database, so cluster connections ignore database.

Message Subscription

For message subscription in the connector, please refer to Script Development / Connector Subscription

.query(...)

Directly call Redis's execute_command(...) to execute commands. The parameters are as follows:

Parameter Type Required / Default Description
command str Required Redis command
*args - - Redis command parameters
**kwargs - - Parameters passed to redis-py
Example
1
2
3
db.query('SET', 'myKey', 'myValue', 'nx')
result = db.query('GET', 'myKey')
# b'myValue'

.query(...) does not convert the return value of redis-py; string results are usually bytes. In actual operations, you can convert them according to the data format:

Python
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
import json

result = db.query('GET', 'intValue')
print(int(result))

result = db.query('GET', 'strValue')
print(result.decode('utf-8'))

result = db.query('GET', 'jsonValue')
print(json.loads(result.decode('utf-8')))

.run(...)

Call client methods by redis-py method names, with the signature .run(command, *args, **kwargs). For example:

Example
1
result = db.run('set', 'myKey', 'myValue', nx=True)

command is a case-sensitive redis-py method name and is usually lowercase. The return value is consistent with the corresponding redis-py method.

Common Convenience Methods

The Connector provides the following convenience methods. Read-type methods recursively decode bytes into UTF-8 strings in their implementations, so the return values may differ from the raw results of .query(...).

Database and Keys

Method Description
.ping() Checks the connection
.info() Gets Redis information
.dbsize() Gets the number of keys in the current database
.type(key) Gets the key type; returns None if the key does not exist
.memory_usage(key, samples=0) Gets the memory occupied by a key
.keys(pattern='*', limit=None) Uses SCAN to get matching keys; the number of returned keys can be limited
.exists(key) Checks whether a key exists and returns bool
.expire(key, expires) Sets expiration time in seconds
.expireat(key, timestamp) Sets an absolute expiration time
.ttl(key) / .pttl(key) Gets remaining time in seconds / milliseconds
.rename(key, new_key) Renames a key
.renamenx(key, new_key) Renames only if the new key does not exist
.delete(keys) Deletes a key or a list of keys and returns the number deleted

Strings and Hashes

Method Description
.set(key, value, expires=None, not_exists=False, exists=False, get_old_value=False) Sets a value, and can set expiration time and NX, XX, GET options
.mset(key_values) Sets multiple key-value pairs from a dictionary
.get(key) Gets and decodes a single value
.mget(keys) Gets multiple values and returns {key: value}
.getset(key, value) Sets a new value and returns the old value
.incr(key) / .incrby(key, step=1) / .incrbyfloat(key, step=1) Increments a numeric value
.hkeys(key, pattern='*', with_values=False) Uses HSCAN to get fields; can also return values
.hset(key, field=None, value=None, field_values=None) Sets a single field or a field mapping
.hmset(key, field_values) Sets multiple fields
.hsetnx(key, field, value) Sets only if the field does not exist
.hget(key, field) / .hgetall(key) Gets and decodes field values
.hmget(key, fields) Gets multiple and returns {field: value}
.hstrlen(key, field) / .hstrlenall(key) Gets the byte length of one or all field values
.hincr(key, field) / .hincrby(key, field, step=1) / .hincrbyfloat(key, field, step=1) Increments a field's numeric value
.hdel(key, field) Deletes a field or a list of fields

Lists, Sets, and Sorted Sets

Method Description
.lpush(key, value) / .rpush(key, value) Pushes a value or a list of values from the left / right
.lpop(key, count=None) / .rpop(key, count=None) Pops one or more values from the left / right
.blpop(key, timeout=0) / .brpop(key, timeout=0) Blocking pop
.rpoplpush(key, dest_key=None) Pops and pushes to another list
.brpoplpush(key, dest_key=None, timeout=0) Blocking pop and push
.llen(key) / .lrange(key, start=0, stop=-1) Gets list length / range
.ltrim(key, start, stop) Trims the list
.push(...) / .pop(...) / .bpop(...) Aliases for .lpush(...) / .rpop(...) / .brpop(...)
.sadd(key, member) / .srem(key, member) Adds / removes a member or a list of members
.scard(key) / .smembers(key) / .sismember(key, member) Gets the set count, members, or checks membership
.zadd(key, member_scores) / .zrem(key, member) Adds / removes sorted set members
.zcard(key) Gets the number of sorted set members
.zrange(key, start=0, stop=-1, with_scores=False) Gets members by position
.zrangebyscore(key, min_score='-inf', max_score='+inf', with_scores=False) Gets members by score

.publish(...)

Publishes a message to a Redis channel and returns the number of subscribers that received the message. .pub(...) is its alias.

Example
1
subscriber_count = db.publish('events', 'hello')

Extended Methods

Method Description
.eval(lua_script, key_count, *args) Executes a Lua Script and decodes the return value
.push_limit(key, value, limit) Pushes from the left and limits the list to the specified length
.get_pattern(pattern) Gets matching key-value pairs, returning {key: value}
.delete_pattern(pattern) Deletes all matching keys
.hget_pattern(key, pattern) Gets matching hash fields and values
.lock(lock_key, lock_value, max_lock_time) Acquires a lock using SET EX NX
.extend_lock_time(lock_key, lock_value, max_lock_time) Extends the expiration time after verifying the lock holder
.unlock(lock_key, lock_value) Releases the lock only when the lock value matches