Set stores a value in KV storage with the specified group and key. Updates the value if it already exists.
(ctx context.Context, params KVParams)
| 173 | // Set stores a value in KV storage with the specified group and key. |
| 174 | // Updates the value if it already exists. |
| 175 | func (kv *KVOperator) Set(ctx context.Context, params KVParams) error { |
| 176 | if params.Key == "" { |
| 177 | return ErrKVKeyEmpty |
| 178 | } |
| 179 | |
| 180 | query, cleanup := kv.getSession(ctx) |
| 181 | defer cleanup() |
| 182 | |
| 183 | data := &entity.PluginKVStorage{ |
| 184 | PluginSlugName: kv.pluginSlugName, |
| 185 | Group: params.Group, |
| 186 | Key: params.Key, |
| 187 | Value: params.Value, |
| 188 | } |
| 189 | |
| 190 | kv.cleanCache(ctx, params) |
| 191 | |
| 192 | affected, err := query.Where(builder.Eq{ |
| 193 | "plugin_slug_name": kv.pluginSlugName, |
| 194 | "`group`": params.Group, |
| 195 | "`key`": params.Key, |
| 196 | }).Cols("value").Update(data) |
| 197 | if err != nil { |
| 198 | return err |
| 199 | } |
| 200 | |
| 201 | if affected == 0 { |
| 202 | _, err = query.Insert(data) |
| 203 | if err != nil { |
| 204 | return err |
| 205 | } |
| 206 | } |
| 207 | return nil |
| 208 | } |
| 209 | |
| 210 | // Del removes values from KV storage by group and/or key. |
| 211 | // If both group and key are provided, only that specific entry is deleted. |