概念

会话存储

保存缓存会话的间隔由运行时间决定—— 文件、 本地设置或内存。 您可以用三种方法替换它 。

read · write · clear
Using Python?

Switch any code block to Python: the choice applies to every page. Python SDK guide and resource reference.

默认的获得

运行时间内存幸存者
节点~/.iagenify/session,在以模式 0700 创建的目录中重新开始
浏览器地方单位 iagenify_session每种来源的重新装货
工人工人内内内仅是孤立的

内 Node, sessionPath 移动文件。 其它地方的位置都固定了, 改变的方式就是提供您自己的存储 。

自己供应

3个同步方法 read 返回上一个写入的内容,或 null; write 储存; clear 把它删除

const api = new IaGenify({
  apiKey,
  storage: {
    storageType: "custom",          // reported at exchange; informational
    async read()      { return JSON.parse((await redis.get(key)) || "null"); },
    async write(data) { await redis.set(key, JSON.stringify(data)); },
    async clear()     { await redis.del(key); },
  },
});
import json

class RedisSession:
    storage_type = "custom"          # informational
    def read(self): return json.loads(redis.get(key) or "null")
    def write(self, data): redis.set(key, json.dumps(data))
    def clear(self): redis.delete(key)

api = IaGenify(api_key=api_key, storage=RedisSession())
必须读返回无效, 不丢弃

第一次运行没有存储任何东西。 抓取内部读取并返回无效, 或者第一个呼叫失败, 而不是交换密钥 。

一个完全没有密钥的客户端

如果您的存储已经有一个会话, 您可以在没有密钥的情况下构建客户端 。 既不通过, 也不拒绝, 且指定两个出路 。

// Valid: the session comes from the store
const api = new IaGenify({ storage: myStore });

// Refused at construction
new IaGenify({});
// Error: IaGenify requires an API key. Pass { apiKey } or provide a custom
// storage with an existing session.
# Valid: the session comes from the store
api = IaGenify(storage=my_store)

# Refused at construction
IaGenify()
# ValueError: IaGenify requires api_key or a custom session storage.

使租户分开

const clientFor = (tenant) => new IaGenify({
  apiKey: keyOf(tenant),
  appName: "billing-worker",
  storage: {
    storageType: "custom",
    async read()      { return JSON.parse((await kv.get(`sess:${tenant}`)) || "null"); },
    async write(data) { await kv.put(`sess:${tenant}`, JSON.stringify(data)); },
    async clear()     { await kv.delete(`sess:${tenant}`); },
  },
});
import json

class TenantSession:
    def __init__(self, tenant): self.key = f"sess:{tenant}"
    def read(self): return json.loads(kv.get(self.key) or "null")
    def write(self, data): kv.set(self.key, json.dumps(data))
    def clear(self): kv.delete(self.key)

def client_for(tenant):
    return IaGenify(api_key=key_of(tenant), app_name="billing-worker", storage=TenantSession(tenant))