/*
 * @description: redis 操作相关
 * @Author: CP
 * @Date: 2021-01-27 11:35:58
 * @FilePath: \construction_management\lib\redis.go
 */
package lib

import (
	"log"
	"strconv"

	"go.mod/comm"
	"go.mod/datasource"
)

type Redis struct {
}

func NewRedis() *Redis {
	return &Redis{}
}

// 从redis获得标段ID
func (s *Redis) GetBidsectionIdByCache(key string) int {
	rds := datasource.InstanceCache()
	// 读取缓存
	rs, err := rds.Do("GET", key)
	if err != nil {
		log.Println("redis.GetBidsectionId GET key=", key, ", error=", err)
		return 0
	}

	str := comm.GetString(rs, "")
	num, err := strconv.Atoi(str)
	if err != nil {
		return 0
	} else {
		return int(num)
	}
}

// 设置标段ID的redis
func (s *Redis) SetBidsectionIdByCache(key string, value interface{}) {
	// 集群模式，redis缓存
	rds := datasource.InstanceCache()
	// 更新缓存
	_, err := rds.Do("SET", key, value)
	if err != nil {
		log.Println("redis.SetBidsectionId SET key=", key,
			", value=", value, ", error=", err)
	}
}
