WxUserCache.go 1014 Bytes
Newer Older
Ford committed
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
package models

import "sync"

// 定义微信用户的结构
type WxUser struct {
	Username     string // 用户名
	Avatar       string // 头像链接
	PhoneNumber  string // 手机号
	UserID       string // 用户ID
	WxCode       string //微信登录的code
	WxCodeStatus string // 登录校验状态
	OpenId       string
	SessionKey   string
}

// 定义一个结构来实现缓存
type WxUserCache struct {
	mu    sync.RWMutex
	users map[string]WxUser
}

// NewCache 创建新缓存
func NewCache() *WxUserCache {
	return &WxUserCache{
		users: make(map[string]WxUser),
	}
}

// Set 添加或更新用户信息
func (c *WxUserCache) Set(key string, user WxUser) {
	c.mu.Lock()
	c.users[key] = user
	c.mu.Unlock()
}

// Get 通过key获取用户信息
func (c *WxUserCache) Get(key string) (WxUser, bool) {
	c.mu.RLock()
	user, ok := c.users[key]
	c.mu.RUnlock()
	return user, ok
}

// Delete 删除用户信息
func (c *WxUserCache) Delete(key string) {
	c.mu.Lock()
	delete(c.users, key)
	c.mu.Unlock()
}