aboutsummaryrefslogtreecommitdiff
path: root/telegram/cache/cache.go
blob: 3144ca9ea099a8a508656306eafa087aada51e1f (plain) (blame)
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package cache

import (
	"sync"

	"github.com/godcong/go-tdlib/client"
)

// Status stores formatted data for XMPP presence
type Status struct {
	ID          int64
	XMPP        string
	Description string
}

// Cache allows operating the chats and users cache in
// a thread-safe manner
type Cache struct {
	chats        map[int64]*client.Chat
	users        map[int64]*client.User
	statuses     map[int64]*Status
	chatsLock    sync.Mutex
	usersLock    sync.Mutex
	statusesLock sync.Mutex
}

// NewCache initializes a cache
func NewCache() *Cache {
	return &Cache{
		chats:    map[int64]*client.Chat{},
		users:    map[int64]*client.User{},
		statuses: map[int64]*Status{},
	}
}

// ChatsKeys grabs chat ids synchronously to avoid lockups
// while they are used
func (cache *Cache) ChatsKeys() []int64 {
	cache.chatsLock.Lock()
	defer cache.chatsLock.Unlock()

	var keys []int64
	for id := range cache.chats {
		keys = append(keys, id)
	}
	return keys
}

// UsersKeys grabs user ids synchronously to avoid lockups
// while they are used
func (cache *Cache) UsersKeys() []int64 {
	cache.usersLock.Lock()
	defer cache.usersLock.Unlock()

	var keys []int64
	for id := range cache.users {
		keys = append(keys, id)
	}
	return keys
}

// StatusesRange loops through the map in a thread-safe manner
func (cache *Cache) StatusesRange() chan *Status {
	cache.statusesLock.Lock()

	statusChan := make(chan *Status, 1)

	go func() {
		defer func() {
			cache.statusesLock.Unlock()
			close(statusChan)
		}()

		for _, status := range cache.statuses {
			statusChan <- status
		}
	}()

	return statusChan
}

// GetChat retrieves chat by id if it's present in the cache
func (cache *Cache) GetChat(id int64) (*client.Chat, bool) {
	cache.chatsLock.Lock()
	defer cache.chatsLock.Unlock()

	chat, ok := cache.chats[id]
	return chat, ok
}

// GetUser retrieves user by id if it's present in the cache
func (cache *Cache) GetUser(id int64) (*client.User, bool) {
	cache.usersLock.Lock()
	defer cache.usersLock.Unlock()

	user, ok := cache.users[id]
	return user, ok
}

// GetStatus retrieves status by id if it's present in the cache
func (cache *Cache) GetStatus(id int64) (*Status, bool) {
	cache.statusesLock.Lock()
	defer cache.statusesLock.Unlock()

	status, ok := cache.statuses[id]
	return status, ok
}

// SetChat stores a chat in the cache
func (cache *Cache) SetChat(id int64, chat *client.Chat) {
	cache.chatsLock.Lock()
	defer cache.chatsLock.Unlock()

	cache.chats[id] = chat
}

// SetUser stores a user in the cache
func (cache *Cache) SetUser(id int64, user *client.User) {
	cache.usersLock.Lock()
	defer cache.usersLock.Unlock()

	cache.users[id] = user
}

// SetStatus stores a status in the cache
func (cache *Cache) SetStatus(id int64, show string, status string) {
	cache.statusesLock.Lock()
	defer cache.statusesLock.Unlock()

	cache.statuses[id] = &Status{
		ID:          id,
		XMPP:        show,
		Description: status,
	}
}