Get retrieves a value from KV storage by group and key. Returns the value as a string or an error if the key is not found.
(ctx context.Context, params KVParams)
| 137 | // Get retrieves a value from KV storage by group and key. |
| 138 | // Returns the value as a string or an error if the key is not found. |
| 139 | func (kv *KVOperator) Get(ctx context.Context, params KVParams) (string, error) { |
| 140 | if params.Key == "" { |
| 141 | return "", ErrKVKeyEmpty |
| 142 | } |
| 143 | |
| 144 | if value, exist, err := kv.getCache(ctx, params); err == nil && exist { |
| 145 | return value, nil |
| 146 | } |
| 147 | |
| 148 | // query |
| 149 | data := entity.PluginKVStorage{} |
| 150 | query, cleanup := kv.getSession(ctx) |
| 151 | defer cleanup() |
| 152 | |
| 153 | query.Where(builder.Eq{ |
| 154 | "plugin_slug_name": kv.pluginSlugName, |
| 155 | "`group`": params.Group, |
| 156 | "`key`": params.Key, |
| 157 | }) |
| 158 | |
| 159 | has, err := query.Get(&data) |
| 160 | if err != nil { |
| 161 | return "", err |
| 162 | } |
| 163 | if !has { |
| 164 | return "", ErrKVKeyNotFound |
| 165 | } |
| 166 | |
| 167 | params.Value = data.Value |
| 168 | kv.setCache(ctx, params) |
| 169 | |
| 170 | return data.Value, nil |
| 171 | } |
| 172 | |
| 173 | // Set stores a value in KV storage with the specified group and key. |
| 174 | // Updates the value if it already exists. |