get data from storage or memory
(ctx context.Context, key string)
| 108 | |
| 109 | // get data from storage or memory |
| 110 | func (m *manager) get(ctx context.Context, key string) (*item, error) { |
| 111 | if m.storage != nil { |
| 112 | raw, err := m.storage.GetWithContext(ctx, key) |
| 113 | if err != nil { |
| 114 | return nil, fmt.Errorf("cache: failed to get key %q from storage: %w", m.logKey(key), err) |
| 115 | } |
| 116 | if raw == nil { |
| 117 | return nil, errCacheMiss |
| 118 | } |
| 119 | |
| 120 | it := m.acquire() |
| 121 | if _, err := it.UnmarshalMsg(raw); err != nil { |
| 122 | m.release(it) |
| 123 | return nil, fmt.Errorf("cache: failed to unmarshal key %q: %w", m.logKey(key), err) |
| 124 | } |
| 125 | |
| 126 | return it, nil |
| 127 | } |
| 128 | |
| 129 | if value := m.memory.Get(key); value != nil { |
| 130 | it, ok := value.(*item) |
| 131 | if !ok { |
| 132 | return nil, fmt.Errorf("cache: unexpected entry type %T for key %q", value, m.logKey(key)) |
| 133 | } |
| 134 | return it, nil |
| 135 | } |
| 136 | |
| 137 | return nil, errCacheMiss |
| 138 | } |
| 139 | |
| 140 | // get raw data from storage or memory |
| 141 | func (m *manager) getRaw(ctx context.Context, key string) ([]byte, error) { |