-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstorage.go
33 lines (27 loc) · 1.03 KB
/
storage.go
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
// Package acc provides an accumulator mechanism to accumulate, process, and manage data.
package acc
import "context"
// DataStorage defines the interface for any storage mechanism to save and load data.
type DataStorage[T interface{}] interface {
// Save stores the provided data items.
Save(ctx context.Context, data []T) error
// Load retrieves the stored data items.
Load(ctx context.Context) ([]T, error)
}
// InMemoryStorage is an in-memory storage implementation for the DataStorage interface.
type InMemoryStorage[T any] struct {
data []Data[T]
}
// NewInMemoryStorage initializes a new instance of InMemoryStorage.
func NewInMemoryStorage[T any]() *InMemoryStorage[T] {
return &InMemoryStorage[T]{}
}
// Save stores the provided data items in memory.
func (s *InMemoryStorage[T]) Save(ctx context.Context, data []Data[T]) error {
s.data = append(s.data, data...)
return nil
}
// Load retrieves the stored data items from memory.
func (s *InMemoryStorage[T]) Load(ctx context.Context) ([]Data[T], error) {
return s.data, nil
}