// Addmodel.User insert a new model.User into database and returns // last inserted Id on success. func AddUser(m *model.User) (id int64, err error) { o := orm.NewOrm() o.Using(model.DbName) m.Ctime = time.Now().Unix() id, err = o.Insert(m) if err == nil { cache.AddUser(m) } return }
// Updatemodel.User updates model.User by Id and returns error if // the record to be updated doesn't exist func UpdateUserById(m *model.User) (err error) { o := orm.NewOrm() o.Using(model.DbName) v := model.User{Id: m.Id} // ascertain id exists in the database err = o.Read(&v) if err != nil { return } m.Password = v.Password m.Ctime = time.Now().Unix() if num, err := o.Update(m); err == nil { fmt.Println("Number of records updated in database:", num) //修改缓存 cache.AddUser(m) } return }