示例#1
0
文件: get.go 项目: bmbstack/go-user
func Get(userId int64) (user *User, err error) {
	user = &User{}

	if err = getFromCache(userId, user); err == nil {
		return
	}
	if err != ErrNotFound {
		return
	}

	// 缓存没有找到, 从数据库读取
	stmt, err := db.GetStmt("select * from user where id=?")
	if err != nil {
		return
	}
	if err = stmt.Get(user, userId); err != nil {
		if err == sql.ErrNoRows {
			err = ErrNotFound
		}
		return
	}

	err = putToCache(user)
	return
}
示例#2
0
文件: set.go 项目: jack0888/go-user
func setPassword(userId int64, password, salt []byte) (err error) {
	stmt, err := db.GetStmt("update user set password=?, password_tag=?, salt=? where id=? and verified=1")
	if err != nil {
		return
	}

	_, err = stmt.Exec(password, random.NewRandomEx(), salt, userId)
	return
}
示例#3
0
文件: set.go 项目: jack0888/go-user
func setNickname(userId int64, nickname string) (err error) {
	stmt, err := db.GetStmt("update user set nickname=? where id=? and verified=1")
	if err != nil {
		return
	}

	_, err = stmt.Exec(nickname, userId)
	return
}
示例#4
0
文件: get.go 项目: bmbstack/go-user
func GetByWechat(openid string) (user *User, err error) {
	stmt, err := db.GetStmt("select A.id, A.nickname, A.bind_types, A.password, A.password_tag, A.salt, A.create_time, A.verified from user as A, user_wechat as B where B.openid=? and A.id=B.user_id and A.verified=B.verified and A.bind_types&?<>0")
	if err != nil {
		return
	}

	user = &User{}
	if err = stmt.Get(user, openid, BindTypeWechat); err != nil {
		if err == sql.ErrNoRows {
			err = ErrNotFound
		}
		return
	}
	return
}
示例#5
0
文件: cache.go 项目: bmbstack/go-user
func syncToCache(userId int64) (err error) {
	stmt, err := db.GetStmt("select * from user where id=?")
	if err != nil {
		return
	}

	var user User
	if err = stmt.Get(&user, userId); err != nil {
		if err == sql.ErrNoRows {
			err = ErrNotFound
		}
		return
	}

	return putToCache(&user)
}