ZBLOG

不需要索引来实现

如果不使用索引(例如,Set 数据结构)来存储 nfType 相关的记录,我们可以直接将所有 NF Profile 存储为 Hash,并在每次查询时遍历所有记录。尽管这种方法在数据量较小时可以工作,但在大规模数据时效率会低下。

数据结构设计

  • 使用 Hash 存储每个 NF Profile
    • 键:nfprofile:<id>(唯一标识符)
    • 字段:
      • nfType
      • nfStatus
      • plmnList
      • ipv4Addresses
      • nfInstanceId

示例代码实现

下面是 Go 语言中如何实现这个数据结构,并提供存储和获取功能的示例代码。

1. 实现存储与获取方法

  1. package main
  2. import (
  3. "context"
  4. "encoding/json"
  5. "fmt"
  6. "log"
  7. "github.com/go-redis/redis/v8"
  8. )
  9. type NfProfile struct {
  10. ID string `json:"_id"`
  11. NfType string `json:"nfType"`
  12. NfStatus string `json:"nfStatus"`
  13. PmlnList []Plmn `json:"plmnList"`
  14. Ipv4Addresses []string `json:"ipv4Addresses"`
  15. NfInstanceId string `json:"nfInstanceId"`
  16. }
  17. type Plmn struct {
  18. MCC string `json:"mcc"`
  19. MNC string `json:"mnc"`
  20. }
  21. // 将NfProfile数据保存到Redis
  22. func storeProfile(ctx context.Context, rdb *redis.Client, profile NfProfile) error {
  23. key := fmt.Sprintf("nfprofile:%s", profile.ID)
  24. // 存入Hash表
  25. if err := rdb.HSet(ctx, key, map[string]interface{}{
  26. "nfType": profile.NfType,
  27. "nfStatus": profile.NfStatus,
  28. "plmnList": profile.PmlnList,
  29. "ipv4Addresses": profile.Ipv4Addresses,
  30. "nfInstanceId": profile.NfInstanceId,
  31. }).Err(); err != nil {
  32. return err
  33. }
  34. return nil
  35. }
  36. // 获取所有 NfProfiles 并根据 nfType 筛选
  37. func getProfilesByNfType(ctx context.Context, rdb *redis.Client, nfType string) ([]NfProfile, error) {
  38. var profiles []NfProfile
  39. keys, err := rdb.Keys(ctx, "nfprofile:*").Result() // 获取所有NF Profiles的键
  40. if err != nil || len(keys) == 0 {
  41. return nil , err
  42. }
  43. for _, key := range keys {
  44. recordData, err := rdb.HGetAll(ctx, key).Result()
  45. if err != nil || len(recordData) == 0 { continue }
  46. // 检查 nfType 是否匹配
  47. if recordData["nfType"] == nfType {
  48. var profile NfProfile
  49. // 手动赋值字段信息
  50. profile.ID = key[len("nfprofile:"):]
  51. // 获取其他字段信息并解析为对应类型
  52. if val, exists := recordData["nfStatus"]; exists {
  53. profile.NfStatus = val
  54. }
  55. if val , exists:=recordData["plmnList"]; exists{
  56. json.Unmarshal([]byte(val),&profile.PmlnList)
  57. }
  58. if val , exists:=recordData["ipv4Addresses"]; exists{
  59. json.Unmarshal([]byte(val),&profile.Ipv4Addresses)
  60. }
  61. if val , exists:=recordData["nfInstanceId"]; exists{
  62. profile.NfInstanceId =val
  63. }
  64. profiles = append(profiles ,profile)
  65. }
  66. }
  67. return profiles,nil
  68. }
  69. func main() {
  70. ctx := context.Background()
  71. rdb := redis.NewClient(&redis.Options{
  72. Addr: "localhost:6379",
  73. })
  74. defer rdb.Close()
  75. profile1 := NfProfile{
  76. ID: "123",
  77. NfType: "gateway",
  78. NfStatus: "active",
  79. PmlnList: []Plmn{{MCC: "460", MNC: "00"}},
  80. Ipv4Addresses: []string{"192.168.1.1"},
  81. NfInstanceId: "inst123",
  82. }
  83. err := storeProfile(ctx,rdb ,profile1)
  84. if err !=nil{ log.Fatalf("Failed to store data in Redis : %v",err)}
  85. profiles ,err:=getProfilesByNfType (ctx,rdb,"gateway")
  86. if err!=nil{log.Fatalf("Failed to retrieve data : %v",err)}
  87. fmt.Printf("Retrieved Profiles by Type 'gateway': %+v\n", profiles)
  88. }

注意事项:

  • 性能问题:由于我们没有建立索引,这种方式在数据库中的记录数量很大时,性能会变得非常低下。
  • JSON 序列化与反序列化:对于复杂的数据结构(如切片),需要通过 JSON 来进行存取,你可能还需额外处理编码和解码。
  • 错误处理:确保你有良好的错误处理,以避免程序因单个操作失败而崩溃。

这样你就能通过简单的方法实现基于 NF Type 的记录存储与获取,而无需创建额外的索引。


内容由零声教学AI助手提供,问题来源于学员提问

本站部分文章来源于网络,版权归原作者所有,如有侵权请联系站长删除。
转载请注明出处:https://golang.0voice.com/?id=21136

分享:
扫描分享到社交APP
上一篇
下一篇
发表列表
游客 游客
此处应有掌声~
评论列表

还没有评论,快来说点什么吧~

联系我们

在线咨询: 点击这里给我发消息

微信号:3007537140

上班时间: 10:30-22:30

关注我们
x

注册

已经有帐号?