Skip to content

Script Development / Simple Cache DFF.CACHE

DataFlux Func has a built-in simple cache feature DFF.CACHE based on Redis. For scenarios that require data caching but are not complex, this built-in cache feature can be used directly.

The storage structure is Scope-Key-Value, allowing the same Key to exist under different namespaces.

Simple Cache does not preserve original data types

When operating the simple cache, whether you write a string or a number in the code, it will be read back as a string.

Users need to handle the read content themselves and perform appropriate type conversion or deserialization.

General Methods

Some methods related to the cache database itself and unrelated to specific Key types.

DFF.CACHE.ping()

Added in version 6.0.6

Ping to check the cache database response.

Example
1
2
DFF.CACHE.ping()
# True

DFF.CACHE.info()

Added in version 6.0.6

Get cache database information.

Example
1
DFF.CACHE.info()

DFF.CACHE.dbsize()

Added in version 6.0.6

Get the number of Keys in the cache database.

Example
1
2
DFF.CACHE.dbsize()
# 10

DFF.CACHE.type(...)

Added in version 6.0.6

Get the type of a Key, parameters are as follows:

Parameter Type Required / Default Description
key str Required Key name
scope str Current script name Namespace
Example
1
2
3
DFF.CACHE.set('my_key', 100, scope='my_scope')
DFF.CACHE.type('my_key', scope='my_scope')
# 'string'

DFF.CACHE.keys(...)

Added in version 6.0.6

Get a list of Keys, parameters are as follows:

Parameter Type Required / Default Description
pattern str "*" Key name pattern
Supports * wildcard
scope str Current script name Namespace
Example
1
2
DFF.CACHE.keys('*', scope='my_scope')
# ['my_key']

DFF.CACHE.exists(...)

Added in version 6.0.6

Check if a Key exists, parameters are as follows:

Parameter Type Required / Default Description
key str Required Key name
scope str Current script name Namespace
Example
1
2
DFF.CACHE.exists('my_key', scope='my_scope')
# True

DFF.CACHE.expire(...)

Set the expiration time for the cache, parameters are as follows:

Parameter Type Required / Default Description
key str Required Key name
expire int None Expiration time.
Unit: seconds
None means no expiration
scope str Current script name Namespace
Example
1
DFF.CACHE.expire('my_key', 3600, scope='my_scope')

DFF.CACHE.expireat(...)

Added in version 6.0.6

Set the expiration timestamp for the cache, parameters are as follows:

Parameter Type Required / Default Description
key str Required Key name
timestamp int Required UNIX timestamp (unit: seconds)
scope str Current script name Namespace
Example
1
DFF.CACHE.expireat('my_key', int(time.time() + 3600), scope='my_scope')

DFF.CACHE.ttl(...)

Added in version 6.0.6

Get the remaining expiration time (in seconds) for a Key, parameters are as follows:

Parameter Type Required / Default Description
key str Required Key name
scope str Current script name Namespace
Example
1
2
DFF.CACHE.ttl('my_key', scope='my_scope')
# 3599

DFF.CACHE.delete(...)

Delete stored data, parameters are as follows:

Parameter Type Required / Default Description
key str Required Key name
scope str Current script name Namespace
Example
1
DFF.CACHE.delete('my_key', scope='my_scope')

DFF.CACHE.delete_pattern(...)

Delete stored data based on a Key name pattern, parameters are as follows:

Parameter Type Required / Default Description
pattern str "*" Key name pattern
Supports * wildcard
scope str Current script name Namespace
Example
1
2
3
4
DFF.CACHE.set('my_key', 100, scope='my_scope')
DFF.CACHE.set('my_key2', 200, scope='my_scope')

DFF.CACHE.delete_pattern('my*', scope='my_scope')

String Methods

Methods related to values written via DFF.CACHE.set(...).

DFF.CACHE.set(...)

Create a cache, parameters are as follows:

Parameter Type Required / Default Description
key str Required Key name
value str / int / float Required Data
expires / expire int None Expiration time.
Unit: seconds
None means no expiration
not_exists bool False Write only if the Key does not exist
exists bool False Write only if the Key exists
scope str Current script name Namespace
Example
1
DFF.CACHE.set('my_key', 100, scope='my_scope')

DFF.CACHE.mset(...)

Added in version 6.0.6

Batch create cache, parameters are as follows:

Parameter Type Required / Default Description
key_values dict Required Key-value dictionary
scope str Current script name Namespace
Example
1
2
3
4
5
data = {
  'my_key' : 100,
  'my_key2': 200,
}
DFF.CACHE.mset(data, scope='my_scope')

DFF.CACHE.get(...)

Get cache, parameters are as follows:

Parameter Type Required / Default Description
key str Required Key name
scope str Current script name Namespace
Example
1
2
3
4
DFF.CACHE.set('my_key', 100, scope='my_scope')

DFF.CACHE.get('my_key', scope='my_scope')
# '100'

DFF.CACHE.mget(...)

Added in version 6.0.6

Batch get cache, parameters are as follows:

Parameter Type Required / Default Description
keys str Required Key name list
scope str Current script name Namespace
Example
1
2
3
4
5
DFF.CACHE.set('my_key', 100, scope='my_scope')
DFF.CACHE.set('my_key2', 200, scope='my_scope')

DFF.CACHE.mget([ 'my_key', 'my_key2' ], scope='my_scope')
# {'my_key': '100', 'my_key2': '200'}

DFF.CACHE.get_pattern(...)

Added in version 6.0.6

Get cache based on a Key name pattern, parameters are as follows:

Parameter Type Required / Default Description
pattern str "*" Key name pattern
Supports * wildcard
scope str Current script name Namespace
Example
1
2
3
4
5
DFF.CACHE.set('my_key', 100, scope='my_scope')
DFF.CACHE.set('my_key2', 200, scope='my_scope')

DFF.CACHE.get_pattern('my_*', scope='my_scope')
# {'my_key2': '200', 'my_key': '100'}

DFF.CACHE.getset(...)

Get cache and set a new cache value, parameters are as follows:

Parameter Type Required / Default Description
key str Required Key name
value str / int / float Required Data
scope str Current script name Namespace
Example
1
2
3
4
DFF.CACHE.set('my_key', 100, scope='my_scope')

DFF.CACHE.getset('my_key', 200, scope='my_scope')
# '100'

DFF.CACHE.incr(...)

Increment the cache value by a step, parameters are as follows:

Parameter Type Required / Default Description
key str Required Key name
step int 1 Step value
scope str Current script name Namespace
Example
1
DFF.CACHE.incr('my_key', scope='my_scope')

DFF.CACHE.incrby(...)

Increment the cache value by a specified step, parameters are as follows:

Parameter Type Required / Default Description
key str Required Key name
step int Required Step value
scope str Current script name Namespace
Example
1
DFF.CACHE.incrby('my_key', step=2, scope='my_scope')

Hash Methods

Using the hash structure, you can store caches of the same type under the same Key, reducing the number of Keys in the main cache space.

DFF.CACHE.hkeys(...)

Get a list of fields in a hash structure, parameters are as follows:

Parameter Type Required / Default Description
key str Required Key name
pattern str "*" Field name pattern
Supports * wildcard
scope str Current script name Namespace
Example
1
2
3
4
DFF.CACHE.hmset('user:001', { 'name': 'Tom', 'city': 'Beijing' }, scope='my_scope')

DFF.CACHE.hkeys('user:001', scope='userCache')
# ['name', 'city']

DFF.CACHE.hset(...)

Set a field value in a hash structure, parameters are as follows:

Parameter Type Required / Default Description
key str Required Key name
field str Required Field name
value str / int / float Required Data
not_exists bool False Write only if the field does not exist
scope str Current script name Namespace
Example
1
DFF.CACHE.hset('user:001', 'name', 'Tom', scope='my_scope')

DFF.CACHE.hmset(...)

Set multiple field values in a hash structure, parameters are as follows:

Parameter Type Required / Default Description
key str Required Key name
obj dict Required Data
scope str Current script name Namespace
Example
1
DFF.CACHE.hmset('user:001', { 'name': 'Tom', 'city': 'Beijing' }, scope='my_scope')

DFF.CACHE.hget(...)

Get a field value from a hash structure, parameters are as follows:

Parameter Type Required / Default Description
key str Required Key name
field str Required Field name
scope str Current script name Namespace
Example
1
2
3
4
DFF.CACHE.hmset('user:001', { 'name': 'Tom', 'city': 'Beijing', 'age': 20 }, scope='my_scope')

DFF.CACHE.hget('user:001', 'name', scope='my_scope')
# 'Tom'

DFF.CACHE.hmget(...)

Added in version 6.0.6

Get multiple field values from a hash structure, parameters are as follows:

Parameter Type Required / Default Description
key str Required Key name
fields list Required Field name list
scope str Current script name Namespace
Example
1
2
3
4
DFF.CACHE.hmset('user:001', { 'name': 'Tom', 'city': 'Beijing', 'age': 20 }, scope='my_scope')

DFF.CACHE.hmget('user:001', ['name', 'city'], scope='my_scope')
# {'name': 'Tom', 'city': 'Beijing'}

DFF.CACHE.hgetall(...)

Added in version 6.0.6

Get all field values from a hash structure, parameters are as follows:

Parameter Type Required / Default Description
key str Required Key name
scope str Current script name Namespace
Example
1
2
3
4
DFF.CACHE.hmset('user:001', { 'name': 'Tom', 'city': 'Beijing', 'age': 20 }, scope='my_scope')

DFF.CACHE.hgetall('user:001', scope='my_scope')
# {'age': '20', 'city': 'Beijing', 'name': 'Tom'}

DFF.CACHE.hincr(...)

Added in version 6.0.6

Increment a field value in a hash structure by a step, parameters are as follows:

Parameter Type Required / Default Description
key str Required Key name
field str Required Field name
step int 1 Step value
scope str Current script name Namespace
Example
1
DFF.CACHE.hincr('user:001', 'signCount', scope='my_scope')

DFF.CACHE.hincrby(...)

Added in version 6.0.6

Increment a field value in a hash structure by a specified step, parameters are as follows:

Parameter Type Required / Default Description
key str Required Key name
field str Required Field name
step int Required Step value
scope str Current script name Namespace
Example
1
DFF.CACHE.hincrby('user:001', 'signCount', step=2, scope='my_scope')

DFF.CACHE.hdel(...)

Delete a field from a hash structure, parameters are as follows:

Parameter Type Required / Default Description
key str Required Key name
field str Required Field name
scope str Current script name Namespace
Example
1
2
3
DFF.CACHE.hmset('user:001', { 'name': 'Tom', 'city': 'Beijing' }, scope='my_scope')

DFF.CACHE.hdel('user:001', 'city', scope='my_scope')

List Methods

Using the list structure, you can store multiple caches in order under the same Key, often used to implement queues, stacks, etc.

DFF.CACHE.lpush(...)

Add an element to the left side of a list structure, parameters are as follows:

Parameter Type Required / Default Description
key str Required Key name
value str / int / float Required Data
scope str Current script name Namespace
Example
1
2
DFF.CACHE.lpush('userQueue', '001', scope='queue')
DFF.CACHE.lpush('userQueue', '002', scope='queue')

DFF.CACHE.rpush(...)

Add an element to the right side of a list structure, parameters are as follows:

Parameter Type Required / Default Description
key str Required Key name
value str / int / float Required Data
scope str Current script name Namespace
Example
1
2
DFF.CACHE.rpush('userQueue', '001', scope='queue')
DFF.CACHE.rpush('userQueue', '002', scope='queue')

DFF.CACHE.lpop(...)

Pop an element from the left side of a list structure, parameters are as follows:

Parameter Type Required / Default Description
key str Required Key name
scope str Current script name Namespace
Example
1
2
3
4
5
DFF.CACHE.lpush('userQueue', '001', scope='queue')
DFF.CACHE.lpush('userQueue', '002', scope='queue')

DFF.CACHE.lpop('userQueue', scope='queue')
# '002'

DFF.CACHE.rpop(...)

Pop an element from the right side of a list structure, parameters are as follows:

Parameter Type Required / Default Description
key str Required Key name
scope str Current script name Namespace
Example
1
2
3
4
5
DFF.CACHE.lpush('userQueue', '001', scope='queue')
DFF.CACHE.lpush('userQueue', '002', scope='queue')

DFF.CACHE.rpop('userQueue', scope='queue')
# '001'

DFF.CACHE.blpop(...)

Added in version 6.0.6

Pop an element from the left side of a list structure in a blocking manner, parameters are as follows:

Parameter Type Required / Default Description
key str / list Required Key name / Key name list
timeout int 0 Blocking timeout (seconds)
scope str Current script name Namespace
Example
1
2
3
4
5
DFF.CACHE.lpush('userQueue', '001', scope='queue')
DFF.CACHE.lpush('userQueue', '002', scope='queue')

DFF.CACHE.blpop('userQueue', timeout=3, scope='queue')
# ['userQueue', '002']

DFF.CACHE.brpop(...)

Added in version 6.0.6

Pop an element from the right side of a list structure in a blocking manner, parameters are as follows:

Parameter Type Required / Default Description
key str / list Required Key name / Key name list
timeout int 0 Blocking timeout (seconds)
scope str Current script name Namespace
Example
1
2
3
4
5
DFF.CACHE.lpush('userQueue', '001', scope='queue')
DFF.CACHE.lpush('userQueue', '002', scope='queue')

DFF.CACHE.brpop('userQueue', timeout=3, scope='queue')
# ['userQueue', '001']

DFF.CACHE.rpoplpush(...)

Pop an element from the right side of a list structure and push it to the left side of another list structure, and return the element, parameters are as follows:

Parameter Type Required / Default Description
key str Required Key name (source)
dest_key str Same as key Key name (destination)
scope str Current script name Namespace (source)
dest_scope str Same as scope Namespace (destination)
Example
1
2
3
4
5
6
DFF.CACHE.lpush('userQueue', '001', scope='queue')
DFF.CACHE.lpush('userQueue', '002', scope='queue')
DFF.CACHE.lpush('userQueue', '003', scope='queue')

DFF.CACHE.rpoplpush('userQueue', 'userQueue2', scope='queue')
# '001'
Tip: Queue Rolling
1
2
3
4
5
6
7
8
9
DFF.CACHE.lpush('userQueue', '001', scope='queue')
DFF.CACHE.lpush('userQueue', '002', scope='queue')
DFF.CACHE.lpush('userQueue', '003', scope='queue')

DFF.CACHE.rpoplpush('userQueue', scope='queue')
# '001'

DFF.CACHE.rpoplpush('userQueue', scope='queue')
# '002'

DFF.CACHE.brpoplpush(...)

Added in version 6.0.6

Pop an element from the right side of a list structure in a blocking manner and push it to the left side of another list structure, and return the element, parameters are as follows:

Parameter Type Required / Default Description
key str Required Key name (source)
dest_key str Same as key Key name (destination)
scope str Current script name Namespace (source)
dest_scope str Same as scope Namespace (destination)
Example
1
2
3
4
5
6
DFF.CACHE.lpush('userQueue', '001', scope='queue')
DFF.CACHE.lpush('userQueue', '002', scope='queue')
DFF.CACHE.lpush('userQueue', '003', scope='queue')

DFF.CACHE.brpoplpush('userQueue', 'userQueue2', timeout=3, scope='queue')
# '001'

DFF.CACHE.llen(...)

Get the number of elements in a list structure, parameters are as follows:

Parameter Type Required / Default Description
key str Required Key name
scope str Current script name Namespace
Example
1
2
3
4
5
DFF.CACHE.lpush('userQueue', '001', scope='queue')
DFF.CACHE.lpush('userQueue', '002', scope='queue')

DFF.CACHE.llen('userQueue', scope='queue')
# 2

DFF.CACHE.lrange(...)

Get a list of elements in a list structure (without popping), parameters are as follows:

Parameter Type Required / Default Description
key str Required Key name
start int 0 Start index (inclusive)
stop int -1 End index (inclusive, -1 means the last one)
scope str Current script name Namespace
Example
1
2
3
4
5
6
7
8
9
DFF.CACHE.rpush('userQueue', '001', scope='queue')
DFF.CACHE.rpush('userQueue', '002', scope='queue')
DFF.CACHE.rpush('userQueue', '003', scope='queue')
DFF.CACHE.rpush('userQueue', '004', scope='queue')

DFF.CACHE.lrange('userQueue', 0, 1, scope='queue')
# [ '001', '002' ]
DFF.CACHE.lrange('userQueue', 0, -1, scope='queue')
# [ '001', '002', '003', '004' ]

DFF.CACHE.ltrim(...)

Keep elements in a list structure from the left side, parameters are as follows:

Parameter Type Required / Default Description
key str Required Key name
start int Required Start index (inclusive)
stop int Required End index (inclusive, -1 means the last one)
scope str Current script name Namespace
Example
1
2
3
4
5
6
DFF.CACHE.rpush('userQueue', '001', scope='queue')
DFF.CACHE.rpush('userQueue', '002', scope='queue')
DFF.CACHE.rpush('userQueue', '003', scope='queue')
DFF.CACHE.rpush('userQueue', '004', scope='queue')

DFF.CACHE.ltrim('userQueue', 0, 1, scope='queue')
Tip: Limit Queue Length (Rolling)
1
2
3
4
limit = 3
for i in range(100):
    DFF.CACHE.lpush('userQueue', i, scope='queue')
    DFF.CACHE.ltrim('userQueue', 0, limit, scope='queue')

Alias Methods

Added in version 6.0.6

Since PUSH / POP operations on list structures support both left-to-right and right-to-left.

To avoid confusion and ensure consistent operation direction, the following alias methods can be used.

Alias Method Corresponding Actual Method
push lpush
pop rpop
bpop brpop

Set Methods

Using the set structure, you can save non-repeating members.

DFF.CACHE.sadd(...)

Added in version 6.0.6

Add a member, parameters are as follows:

Parameter Type Required / Default Description
key str Required Key name
member str Required Member
scope str Current script name Namespace
Example
1
DFF.CACHE.sadd('staredUsers', 'user-001', scope='set')

DFF.CACHE.srem(...)

Added in version 6.0.6

Delete a member, parameters are as follows:

Parameter Type Required / Default Description
key str Required Key name
member str Required Member
scope str Current script name Namespace
Example
1
DFF.CACHE.srem('staredUsers', 'user-001', scope='set')

DFF.CACHE.scard(...)

Added in version 6.0.6

Get the number of members, parameters are as follows:

Parameter Type Required / Default Description
key str Required Key name
scope str Current script name Namespace
Example
1
2
DFF.CACHE.scard('staredUsers', scope='set')
# 1

DFF.CACHE.smembers(...)

Added in version 6.0.6

Get a list of members, parameters are as follows:

Parameter Type Required / Default Description
key str Required Key name
scope str Current script name Namespace
Example
1
2
3
4
5
DFF.CACHE.sadd('staredUsers', 'user-001', scope='set')
DFF.CACHE.sadd('staredUsers', 'user-002', scope='set')

DFF.CACHE.smembers('staredUsers', scope='set')
# ['user-002', 'user-001']

DFF.CACHE.sismember(...)

Added in version 6.0.6

Check if a member is in the set, parameters are as follows:

Parameter Type Required / Default Description
key str Required Key name
scope str Current script name Namespace
Example
1
2
3
4
5
DFF.CACHE.sadd('staredUsers', 'user-001', scope='set')
DFF.CACHE.sadd('staredUsers', 'user-002', scope='set')

DFF.CACHE.sismember('staredUsers', 'user-002', scope='set')
# True

Sorted Set Methods

Using the sorted set structure, you can save non-repeating members and keep them ordered by a specified score (score, int / float type).

DFF.CACHE.zadd(...)

Added in version 6.0.6

Add a member, parameters are as follows:

Parameter Type Required / Default Description
key str Required Key name
member_scores dict Required Member-score dictionary
scope str Current script name Namespace
Example
1
2
3
4
5
data = {
  'user-001': 100,
  'user-002': 200,
}
DFF.CACHE.zadd('staredUsers', data, scope='zset')

DFF.CACHE.zrem(...)

Added in version 6.0.6

Delete a member, parameters are as follows:

Parameter Type Required / Default Description
key str Required Key name
member str Required Member
scope str Current script name Namespace
Example
1
DFF.CACHE.zrem('staredUsers', 'user-001', scope='zset')

DFF.CACHE.zcard(...)

Added in version 6.0.6

Get the number of members, parameters are as follows:

Parameter Type Required / Default Description
key str Required Key name
scope str Current script name Namespace
Example
1
2
DFF.CACHE.zcard('staredUsers', scope='zset')
# 1

DFF.CACHE.zrange(...)

Added in version 6.0.6

Get members by Score order index, parameters are as follows:

Parameter Type Required / Default Description
key str Required Key name
start int 0 Start index (inclusive)
stop int -1 End index (inclusive, -1 means the last one)
reverse boolean False Whether to reverse output
with_scores boolean False Whether to return scores
scope str Current script name Namespace
Example
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
data = {
  'user-001': 100,
  'user-002': 200,
  'user-003': 300,
}
DFF.CACHE.zadd('staredUsers', data, scope='zset')

DFF.CACHE.zrange('staredUsers', scope='zset')
# ['user-001', 'user-002', 'user-003']
DFF.CACHE.zrange('staredUsers', start=1, stop=-1, scope='zset')
# ['user-002', 'user-003']
DFF.CACHE.zrange('staredUsers', with_scores=True, scope='zset')
# [['user-001', 100.0], ['user-002', 200.0], ['user-003', 300.0]]

DFF.CACHE.zrangebyscore(...)

Added in version 6.0.6

Get members by Score, parameters are as follows:

Parameter Type Required / Default Description
key str Required Key name
min_score int "-inf" Start Score (inclusive, "-inf" means minimum value)
max_score int "+inf" End Score (inclusive, "+inf" means maximum value)
with_scores boolean False Whether to return scores
scope str Current script name Namespace
Example
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
data = {
  'user-001': 100,
  'user-002': 200,
  'user-003': 300,
}
DFF.CACHE.zadd('staredUsers', data, scope='zset')

DFF.CACHE.zrangebyscore('staredUsers', scope='zset')
# ['user-001', 'user-002', 'user-003']
DFF.CACHE.zrangebysscore('staredUsers', min_score=200, max_score=200, scope='zset')
# ['user-002']
DFF.CACHE.zrangebyscore('staredUsers', with_scores=True, scope='zset')
# [['user-001', 100.0], ['user-002', 200.0], ['user-003', 300.0]]

Message Methods

Using message methods, you can send messages to clients subscribed to specific topics.

DFF.CACHE.publish(...)

Publish a message to a topic, parameters are as follows:

Parameter Type Required / Default Description
topic str Required Topic
message str Required Message content
scope str Current script name Namespace
Example
1
DFF.CACHE.publish('some_topic', 'hello', scope='app')