func (uc UpdateCache) Run() { revel.WARN.Print("update cache...") for page := 1; page <= models.CachePageSize; page++ { cache.Delete("topics" + routes.Topic.Index(page)) c.GetTopics(page, "", "created", routes.Topic.Index(page)) time.Sleep(time.Second) cache.Delete("topics" + routes.Topic.Hot(page)) c.GetTopics(page, "", "hits", routes.Topic.Hot(page)) time.Sleep(time.Second) cache.Delete("topics" + routes.Topic.Good(page)) c.GetTopics(page, "good = true", "created", routes.Topic.Good(page)) time.Sleep(time.Second) } }
func (c Product) EditPost(id int64, product models.Product) revel.Result { var tmp models.Product has, _ := engine.Id(id).Get(&tmp) if !has { return c.NotFound("案例不存在") } product.Validate(c.Validation) file, header, err := c.Request.FormFile("image") if err == nil { defer file.Close() if ok := checkImageExt(c.Validation, &file, header, "image"); ok { fileName := uuidFileName(header.Filename) err, ret := qiniuUploadImage(&file, fileName) if err != nil { c.Flash.Error("上传到七牛出错,请检查七牛配置。") return c.Redirect(routes.Product.Edit(id)) } else { if tmp.Image != "" { qiniuDeleteImage(tmp.Image) } product.Image = ret.Key } } } else { product.Image = tmp.Image } if c.Validation.HasErrors() { c.Validation.Keep() c.FlashParams() return c.Redirect(routes.Product.Edit(id)) } // 强制更新允许空值的字段 aff, _ := engine.Id(id).MustCols("site", "repository").Update(&product) if aff > 0 { c.Flash.Success("编辑案例成功") cache.Delete("products") } else { c.Flash.Error("编辑案例失败") return c.Redirect(routes.Product.Edit(id)) } return c.Redirect(routes.Product.Index()) }
func (c App) Update() revel.Result { var widget models.Widget b, err := ioutil.ReadAll(c.Request.Body) if err != nil { log.Fatal("Problem reading the body: " + err.Error()) } if err := json.Unmarshal(b, &widget); err != nil { log.Fatal("Problem with Unmarshal: " + err.Error()) } models.SetWidget(widget) go cache.Delete("widgets") return c.RenderJson(widget) }
func (c Admin) NewCategoryPost(category models.Category) revel.Result { category.Validate(c.Validation) if c.Validation.HasErrors() { c.Validation.Keep() c.FlashParams() return c.Redirect(routes.Admin.NewCategory()) } aff, _ := engine.Insert(&category) if aff > 0 { c.Flash.Success("添加分类成功") cache.Delete("categories") } else { c.Flash.Error("添加分类失败") return c.Redirect(routes.Admin.NewCategory()) } return c.Redirect(routes.Admin.ListCategory()) }
func (c Product) NewPost(product models.Product) revel.Result { product.Validate(c.Validation) file, header, err := c.Request.FormFile("image") if err == nil { defer file.Close() if ok := checkImageExt(c.Validation, &file, header, "image"); ok { fileName := uuidFileName(header.Filename) err, ret := qiniuUploadImage(&file, fileName) if err != nil { c.Flash.Error("上传到七牛出错,请检查七牛配置。") return c.Redirect(routes.Product.New()) } else { product.Image = ret.Key } } } else { err := &revel.ValidationError{ Message: "字段不能为空", Key: "image", } c.Validation.Errors = append(c.Validation.Errors, err) } if c.Validation.HasErrors() { c.Validation.Keep() c.FlashParams() return c.Redirect(routes.Product.New()) } product.User = models.User{Id: c.user().Id} aff, _ := engine.Insert(&product) if aff > 0 { c.Flash.Success("提交案例成功") cache.Delete("products") } else { c.Flash.Error("提交案例失败") } return c.Redirect(routes.Product.Index()) }
func InvalidateSession() { go cache.Delete(SessionId + SESSION_KEY) }