Skip to content

Script Development / Simple Cache DFF.CACHE

DataFlux Func includes a built-in simple cache functionality DFF.CACHE based on Redis. For scenarios requiring data caching with relatively simple needs, this built-in cache can be used directly.

The storage follows a Scope-Key-Value structure. Keys with the same name are allowed under different namespaces.

Simple Cache Does Not Preserve Original Data Types

When operating the simple cache, regardless of whether a string or a number is written in the code, the value read back will always be a string.

Users need to handle the read content themselves, performing appropriate type conversion or deserialization.

General Methods

Methods related to the cache database itself and independent of 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 matching 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 a cache. Parameters are as follows:

Parameter Type Required / Default Description
key str Required Key name
expire int None Expiration duration.
Unit: seconds
None means never expires
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 a 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 (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 matching pattern. Parameters are as follows:

Parameter Type Required / Default Description
pattern str "*" Key name matching 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 for operating on values written via the DFF.CACHE.set(...) method.

DFF.CACHE.set(...)

Set 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 never expires
not_exists bool False Write only if the key does not exist
exists bool False Write only if the key does exist
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 set 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 list Required List of key names
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 matching pattern. Parameters are as follows:

Parameter Type Required / Default Description
pattern str "*" Key name matching 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 simultaneously 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 hash structures allows storing caches of the same type under the same Key, reducing the number of Keys in the main cache space.

DFF.CACHE.hkeys(...)

Get the 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 matching 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 the value of a specific field 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 the value of a field in 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 in a hash structure. Parameters are as follows:

Parameter Type Required / Default Description
key str Required Key name
fields list Required List of field names
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 in 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 the value of a field 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 the value of a field 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 in 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 list structures allows storing multiple caches sequentially under the same Key, often used to implement queues, stacks, etc.

DFF.CACHE.lpush(...)

Add an element to the list structure from the left side. 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 list structure from the right side. 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 list structure from the left side. 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 list structure from the right side. 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 list structure from the left side in a blocking manner. Parameters are as follows:

Parameter Type Required / Default Description
key str / list Required Key name / List of key names
timeout int 0 Blocking timeout duration (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 list structure from the right side in a blocking manner. Parameters are as follows:

Parameter Type Required / Default Description
key str / list Required Key name / List of key names
timeout int 0 Blocking timeout duration (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 one list structure, simultaneously push it to the left side of another list structure, and return this 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 Rotation
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 one list structure, simultaneously push it to the left side of another list structure in a blocking manner, and return this 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 within 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(...)

Trim the list structure, keeping elements starting 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 (Circular)
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 directions.

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 set structures allows storing unique 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

Remove 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 the 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
member str Required Member
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 sorted set structures allows storing unique members and maintaining order based on 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

Remove 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 based on 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 output in reverse order
with_scores boolean False Whether to return scores simultaneously
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 based on 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 simultaneously
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.zrangebyscore('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]]

Pub/Sub Methods

Using Pub/Sub methods allows sending 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')