summaryrefslogtreecommitdiff
path: root/cache/folders.go
blob: 7ff54e640ef48c87bb0cceb0edc59c3b8913a77f (plain)
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
package cache

import (
	"lain/types"
	"time"

	"github.com/gofiber/fiber/v2"
)

var folders *types.FolderCache

func init() {
	folders = &types.FolderCache{
		Data: make(map[string]*types.FolderCacheEntry),
		TTL:  5 * time.Minute,
	}
}

func GetFolders(userEmail string) ([]fiber.Map, bool) {
	folders.Mu.RLock()
	defer folders.Mu.RUnlock()

	entry, exists := folders.Data[userEmail]
	if !exists {
		return nil, false
	}

	if time.Now().After(entry.ExpiresAt) {
		return nil, false
	}

	return entry.Folders, true
}

func SetFolders(userEmail string, folderList []fiber.Map) {
	folders.Mu.Lock()
	defer folders.Mu.Unlock()

	now := time.Now()
	folders.Data[userEmail] = &types.FolderCacheEntry{
		Folders:   folderList,
		CachedAt:  now,
		ExpiresAt: now.Add(folders.TTL),
	}
}

func InvalidateFolders(userEmail string) {
	folders.Mu.Lock()
	defer folders.Mu.Unlock()

	delete(folders.Data, userEmail)
}

func InvalidateAllFolders() {
	folders.Mu.Lock()
	defer folders.Mu.Unlock()

	folders.Data = make(map[string]*types.FolderCacheEntry)
}

func SetFolderTTL(duration time.Duration) {
	folders.Mu.Lock()
	defer folders.Mu.Unlock()

	folders.TTL = duration
}