Ejemplo n.º 1
0
// Get the content of a specified page and check if it exists
func getPage(identifier string, r *http.Request) (bool, Pages) {
	// TODO: Compile page list to make a check without lookup in the datastore
	c := appengine.NewContext(r)

	// Check if we have the item in the cache
	cachedItem, cacheStatus := cache.GetCache(r, "getPage-"+identifier)
	if cacheStatus == true {
		var pages Pages
		pPages := bytes.NewBuffer(cachedItem)
		decPages := gob.NewDecoder(pPages)
		decPages.Decode(&pages)
		return true, pages
	}

	// We don't have it in the cache, do a lookup and store it to the cache
	var page Pages
	key := datastore.NewKey(c, "Pages", identifier, 0, nil)
	if err := datastore.Get(c, key, &page); err != nil {
		return false, Pages{}
	}

	// Found it!
	cachedPage := Pages{Identifier: key.StringID(), URL: page.URL, Title: page.Title, ContentString: template.HTML(page.Content)}

	// Add it to the cache
	mPages := new(bytes.Buffer) //initialize a *bytes.Buffer
	encPages := gob.NewEncoder(mPages)
	encPages.Encode(cachedPage)
	cache.AddCache(r, "getPage-"+identifier, mPages.Bytes())

	return true, cachedPage
}
Ejemplo n.º 2
0
// This functions get all pages
func getAllPages(w http.ResponseWriter, r *http.Request) []Pages {
	// Check if we have the item in the cache
	cachedItem, cacheStatus := cache.GetCache(r, "getAllPages")
	if cacheStatus == true {
		var pages []Pages
		pPages := bytes.NewBuffer(cachedItem)
		decPages := gob.NewDecoder(pPages)
		decPages.Decode(&pages)
		return pages
	}

	// We don't have it in the cache, make a query and store it to the cache
	c := appengine.NewContext(r)
	q := datastore.NewQuery("Pages")
	var pages []Pages
	keys, err := q.GetAll(c, &pages)
	if err != nil {
		errorHandler.Error(w, r, err.Error())
		return []Pages{}
	}
	models := make([]Pages, len(pages))
	for i := 0; i < len(pages); i++ {
		models[i].Identifier = keys[i].StringID()
		models[i].URL = pages[i].URL
		models[i].Title = pages[i].Title
	}

	// Add it to the cache
	mModels := new(bytes.Buffer) //initialize a *bytes.Buffer
	encModels := gob.NewEncoder(mModels)
	encModels.Encode(models)
	cache.AddCache(r, "getAllPages", mModels.Bytes())

	return models
}
Ejemplo n.º 3
0
func getPost(w http.ResponseWriter, r *http.Request, Seq int) Post {
	cachedItem, cacheStatus := cache.GetCache(r, "post-"+strconv.Itoa(Seq))
	if cacheStatus == true {
		var post Post
		buffPost := bytes.NewBuffer(cachedItem)
		decPost := gob.NewDecoder(buffPost)
		decPost.Decode(&post)
		return post
	}

	c := appengine.NewContext(r)
	q := datastore.NewQuery("Post").Filter("Sequence=", Seq)
	var p []Post
	q.GetAll(c, &p)

	if p != nil {
		post := p[0]
		post.ContentString = string(post.Content)
		// Add Cache
		mPost := new(bytes.Buffer)
		encPost := gob.NewEncoder(mPost)
		encPost.Encode(post)
		cache.AddCache(r, "post-"+strconv.Itoa(Seq), mPost.Bytes())

		return post
	}
	var nilPost Post
	return nilPost
}
Ejemplo n.º 4
0
func getCount(w http.ResponseWriter, r *http.Request) int64 {
	cachedItem, cacheStatus := cache.GetCache(r, "Counter")
	if cacheStatus == true {
		var counter Counter
		buffCount := bytes.NewBuffer(cachedItem)
		decCount := gob.NewDecoder(buffCount)
		decCount.Decode(&counter)
		return counter.Count
	}

	c := appengine.NewContext(r)
	var counter Counter
	key := datastore.NewKey(c, "Counter", "", 1, nil)
	datastore.Get(c, key, &counter)

	if counter.Count != 0 {
		// Add Cache
		mCount := new(bytes.Buffer)
		encCount := gob.NewEncoder(mCount)
		encCount.Encode(counter)
		cache.AddCache(r, "Counter", mCount.Bytes())

		return counter.Count
	}

	return 0
}
Ejemplo n.º 5
0
//Get the menu
// TODO: It's ugly to have it here, maybe it should on another place...
func getMenu(r *http.Request) []Navigation {
	var navigation []Navigation

	// Check if we have the item in the cache
	cachedItem, cacheStatus := cache.GetCache(r, "getMenu")
	if cacheStatus == true {
		pNavigation := bytes.NewBuffer(cachedItem)
		decNavigation := gob.NewDecoder(pNavigation)
		decNavigation.Decode(&navigation)
	} else {
		// Get it and cache
		c := appengine.NewContext(r)
		q := datastore.NewQuery("Menu").Order("Order")
		var navigation []Navigation
		q.GetAll(c, &navigation)

		mModels := new(bytes.Buffer) //initialize a *bytes.Buffer
		encModels := gob.NewEncoder(mModels)
		encModels.Encode(navigation)
		cache.AddCache(r, "getMenu", mModels.Bytes())
	}

	for key := range navigation {
		if navigation[key].NavLink == r.URL.Path {
			navigation[key].Active = true
		}
	}

	return navigation
}
Ejemplo n.º 6
0
/*
 * Returns the content of the robots.txt
 */
func getRobot(r *http.Request) []byte {
	// Check if we have the robot in the cache
	cachedItem, cacheStatus := cache.GetCache(r, "getRobot")
	if cacheStatus == true {
		return cachedItem
	}

	// Let's query
	c := appengine.NewContext(r)
	key := datastore.NewKey(c, "Robots", "Robots.txt", 0, nil)
	var robots Robots
	datastore.Get(c, key, &robots)

	cache.AddCache(r, "getRobot", robots.Content)

	return robots.Content
}