store.go 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. package gomatrix
  2. // Storer is an interface which must be satisfied to store client data.
  3. //
  4. // You can either write a struct which persists this data to disk, or you can use the
  5. // provided "InMemoryStore" which just keeps data around in-memory which is lost on
  6. // restarts.
  7. type Storer interface {
  8. SaveFilterID(userID, filterID string)
  9. LoadFilterID(userID string) string
  10. SaveNextBatch(userID, nextBatchToken string)
  11. LoadNextBatch(userID string) string
  12. SaveRoom(room *Room)
  13. LoadRoom(roomID string) *Room
  14. }
  15. // InMemoryStore implements the Storer interface.
  16. //
  17. // Everything is persisted in-memory as maps. It is not safe to load/save filter IDs
  18. // or next batch tokens on any goroutine other than the syncing goroutine: the one
  19. // which called Client.Sync().
  20. type InMemoryStore struct {
  21. Filters map[string]string
  22. NextBatch map[string]string
  23. Rooms map[string]*Room
  24. }
  25. // SaveFilterID to memory.
  26. func (s *InMemoryStore) SaveFilterID(userID, filterID string) {
  27. s.Filters[userID] = filterID
  28. }
  29. // LoadFilterID from memory.
  30. func (s *InMemoryStore) LoadFilterID(userID string) string {
  31. return s.Filters[userID]
  32. }
  33. // SaveNextBatch to memory.
  34. func (s *InMemoryStore) SaveNextBatch(userID, nextBatchToken string) {
  35. s.NextBatch[userID] = nextBatchToken
  36. }
  37. // LoadNextBatch from memory.
  38. func (s *InMemoryStore) LoadNextBatch(userID string) string {
  39. return s.NextBatch[userID]
  40. }
  41. // SaveRoom to memory.
  42. func (s *InMemoryStore) SaveRoom(room *Room) {
  43. s.Rooms[room.ID] = room
  44. }
  45. // LoadRoom from memory.
  46. func (s *InMemoryStore) LoadRoom(roomID string) *Room {
  47. return s.Rooms[roomID]
  48. }
  49. // NewInMemoryStore constructs a new InMemoryStore.
  50. func NewInMemoryStore() *InMemoryStore {
  51. return &InMemoryStore{
  52. Filters: make(map[string]string),
  53. NextBatch: make(map[string]string),
  54. Rooms: make(map[string]*Room),
  55. }
  56. }