Get implements the groupcache.Getter interface. Use `GetByID` and `List` instead.
(ctx context.Context, key string, dest groupcache.Sink)
| 49 | // Get implements the groupcache.Getter interface. |
| 50 | // Use `GetByID` and `List` instead. |
| 51 | func (c *Cache) Get(ctx context.Context, key string, dest groupcache.Sink) error { |
| 52 | if len(key) < 2 { // empty or missing prefix+key, should never happen. |
| 53 | return sql.ErrUnprocessable |
| 54 | } |
| 55 | |
| 56 | var v interface{} |
| 57 | |
| 58 | prefix := key[0:1] |
| 59 | key = key[1:] |
| 60 | switch prefix { |
| 61 | case prefixID: |
| 62 | // Get by ID. |
| 63 | id, err := strconv.ParseInt(key, 10, 64) |
| 64 | if err != nil || id <= 0 { |
| 65 | return err |
| 66 | } |
| 67 | |
| 68 | switch c.service.RecordInfo().(type) { |
| 69 | case *entity.Category: |
| 70 | v = new(entity.Category) |
| 71 | case *entity.Product: |
| 72 | v = new(entity.Product) |
| 73 | } |
| 74 | |
| 75 | err = c.service.GetByID(ctx, v, id) |
| 76 | if err != nil { |
| 77 | return err |
| 78 | } |
| 79 | |
| 80 | case prefixList: |
| 81 | // Get a set of records, list. |
| 82 | q, err := url.ParseQuery(key) |
| 83 | if err != nil { |
| 84 | return err |
| 85 | } |
| 86 | opts := sql.ParseListOptions(q) |
| 87 | |
| 88 | switch c.service.RecordInfo().(type) { |
| 89 | case *entity.Category: |
| 90 | v = new(entity.Categories) |
| 91 | case *entity.Product: |
| 92 | v = new(entity.Products) |
| 93 | } |
| 94 | |
| 95 | err = c.service.List(ctx, v, opts) |
| 96 | if err != nil { |
| 97 | return err |
| 98 | } |
| 99 | |
| 100 | default: |
| 101 | return sql.ErrUnprocessable |
| 102 | } |
| 103 | |
| 104 | b, err := json.Marshal(v) |
| 105 | if err != nil { |
| 106 | return err |
| 107 | } |
| 108 |