| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677 |
- package log
- import (
- "context"
- "log-server/global"
- )
- type Computer struct {
- Id uint `json:"id"`
- PcCode string `json:"pc_code"`
- User string `json:"user"`
- Status int `json:"status"`
- CreateTime string `json:"create_time"` // 创建时间
- UpdateTime string `json:"update_time"` // 更新时间
- Supplier string `json:"supplier"` //电脑供应商
- }
- func (Computer) TableName() string {
- return "computer"
- }
- const OnlinePcCode = "Online:PcCode"
- func (c *Computer) OnlinePcCodeCache() (mps map[string]string, err error) {
- ctx := context.Background()
- isNull, _ := global.GVA_REDIS.Exists(ctx, OnlinePcCode).Result()
- if isNull != 0 {
- mps, err = global.GVA_REDIS.HGetAll(ctx, OnlinePcCode).Result()
- return
- }
- var computers []*Computer
- err = global.GVA_DB.Where("status = ?", 1).Find(&computers).Error
- if err != nil {
- return
- }
- if len(computers) == 0 {
- return
- }
- var mp = map[string]string{}
- for _, pc := range computers {
- mp[pc.PcCode] = pc.User
- }
- err = global.GVA_REDIS.HSet(ctx, OnlinePcCode, mp).Err()
- if err != nil {
- return
- }
- return mp, err
- }
- // 更新单条缓存数据
- func (c *Computer) UpdateOnlinePcCodeCache(pcCode, user string) (err error) {
- ctx := context.Background()
- err = global.GVA_REDIS.HSet(ctx, OnlinePcCode, pcCode, user).Err()
- return
- }
- // 删除单条缓存数据
- func (c *Computer) DelOnlinePcCodeCache(pcCode string) (err error) {
- ctx := context.Background()
- err = global.GVA_REDIS.HDel(ctx, OnlinePcCode, pcCode).Err()
- return
- }
- // 清空缓存数据
- func (c *Computer) DelAllOnlinePcCodeCache() (err error) {
- ctx := context.Background()
- keys, _ := global.GVA_REDIS.HKeys(ctx, OnlinePcCode).Result()
- err = global.GVA_REDIS.HDel(ctx, OnlinePcCode, keys...).Err()
- return
- }
- func (c *Computer) ExistsPcCodeCache(pcCode string) bool {
- ctx := context.Background()
- b, _ := global.GVA_REDIS.HExists(ctx, OnlinePcCode, pcCode).Result()
- return b
- }
|