# Armazenamento de sessões

Canonical URL: https://developers.iagenify.com/pt/sdk/concepts/session-storage/
Language: pt

Se la sessione catched è mantenida è decisa dal tempo d'esecuzione — un file, localStorage, o memoria. Può sostituirlo con tre metodi.

Status: read · write · clear

## Ce obten per default {#default}

| Tempo di esecuzione | Manteneva in | Sobrevivèncias |
| --- | --- | --- |
| nodo | ~/.iagenify/session, in un directory creato con modo 0700 | reavizia |
| Navegatore | LocalStorage, sotto iagenify_session | recarghe, per origine |
| - operà | Memória | Solo l'isolado |

In Node, `sessionPath` move il file. In ogni altra parte il luogo è fixed, e la forma di cambiarlo è di fornire il vostro storage.

## Fornecendo il tuo {#custom}

Tre metodi async. `read` restituisce o `null`; `write` lo memorizza; `clear` lo remove.

```javascript
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); },
  },
});
```

> **lettura deve restituire null, non lancia**
>
> Una prima run non ha nulla memorized. Prende dentro led e restitui null, o la prima chiamata fa fail in lugar di scambiare la chiave.

## Un client senza chiude {#without-key}

Se il vostro stoccaggio già tiene una sessione, si può costruire un client senza una chiave. Pass nén e il costruttor renie, nomeando ambele usa.

```javascript
// 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.
```

## Mantenendo i locatari separati {#isolation}

```javascript
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}`); },
  },
});
```

```python
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())
```

```python
# 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.
```

```python
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))
```
