func GetTopics(page int, where string, order string, url string) ([]models.Topic, *Pagination) { if page < 1 { page = 1 url = url[:strings.Index(url, "=")+1] + "1" } var topics []models.Topic var pagination *Pagination if page > models.CachePageSize { // 当前页超出缓存页,从数据库取数据 topics, pagination = queryDb(page, where, order, url) } else { // 当前页在缓存中,从缓存中取数据 if err := cache.Get("topics"+url, &topics); err != nil { // 缓存中没有找到,查数据库 topics, pagination = queryDb(page, where, order, url) if len(topics) == 0 { return topics, pagination } // 写入缓存 go cache.Set("topics"+url, topics, cache.FOREVER) go cache.Set("pagination"+url, pagination, cache.FOREVER) } else { // 从缓存中取分页 if err := cache.Get("pagination"+url, &pagination); err != nil { revel.ERROR.Println(err) } } } return topics, pagination }
// VerifySession checks stored session id against stored value func VerifySession(sid string) bool { var session Session if err := cache.Get(SessionId+SESSION_KEY, &session); err != nil { return false } return sid == session.Id }
func getCategories() []models.Category { var categories []models.Category if err := cache.Get("categories", &categories); err != nil { engine.Find(&categories) go cache.Set("categories", categories, cache.FOREVER) } return categories }
func getProducts() []models.Product { var products []models.Product if err := cache.Get("products", &products); err != nil { engine.Find(&products) go cache.Set("products", products, cache.FOREVER) } return products }
func (c App) Index() revel.Result { var widgets []models.Widget if err := cache.Get("widgets", &widgets); err != nil { log.Printf("Cache not working or first time request") widgets = models.GetWidgets() go cache.Set("widgets", widgets, cache.DEFAULT) } return c.Render(widgets) }
func getReplies(id int64) []models.Reply { var replies []models.Reply str := strconv.Itoa(int(id)) if err := cache.Get("replies"+str, &replies); err != nil { engine.Where("topic_id = ?", id).Find(&replies) go cache.Set("replies"+str, replies, cache.FOREVER) } return replies }
func (c Restaurants) Index(restaurantName string) revel.Result { c.Validation.Required(restaurantName).Message("Restaurant name is required.") c.Validation.MinSize(restaurantName, 3).Message("Restaurant name is not long enough.") if c.Validation.HasErrors() { c.Validation.Keep() c.FlashParams() return c.Redirect(App.Index) } var restaurants []*models.Restaurant if err := cache.Get("restaurants_by_name_"+strings.ToLower(restaurantName), &restaurants); err != nil { restaurants = c.loadRestaurants(restaurantName) go cache.Set("restaurants_by_name_"+strings.ToLower(restaurantName), restaurants, 1*time.Minute) } return c.Render(restaurantName, restaurants) }
// 帖子详细 func (c Topic) Show(id int64) revel.Result { topic := new(models.Topic) str := strconv.Itoa(int(id)) if err := cache.Get("topic"+str, &topic); err != nil { has, _ := engine.Id(id).Get(topic) if !has { return c.NotFound("帖子不存在") } go cache.Set("topic"+str, topic, cache.FOREVER) } topic.Hits += 1 engine.Id(id).Cols("hits").Update(topic) replies := getReplies(id) title := topic.Title return c.Render(title, topic, replies) }