-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.go
More file actions
50 lines (46 loc) · 1.27 KB
/
utils.go
File metadata and controls
50 lines (46 loc) · 1.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package bmemcache
import (
"encoding/json"
)
// generateEmptyData returns the zero value for a given type T.
//
// Returns:
// - The zero value of type T.
func generateEmptyData[T any]() T {
var emptyValue T
return emptyValue
}
// serializeKey converts a slice of strings into a JSON-encoded string to be used
// as a safe cache key.
//
// This approach avoids ambiguity caused by string separators when generating
// composite keys.
//
// Parameters:
// - keys: A slice of strings representing the individual parts of the cache key.
//
// Returns:
// - A JSON-formatted string representing the composite key.
func serializeKey(keys []string) string {
if keys == nil {
keys = []string{}
}
b, _ := json.Marshal(keys) // Always succeeds for []string
return string(b)
}
// deserializeKey converts a JSON-encoded cache key string back into its original
// slice of string components.
//
// This function is the inverse of serializeKey and is used for extracting key parts
// from stored cache map keys.
//
// Parameters:
// - s: A JSON-formatted string representing a composite cache key.
//
// Returns:
// - A slice of strings representing the original key components.
func deserializeKey(s string) []string {
var keys []string
_ = json.Unmarshal([]byte(s), &keys)
return keys
}