Exemplo n.º 1
0
func UpdateUser(name string, user *User) error {
	cache := common.GetCacheContainer("user")
	info := cache.GetCache(name)
	if info != nil {
		cache.UpdateCache(name, user)
		return nil
	} else {
		return updateUserDB(user)
	}
}
Exemplo n.º 2
0
func DelUser(name string) (bool, error) {
	user, err := GetUser(name)
	if user != nil {
		if g_UserKey2Name != nil {
			delete(g_UserKey2Name, user.AuthKey)
		}
	}
	cache := common.GetCacheContainer("user")
	cache.DelCache(name)
	result, err := g_DelUserStmt.Exec(name)
	n, _ := result.RowsAffected()
	return n > 0, err
}
Exemplo n.º 3
0
func GetUser(name string) (*User, error) {
	cache := common.GetCacheContainer("user")
	var user *User = nil
	var err error
	info := cache.GetCache(name)
	if info == nil {
		_user := &User{}
		row := preScan(_user)
		err = g_QueryUserStmt.QueryRow(name).Scan(row...)
		//fmt.Printf("load from db %+v,%d,%v\n", _user, _user.TodayCSModeData, err)
		if err == nil {
			user = _user
			cache.AddCache(name, _user, CacheTime)
			user.OnLogin()
		}
		if err == sql.ErrNoRows {
			err = nil
		}
	} else {
		user = info.(*User)
		user.SetCacheTime(-1)
	}
	return user, err
}
Exemplo n.º 4
0
func dnsLoop() {
	for {
		select {
		case info := <-checkDns:
			cache := common.GetCacheContainer("dns")
			cacheInfo := cache.GetCache(info.host)
			if cacheInfo == nil {
				cache.AddCache(info.host, &dnsInfo{Queue: []*dnsQueryReq{info}, Status: "querying"}, int64(*dnsCacheNum*60))
				go func() {
					back := &dnsQueryBack{host: info.host}
					//log.Println("try dial", info.url)
					s_conn, err := net.DialTimeout(info.reqtype, info.url, 30*time.Second)
					//log.Println("try dial", info.url, "ok")
					if err != nil {
						back.status = "queryfail"
						back.err = err
					} else {
						back.status = "queryok"
						back.conn = s_conn
					}
					checkDnsRes <- back
				}()
			} else {
				_cacheInfo := cacheInfo.(*dnsInfo)
				//log.Println("on trigger", info.host, _cacheInfo.GetCacheTime(), len(_cacheInfo.Queue))
				switch _cacheInfo.Status {
				case "querying":
					_cacheInfo.Queue = append(_cacheInfo.Queue, info)
					//cache.UpdateCache(info.host, _cacheInfo)
					cacheInfo.SetCacheTime(-1)
				case "queryok":
					cacheInfo.SetCacheTime(-1)
					go func() {
						info.c <- &dnsQueryRes{ip: _cacheInfo.Ip}
					}()
				}
				//url = cacheInfo.(*dnsInfo).Ip + fmt.Sprintf(":%d", info.port)
			}
		case info := <-checkDnsRes:
			cache := common.GetCacheContainer("dns")
			cacheInfo := cache.GetCache(info.host)
			if cacheInfo != nil {
				_cacheInfo := cacheInfo.(*dnsInfo)
				_cacheInfo.Status = info.status
				switch info.status {
				case "queryfail":
					for _, _info := range _cacheInfo.Queue {
						c := _info.c
						go func() {
							c <- &dnsQueryRes{err: info.err}
						}()
					}
					cache.DelCache(info.host)
				case "queryok":
					log.Println("add host", info.host, "to dns cache")
					_cacheInfo.Ip, _, _ = net.SplitHostPort(info.conn.RemoteAddr().String())
					_cacheInfo.SetCacheTime(-1)
					//log.Println("process the queue of host", info.host, len(_cacheInfo.Queue))
					conn := info.conn
					for _, _info := range _cacheInfo.Queue {
						c := _info.c
						go func() {
							c <- &dnsQueryRes{ip: _cacheInfo.Ip, conn: conn}
						}()
						conn = nil
					}
					_cacheInfo.Queue = []*dnsQueryReq{}
				}
				//cache.UpdateCache(info.host, _cacheInfo)
			}
		}
	}
}