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 }
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 }
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 }
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 }
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) }