Skip to content

RU Storage and Cache

Andrewkydev edited this page Aug 25, 2026 · 1 revision

Storage and Cache

English: EN-Storage-and-Cache

storage.Store объединяет Users, Chats и Sessions repositories.

Memory

store := memory.New()
app.Storage(store)

Подходит для тестов и ephemeral процессов. После рестарта данные исчезают.

PostgreSQL

store, err := postgres.New(ctx, os.Getenv("ASTRAENGINE_DATABASE_URL"))
if err != nil {
	return err
}
defer store.Close()

if err := store.EnsureSchema(ctx); err != nil {
	return err
}
app.Storage(store)

postgres.New создаёт pool, но не schema. EnsureSchema нужно вызвать явно или заменить собственной migration системой.

Sessions

repo := app.Store().Sessions()
err := repo.Set(ctx, "verification:42", []byte(`{"step":1}`), 15*time.Minute)
value, err := repo.Get(ctx, "verification:42")
err = repo.Delete(ctx, "verification:42")

Отсутствующая или истёкшая запись совместима с storage.ErrNotFound. Repository хранит opaque bytes; структуру определяет потребитель. state сериализует session автоматически.

app.Storage(store) сам по себе не записывает входящих users/chats — это делает application middleware или модуль.

Cache

Доступны cache/memory и cache/nop с операциями Get, Set, Delete, Remember. Redis implementation пока отсутствует.

Clone this wiki locally