mirror of https://github.com/matrix-org/gomatrix
52 lines
1.5 KiB
Go
52 lines
1.5 KiB
Go
package gomatrix
|
|
|
|
// Storer is an interface which must be satisfied to store client data.
|
|
//
|
|
// You can either write a struct which persists this data to disk, or you can use the
|
|
// provided "InMemoryStore" which just keeps data around in-memory which is lost on
|
|
// restarts.
|
|
type Storer interface {
|
|
SaveFilterID(userID, filterID string)
|
|
LoadFilterID(userID string) string
|
|
SaveNextBatch(userID, nextBatchToken string)
|
|
LoadNextBatch(userID string) string
|
|
}
|
|
|
|
// InMemoryStore implements the Storer interface.
|
|
//
|
|
// Everything is persisted in-memory as maps. It is not safe to load/save filter IDs
|
|
// or next batch tokens on any goroutine other than the syncing goroutine: the one
|
|
// which called Client.Sync().
|
|
type InMemoryStore struct {
|
|
Filters map[string]string
|
|
NextBatch map[string]string
|
|
}
|
|
|
|
// SaveFilterID to memory.
|
|
func (s *InMemoryStore) SaveFilterID(userID, filterID string) {
|
|
s.Filters[userID] = filterID
|
|
}
|
|
|
|
// LoadFilterID from memory.
|
|
func (s *InMemoryStore) LoadFilterID(userID string) string {
|
|
return s.Filters[userID]
|
|
}
|
|
|
|
// SaveNextBatch to memory.
|
|
func (s *InMemoryStore) SaveNextBatch(userID, nextBatchToken string) {
|
|
s.NextBatch[userID] = nextBatchToken
|
|
}
|
|
|
|
// LoadNextBatch from memory.
|
|
func (s *InMemoryStore) LoadNextBatch(userID string) string {
|
|
return s.NextBatch[userID]
|
|
}
|
|
|
|
// NewInMemoryStore constructs a new InMemoryStore.
|
|
func NewInMemoryStore() *InMemoryStore {
|
|
return &InMemoryStore{
|
|
Filters: make(map[string]string),
|
|
NextBatch: make(map[string]string),
|
|
}
|
|
}
|