iii-state-management
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseState Management
状态管理
Comparable to: Redis, DynamoDB, Memcached
可类比于:Redis、DynamoDB、Memcached
Key Concepts
核心概念
Use the concepts below when they fit the task. Not every state operation needs all of them.
- State is a scoped key-value store accessed via built-in trigger functions
- state::set writes a value; state::get reads it (returns for missing keys)
null - state::list retrieves all keys in a scope; state::delete removes a key
- state::update performs a partial merge using an array for fine-grained changes
ops - Payloads use ,
scope, andkeyto address state entriesvalue - State is shared across all functions — use meaningful scope names to avoid collisions
当任务匹配时使用以下概念,并非所有状态操作都需要用到全部概念。
- 状态是通过内置触发函数访问的带作用域的键值存储
- state::set 用于写入值;state::get 用于读取值(不存在的键返回)
null - state::list 用于检索某个作用域内的所有键;state::delete 用于删除键
- state::update 通过数组执行部分合并,实现细粒度变更
ops - 负载通过、
scope和key定位状态条目value - 状态在所有函数间共享——使用有意义的作用域名称避免冲突
Architecture
架构
Function
→ trigger('state::set', { scope, key, value })
→ trigger('state::get', { scope, key })
→ trigger('state::update', { scope, key, ops })
→ trigger('state::delete', { scope, key })
→ trigger('state::list', { scope })
→ StateModule → KvStore / Redis adapter
Function
→ trigger('state::set', { scope, key, value })
→ trigger('state::get', { scope, key })
→ trigger('state::update', { scope, key, ops })
→ trigger('state::delete', { scope, key })
→ trigger('state::list', { scope })
→ StateModule → KvStore / Redis adapter
iii Primitives Used
iii 所用原语
| Primitive | Purpose |
|---|---|
| Write a value to state |
| Read a value from state |
| List all keys in a scope |
| Remove a key from state |
| Partial merge with operations array |
| 原语 | 用途 |
|---|---|
| 向状态中写入值 |
| 从状态中读取值 |
| 列出某个作用域内的所有键 |
| 从状态中删除键 |
| 通过操作数组执行部分合并 |
Reference Implementation
参考实现
See ../references/state-management.js for the full working example — functions that read, write, update, and delete state entries across a shared scope.
Also available in Python: ../references/state-management.py
Also available in Rust: ../references/state-management.rs
完整工作示例请查看 ../references/state-management.js——该示例中的函数可在共享作用域内读写、更新和删除状态条目。
同时提供Python版本:../references/state-management.py
以及Rust版本:../references/state-management.rs
Common Patterns
常见模式
Code using this pattern commonly includes, when relevant:
- — worker initialization
registerWorker(url, { workerName }) - — write state
trigger({ function_id: 'state::set', payload: { scope, key, value } }) - — read state (returns
trigger({ function_id: 'state::get', payload: { scope, key } })if missing)null - — partial merge
trigger({ function_id: 'state::update', payload: { scope, key, ops } }) - — enumerate keys
trigger({ function_id: 'state::list', payload: { scope } }) - — remove entry
trigger({ function_id: 'state::delete', payload: { scope, key } }) - — structured logging
const logger = new Logger()
使用此模式的代码通常包含以下内容(相关时):
- —— 工作进程初始化
registerWorker(url, { workerName }) - —— 写入状态
trigger({ function_id: 'state::set', payload: { scope, key, value } }) - —— 读取状态(不存在时返回
trigger({ function_id: 'state::get', payload: { scope, key } }))null - —— 部分合并
trigger({ function_id: 'state::update', payload: { scope, key, ops } }) - —— 枚举键
trigger({ function_id: 'state::list', payload: { scope } }) - —— 删除条目
trigger({ function_id: 'state::delete', payload: { scope, key } }) - —— 结构化日志
const logger = new Logger()
Adapting This Pattern
适配此模式
Use the adaptations below when they apply to the task.
- Name scopes after your domain (e.g. ,
user-sessions,order-data)config - Use with a
state::getcheck to handle missing keys gracefullynull - Use with
state::updatefor partial updates instead of read-modify-write cyclesops - Combine with to persist results after async job completion
iii-queue-processing
当以下适配场景符合任务需求时使用:
- 根据业务领域命名作用域(例如、
user-sessions、order-data)config - 使用并配合
state::get检查,优雅处理不存在的键null - 使用带的
ops执行部分更新,替代读取-修改-写入循环state::update - 与结合,在异步任务完成后持久化结果
iii-queue-processing
Engine Configuration
引擎配置
StateModule must be enabled in iii-config.yaml with a KvStore adapter (file-based or Redis). See ../references/iii-config.yaml for the full annotated config reference.
必须在iii-config.yaml中启用StateModule并配置KvStore适配器(基于文件或Redis)。完整带注释的配置参考请查看 ../references/iii-config.yaml
Pattern Boundaries
模式边界
- If the task needs reactive side effects when state changes, prefer .
iii-state-reactions - If the task needs real-time client push when data updates, prefer .
iii-realtime-streams - Stay with when the primary need is reading and writing persistent key-value data.
iii-state-management
- 如果任务需要在状态变更时触发响应式副作用,优先使用。
iii-state-reactions - 如果任务需要在数据更新时向客户端实时推送,优先使用。
iii-realtime-streams - 当主要需求是读写持久化键值数据时,使用。
iii-state-management
When to Use
使用场景
- Use this skill when the task is primarily about in the iii engine.
iii-state-management - Triggers when the request directly asks for this pattern or an equivalent implementation.
- 当任务主要涉及iii引擎中的时,使用此技能。
iii-state-management - 当请求直接要求此模式或等效实现时触发。
Boundaries
边界限制
- Never use this skill as a generic fallback for unrelated tasks.
- You must not apply this skill when a more specific iii skill is a better fit.
- Always verify environment and safety constraints before applying examples from this skill.
- 切勿将此技能作为无关任务的通用 fallback 方案。
- 当有更特定的iii技能更适合时,不得应用此技能。
- 在应用此技能中的示例前,务必验证环境和安全约束。