-
-
Notifications
You must be signed in to change notification settings - Fork 129
Expand file tree
/
Copy pathuser_cache.go
More file actions
50 lines (43 loc) · 1.4 KB
/
user_cache.go
File metadata and controls
50 lines (43 loc) · 1.4 KB
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
// Copyright (c) 2021-2026 Rustam Gilyazov and Contributors.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
package slackdump
import (
"errors"
"sync"
"time"
"github.com/rusq/slackdump/v4/types"
)
type usercache struct {
users types.Users
mu sync.RWMutex
cachedAt time.Time
}
var errCacheExpired = errors.New("cache expired")
// get retrieves users from cache. If cache is empty or expired, it will
// return errCacheExpired.
func (uc *usercache) get(retention time.Duration) (types.Users, error) {
uc.mu.RLock()
defer uc.mu.RUnlock()
if len(uc.users) > 0 && time.Since(uc.cachedAt) < retention {
return uc.users, nil
}
return nil, errCacheExpired
}
func (uc *usercache) set(users types.Users) {
uc.mu.Lock()
defer uc.mu.Unlock()
uc.users = users
uc.cachedAt = time.Now()
}