Repository navigation
Expand file tree
/
Copy pathcache.go
More file actions
71 lines (62 loc) · 1.78 KB
/
Copy pathcache.go
File metadata and controls
71 lines (62 loc) · 1.78 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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
// Copyright (c) 2026 Rajeh Taher
//
// Licensed under the MIT License. See LICENSE-MIT for details.
package whatsmeow
import (
"time"
"github.com/polymorfa/hypermeow/types"
)
func (cli *Client) invalidateParticipantHashCaches(to types.JID) {
switch to.Server {
case types.GroupServer:
cli.groupCacheLock.Lock()
delete(cli.groupCache, to)
cli.groupCacheLock.Unlock()
case types.DefaultUserServer, types.HiddenUserServer, types.BotServer, types.HostedServer, types.HostedLIDServer, types.MessengerServer:
cli.userDevicesCacheLock.Lock()
delete(cli.userDevicesCache, to)
cli.userDevicesCacheLock.Unlock()
}
}
const (
maxGroupCacheEntries = 8
maxUserDeviceCacheEntries = 2048
maxMessageRetryEntries = 1024
maxIncomingRetryEntries = 1024
maxAppStateKeyRequestEntries = 1024
maxSessionRecreateHistoryItems = 1024
retryCounterWindow = time.Hour
)
func ensureMap[K comparable, V any](cache *map[K]V) map[K]V {
if *cache == nil {
*cache = make(map[K]V)
}
return *cache
}
func putBoundedCache[K comparable, V any](cache map[K]V, key K, value V, limit int) {
if _, exists := cache[key]; !exists && len(cache) >= limit {
for oldKey := range cache {
delete(cache, oldKey)
break
}
}
cache[key] = value
}
func incrementBoundedCounter[K comparable](cache map[K]int, key K, limit int, resetAt *time.Time, now time.Time) (int, bool) {
if resetAt.IsZero() || now.Sub(*resetAt) >= retryCounterWindow {
clear(cache)
*resetAt = now
}
if _, exists := cache[key]; !exists && len(cache) >= limit {
return 0, false
}
cache[key]++
return cache[key], true
}
func pruneExpiredCache[K comparable](cache map[K]time.Time, cutoff time.Time) {
for key, timestamp := range cache {
if timestamp.Before(cutoff) {
delete(cache, key)
}
}
}