|
| 1 | +package cache |
| 2 | + |
| 3 | +import ( |
| 4 | + "errors" |
| 5 | + "fmt" |
| 6 | +) |
| 7 | + |
| 8 | +type InMemoryCache struct { |
| 9 | + cache map[string]map[string][]Message |
| 10 | +} |
| 11 | + |
| 12 | +func (m InMemoryCache) GetMessages(stationName, consumerName string, batchSize int) ([]Message, error) { |
| 13 | + if m.cache == nil { |
| 14 | + return nil, errors.New("In-Memory cache wasn't initialized") |
| 15 | + } |
| 16 | + stationMessages, ok := m.cache[stationName] |
| 17 | + if !ok { |
| 18 | + return []Message{}, nil |
| 19 | + } |
| 20 | + consumerMessages, ok := stationMessages[consumerName] |
| 21 | + if !ok { |
| 22 | + return []Message{}, nil |
| 23 | + } |
| 24 | + if batchSize < len(consumerMessages) { |
| 25 | + return consumerMessages[:batchSize], nil |
| 26 | + } |
| 27 | + return consumerMessages, nil |
| 28 | +} |
| 29 | + |
| 30 | +func (m InMemoryCache) GetMessageById(stationName, consumerName string, id uint) (*Message, error) { |
| 31 | + if m.cache == nil { |
| 32 | + return nil, errors.New("In-Memory cache wasn't initialized") |
| 33 | + } |
| 34 | + stationMessages, ok := m.cache[stationName] |
| 35 | + if !ok { |
| 36 | + return nil, fmt.Errorf("message with id = %v not found", id) |
| 37 | + } |
| 38 | + consumerMessages, ok := stationMessages[consumerName] |
| 39 | + if !ok { |
| 40 | + return nil, fmt.Errorf("message with id = %v not found", id) |
| 41 | + } |
| 42 | + for _, msg := range consumerMessages { |
| 43 | + if msg.ID == id { |
| 44 | + return &msg, nil |
| 45 | + } |
| 46 | + } |
| 47 | + return nil, fmt.Errorf("message with id = %v not found", id) |
| 48 | +} |
| 49 | + |
| 50 | +func (m InMemoryCache) AddMessage(message *Message) error { |
| 51 | + messageCacheLock.Lock() |
| 52 | + defer messageCacheLock.Unlock() |
| 53 | + if m.cache == nil { |
| 54 | + m.cache = map[string]map[string][]Message{} |
| 55 | + } |
| 56 | + if m.cache[message.StationName] == nil { |
| 57 | + m.cache[message.StationName] = map[string][]Message{} |
| 58 | + } |
| 59 | + if m.cache[message.StationName][message.ConsumerName] == nil { |
| 60 | + m.cache[message.StationName][message.ConsumerName] = []Message{} |
| 61 | + } |
| 62 | + consumerMessages := m.cache[message.StationName][message.ConsumerName] |
| 63 | + messageId := len(consumerMessages) + 1 |
| 64 | + message.ID = uint(messageId) |
| 65 | + m.cache[message.StationName][message.ConsumerName] = append(consumerMessages, *message) |
| 66 | + return nil |
| 67 | +} |
| 68 | + |
| 69 | +func (m InMemoryCache) RemoveMessage(message *Message) error { |
| 70 | + if m.cache == nil { |
| 71 | + return errors.New("in-memory cache wasn't initialized") |
| 72 | + } |
| 73 | + stationMessages, ok := m.cache[message.StationName] |
| 74 | + if !ok { |
| 75 | + return fmt.Errorf("no cache found for station - %s", message.StationName) |
| 76 | + } |
| 77 | + consumerNameMessages, ok := stationMessages[message.ConsumerName] |
| 78 | + if !ok { |
| 79 | + return fmt.Errorf("no cache found for consumer - %s", message.StationName) |
| 80 | + } |
| 81 | + index := -1 |
| 82 | + for i, msg := range consumerNameMessages { |
| 83 | + if msg.ID == message.ID { |
| 84 | + index = i |
| 85 | + } |
| 86 | + } |
| 87 | + if index != -1 { |
| 88 | + stationMessages[message.ConsumerName] = append(consumerNameMessages[:index], consumerNameMessages[index+1:]...) |
| 89 | + return nil |
| 90 | + } |
| 91 | + return errors.New("message not found in cache") |
| 92 | +} |
0 commit comments