shop.tonybtw.com

shop.tonybtw.com

https://git.tonybtw.com/shop.tonybtw.com.git git://git.tonybtw.com/shop.tonybtw.com.git
710 bytes raw
1
package lib
2
3
import (
4
	"database/sql"
5
	"sync"
6
)
7
8
type Session_Store struct {
9
	mu    sync.RWMutex
10
	carts map[string]string
11
}
12
13
func New_Session_Store() *Session_Store {
14
	return &Session_Store{
15
		carts: make(map[string]string),
16
	}
17
}
18
19
func (s *Session_Store) Get_Cart(session_id string) string {
20
	s.mu.RLock()
21
	defer s.mu.RUnlock()
22
	return s.carts[session_id]
23
}
24
25
func (s *Session_Store) Set_Cart(session_id, cart_json string) {
26
	s.mu.Lock()
27
	defer s.mu.Unlock()
28
	s.carts[session_id] = cart_json
29
}
30
31
type App_Context struct {
32
	DB            *sql.DB
33
	Session_Store *Session_Store
34
}
35
36
func New_App_Context(db *sql.DB) *App_Context {
37
	return &App_Context{
38
		DB:            db,
39
		Session_Store: New_Session_Store(),
40
	}
41
}