You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/api/state.md
+116Lines changed: 116 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -10,6 +10,122 @@ State management provides a global key–value store for application dependencie
10
10
When prefork is enabled, each worker process has an independent state store, meaning state is not shared between them.
11
11
:::
12
12
13
+
## SharedState (Prefork-safe)
14
+
15
+
For data that must be shared across prefork workers or multiple app processes, use `app.SharedState()` backed by `fiber.Storage`.
16
+
17
+
Configure storage in `fiber.Config`:
18
+
19
+
```go
20
+
app:= fiber.New(fiber.Config{
21
+
AppName: "billing-api",
22
+
SharedStorage: redisStorage, // any implementation of fiber.Storage
23
+
SharedStatePrefix: "billing-shared-", // optional
24
+
})
25
+
```
26
+
27
+
If `SharedStatePrefix` is empty, Fiber derives a default namespace and includes `AppName` (when set) to reduce collisions between apps/services.
28
+
29
+
:::warning Memory storage caveat
30
+
`SharedState` is only cross-worker / cross-process when the configured `SharedStorage` backend is shared.
31
+
32
+
If you use an in-memory backend (for example memory storage), data remains process-local. In prefork mode, each worker process has its own independent in-memory store.
33
+
:::
34
+
35
+
### SharedState Methods
36
+
37
+
```go title="Signature"
38
+
func(app *App) SharedState() *SharedState
39
+
40
+
func (s *SharedState) Set(key string, val []byte, ttl time.Duration) error
41
+
func (s *SharedState) SetWithContext(ctx context.Context, key string, val []byte, ttl time.Duration) error
42
+
43
+
func (s *SharedState) Get(key string) (val []byte, found bool, err error)
44
+
func (s *SharedState) GetWithContext(ctx context.Context, key string) (val []byte, found bool, err error)
45
+
46
+
func (s *SharedState) SetJSON(key string, v any, ttl time.Duration) error
47
+
func (s *SharedState) SetJSONWithContext(ctx context.Context, key string, v any, ttl time.Duration) error
48
+
49
+
func (s *SharedState) GetJSON(key string, out any) (raw []byte, found bool, err error)
50
+
func (s *SharedState) GetJSONWithContext(ctx context.Context, key string, out any) (raw []byte, found bool, err error)
51
+
52
+
func (s *SharedState) SetMsgPack(key string, v any, ttl time.Duration) error
53
+
func (s *SharedState) SetMsgPackWithContext(ctx context.Context, key string, v any, ttl time.Duration) error
54
+
55
+
func (s *SharedState) GetMsgPack(key string, out any) (raw []byte, found bool, err error)
56
+
func (s *SharedState) GetMsgPackWithContext(ctx context.Context, key string, out any) (raw []byte, found bool, err error)
57
+
58
+
func (s *SharedState) SetCBOR(key string, v any, ttl time.Duration) error
59
+
func (s *SharedState) SetCBORWithContext(ctx context.Context, key string, v any, ttl time.Duration) error
60
+
61
+
func (s *SharedState) GetCBOR(key string, out any) (raw []byte, found bool, err error)
62
+
func (s *SharedState) GetCBORWithContext(ctx context.Context, key string, out any) (raw []byte, found bool, err error)
63
+
64
+
func (s *SharedState) SetXML(key string, v any, ttl time.Duration) error
65
+
func (s *SharedState) SetXMLWithContext(ctx context.Context, key string, v any, ttl time.Duration) error
66
+
67
+
func (s *SharedState) GetXML(key string, out any) (raw []byte, found bool, err error)
68
+
func (s *SharedState) GetXMLWithContext(ctx context.Context, key string, out any) (raw []byte, found bool, err error)
69
+
70
+
func (s *SharedState) Delete(key string) error
71
+
func (s *SharedState) DeleteWithContext(ctx context.Context, key string) error
72
+
73
+
func (s *SharedState) Has(key string) (bool, error)
74
+
func (s *SharedState) HasWithContext(ctx context.Context, key string) (bool, error)
// timeout, cancellation, storage error, or JSON serialization error
126
+
}
127
+
```
128
+
13
129
## State Type
14
130
15
131
`State` is a key–value store built on top of `sync.Map` to ensure safe concurrent access. It allows storage and retrieval of dependencies and configurations in a Fiber application as well as thread–safe access to runtime data.
Copy file name to clipboardExpand all lines: docs/whats_new.md
+1Lines changed: 1 addition & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -84,6 +84,7 @@ We have made several changes to the Fiber app, including:
84
84
-**RegisterCustomConstraint**: Allows for the registration of custom constraints.
85
85
-**NewWithCustomCtx**: Initialize an app with a custom context in one step.
86
86
-**State**: Provides a global state for the application, which can be used to store and retrieve data across the application. Check out the [State](./api/state) method for further details.
87
+
-**SharedState**: Introduces storage-backed app state for prefork-safe/multi-process coordination via `Config.SharedStorage`, with optional `Config.SharedStatePrefix` namespacing and JSON/context-aware helpers (`SetJSON`, `GetJSON`, `Has`, `Delete`, and `WithContext` variants).
87
88
-**NewErrorf**: Allows variadic parameters when creating formatted errors.
88
89
-**GetBytes / GetString**: Helpers that detach values only when `Immutable` is enabled and the data still references request or response buffers. Access via `c.App().GetString` and `c.App().GetBytes`.
89
90
-**ReloadViews**: Lets you re-run the configured view engine's `Load()` logic at runtime, including guard rails for missing or nil view engines so development hot-reload hooks can refresh templates safely.
0 commit comments