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/guide/data/cache.md
+63Lines changed: 63 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -88,3 +88,66 @@ You can also use the `store.<modelName>.clearItem` method:
88
88
```ts
89
89
store.User.clearItem('abc')
90
90
```
91
+
92
+
## Layers
93
+
94
+
A cache layer is a way to create a temporary state modification that can be easily reverted. This is how [optimistic updates](./mutation.md#optimistic-updates) are implemented.
95
+
96
+
To create a new layer, use the `addLayer` method:
97
+
98
+
```ts
99
+
store.$cache.addLayer({
100
+
id: 'some-layer-id',
101
+
state: {
102
+
Messages: {
103
+
'some-message-id': {
104
+
$overrideKey: 'some-message-id',
105
+
text: 'This is an optimistic message',
106
+
},
107
+
},
108
+
},
109
+
deleteItems: {},
110
+
optimistic: true, // Optional
111
+
prevent: { // Optional
112
+
update: false,
113
+
delete: false,
114
+
},
115
+
skip: false, // Optional
116
+
})
117
+
```
118
+
119
+
In this example, the layer will override the `text` property of the `Messages` item with id `some-message-id`.
120
+
121
+
::: tip
122
+
If the layer contains records that do not exist in the cache, it will act as if those records were created.
123
+
:::
124
+
125
+
```ts
126
+
store.$cache.addLayer({
127
+
id: 'some-layer-id',
128
+
state: {},
129
+
deleteItems: {
130
+
Messages: newSet(['some-message-id']),
131
+
},
132
+
})
133
+
```
134
+
135
+
In this second example, the layer will delete the `Messages` item with id `some-message-id`.
136
+
137
+
::: tip
138
+
If multiple layers, they are applied in the order they were added.
0 commit comments