File: cache.go

package info (click to toggle)
golang-github-henrybear327-go-proton-api 1.0.0-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,088 kB
  • sloc: sh: 55; makefile: 26
file content (52 lines) | stat: -rw-r--r-- 922 bytes parent folder | download
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
package server

import (
	"sync"

	"github.com/henrybear327/go-proton-api"
)

func NewAuthCache() AuthCacher {
	return &authCache{
		info: make(map[string]proton.AuthInfo),
		auth: make(map[string]proton.Auth),
	}
}

type authCache struct {
	info map[string]proton.AuthInfo
	auth map[string]proton.Auth
	lock sync.RWMutex
}

func (c *authCache) GetAuthInfo(username string) (proton.AuthInfo, bool) {
	c.lock.RLock()
	defer c.lock.RUnlock()

	info, ok := c.info[username]

	return info, ok
}

func (c *authCache) SetAuthInfo(username string, info proton.AuthInfo) {
	c.lock.Lock()
	defer c.lock.Unlock()

	c.info[username] = info
}

func (c *authCache) GetAuth(username string) (proton.Auth, bool) {
	c.lock.RLock()
	defer c.lock.RUnlock()

	auth, ok := c.auth[username]

	return auth, ok
}

func (c *authCache) SetAuth(username string, auth proton.Auth) {
	c.lock.Lock()
	defer c.lock.Unlock()

	c.auth[username] = auth
}