Skip to content

Script Development / Simple Cache DFF.CACHE

DataFlux Func includes a simple caching feature DFF.CACHE based on Redis. For scenarios where there is a need for data caching but the requirements are not complex, this built-in caching feature can be used directly.

The storage function follows a Scope-Key-Value structure, allowing identical Keys to exist under different namespaces.

Simple cache will not maintain original data types

When operating with the simple cache, regardless of whether strings or numbers are written in the code, they will all be read back as strings.

Users need to handle the retrieved content themselves and perform appropriate type conversions or deserialization.

General Class

Some methods related to the cache database itself, as well as those unrelated to specific Key types

DFF.CACHE.ping()

Added in version 6.0.6

Ping test to check cache database response

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

DFF.CACHE.info()

Added in version 6.0.6

Retrieve information about the cache database

Example
1
DFF.CACHE.info()

DFF.CACHE.dbsize()

Added in version 6.0.6

Retrieve the number of Keys in the cache database

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

DFF.CACHE.type(...)

Added in version 6.0.6

Retrieve the type of a Key, parameters as follows:

Parameter Type Required / Default Value 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

Retrieve a list of Keys, parameters as follows:

Parameter Type Required / Default Value 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 as follows:

Parameter Type Required / Default Value 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 duration for the cache, parameters as follows:

Parameter Type Required / Default Value Description
key str Required Key name
expire int None Expiration duration.
Unit: seconds
None indicates 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 time point for the cache, parameters as follows:

Parameter Type Required / Default Value 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

Retrieve the remaining expiration time (seconds) for a key, parameters as follows:

Parameter Type Required / Default Value 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 as follows:

Parameter Type Required / Default Value 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 key name matching patterns, parameters as follows:

Parameter Type Required / Default Value 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 Class (String)

Methods for operations on values written using the DFF.CACHE.set(...) method

DFF.CACHE.set(...)

Create a cache, parameters as follows:

Parameter Type Required / Default Value Description
key str Required Key name
value str / int / float Required Data
expires / expire int None Expiration time.
Unit: seconds
None indicates never expires
not_exists bool False Whether to write only when the key does not exist
exists bool False Whether to write only when 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 caches, parameters as follows:

Parameter Type Required / Default Value Description
key_values dict Required Key-Data 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(...)

Retrieve cache, parameters as follows:

Parameter Type Required / Default Value 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 retrieve caches, parameters as follows:

Parameter Type Required / Default Value Description
keys str Required List of keys
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

Retrieve caches based on key name matching patterns, parameters as follows:

Parameter Type Required / Default Value 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(...)

Retrieve cache while setting a new cache value, parameters as follows:

Parameter Type Required / Default Value 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 cached value by a step, parameters as follows:

Parameter Type Required / Default Value 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 cached value by a specified step, parameters as follows:

Parameter Type Required / Default Value 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 Class (Hash)

Using hash structures allows storing similar types of caches under the same Key, reducing the number of Keys in the main cache space

DFF.CACHE.hkeys(...)

Retrieve a list of fields in the hash structure, parameters as follows:

Parameter Type Required / Default Value 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 a field value in the hash structure, parameters as follows:

Parameter Type Required / Default Value Description
key str Required Key name
field str Required Field name
value str / int / float Required Data
not_exists bool False Whether to write only when 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 the hash structure, parameters as follows:

Parameter Type Required / Default Value 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(...)

Retrieve a field value from the hash structure, parameters as follows:

Parameter Type Required / Default Value 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

Retrieve multiple field values from the hash structure, parameters as follows:

Parameter Type Required / Default Value 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

Retrieve all field values from the hash structure, parameters as follows:

Parameter Type Required / Default Value 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 in the hash structure by a step, parameters as follows:

Parameter Type Required / Default Value 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 in the hash structure by a step, parameters as follows:

Parameter Type Required / Default Value 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 the hash structure, parameters as follows:

Parameter Type Required / Default Value 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 Class (List)

Using list structures allows storing multiple caches in order under the same Key, commonly used for implementing queues, stacks, etc.

DFF.CACHE.lpush(...)

Add elements to the list structure from the left, parameters as follows:

Parameter Type Required / Default Value 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 elements to the list structure from the right, parameters as follows:

Parameter Type Required / Default Value 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 elements from the list structure from the left, parameters as follows:

Parameter Type Required / Default Value 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 elements from the list structure from the right, parameters as follows:

Parameter Type Required / Default Value 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 elements from the list structure from the left in a blocking manner, parameters as follows:

Parameter Type Required / Default Value Description
key str / list Required Key name / List of keys
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 elements from the list structure from the right in a blocking manner, parameters as follows:

Parameter Type Required / Default Value Description
key str / list Required Key name / List of keys
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 of one list structure and simultaneously push it to the left of another list structure, returning the element, parameters as follows:

Parameter Type Required / Default Value 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'
Small 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 of one list structure and simultaneously push it to the left of another list structure in a blocking manner, returning the element, parameters as follows:

Parameter Type Required / Default Value 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(...)

Retrieve the number of elements in the list structure, parameters as follows:

Parameter Type Required / Default Value 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(...)

Retrieve a list of elements within the list structure (without popping), parameters as follows:

Parameter Type Required / Default Value Description
key str Required Key name
start int 0 Start index (inclusive)
stop int -1 End index (inclusive, -1 means last)
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(...)

Retain elements in the list structure starting from the left, parameters as follows:

Parameter Type Required / Default Value Description
key str Required Key name
start int Required Start index (inclusive)
stop int Required End index (inclusive, -1 means last)
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')
Small Tip: Limit Queue Length (Reel Back)
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 the list structure 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 Actual Corresponding Method
push lpush
pop rpop
bpop brpop

Set Class (Set)

Using set structures can store non-repeating members

DFF.CACHE.sadd(...)

Added in version 6.0.6

Add members, parameters as follows:

Parameter Type Required / Default Value 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 members, parameters as follows:

Parameter Type Required / Default Value 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

Retrieve the number of members, parameters as follows:

Parameter Type Required / Default Value 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

Retrieve the list of members, parameters as follows:

Parameter Type Required / Default Value 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 as follows:

Parameter Type Required / Default Value 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 Class (ZSet)

Using set structures can store non-repeating members and guarantee order according to a specified score (score, int / float type)

DFF.CACHE.zadd(...)

Added in version 6.0.6

Add members, parameters as follows:

Parameter Type Required / Default Value 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 members, parameters as follows:

Parameter Type Required / Default Value 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

Retrieve the number of members, parameters as follows:

Parameter Type Required / Default Value 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

Retrieve members based on Score order, parameters as follows:

Parameter Type Required / Default Value Description
key str Required Key name
start int 0 Start index (inclusive)
stop int -1 End index (inclusive, -1 means last)
reverse boolean False Whether to reverse output
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

Retrieve members based on Score, parameters as follows:

Parameter Type Required / Default Value Description
key str Required Key name
min_score int "-inf" Start Score (inclusive, "-inf" means minimum)
max_score int "+inf" End Score (inclusive, "+inf" means maximum)
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]]

Message Class

Using message class methods can send messages to clients subscribed to specific topics

DFF.CACHE.publish(...)

Publish a message to a topic, parameters as follows:

Parameter Type Required / Default Value 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')