Пример #1
0
func GetBuriggies(c appengine.Context, count int) (buriggies []*Buriggie, err error) {

	var gg []*Buriggie

	var cacheKey = "buriggies.list." + strconv.Itoa(count)

	// Get the item from the memcache
	if item, err := memcache.Get(c, cacheKey); err == memcache.ErrCacheMiss {
		// cache miss.. ADD to cache
		q := datastore.NewQuery("Greeting").Order("-Date").Limit(count)
		_, err = q.GetAll(c, &gg)

		qjs, _ := json.Marshal(gg)

		cache_duration, _ := time.ParseDuration("1d")
		// Create an Item - cache for a day
		item := &memcache.Item{
			Key:        cacheKey,
			Value:      qjs,
			Expiration: cache_duration,
		}

		if err := memcache.Add(c, item); err != nil {
			c.Errorf("error setting item: %v", err)
		}

	} else if err != nil {
		c.Errorf("error getting item: %v", err)
	} else {
		err = json.Unmarshal(item.Value, &gg)
	}

	return gg, err
}
Пример #2
0
func getCached(c appengine.Context, key string, missing func(appengine.Context, string) (*bytes.Buffer, error)) (*memcache.Item, error) {

	item, err := memcache.Get(c, key)

	if err == memcache.ErrCacheMiss {
		c.Infof("item not in the cache: %v", key)

		result, err := missing(c, key)

		if err != nil {
			return nil, err
		}

		item = &memcache.Item{
			Key:   key,
			Value: result.Bytes(),
		}

		if err := memcache.Add(c, item); err == memcache.ErrNotStored {
			c.Warningf("item with key %q already exists", item.Key)
		} else if err != nil {
			return item, err
		}
	} else if err != nil {
		return item, err
	} else {
		c.Infof("Cache hit: %v", key)
	}

	return item, nil
}
Пример #3
0
func signInTwitterHandler(w http.ResponseWriter, r *http.Request) {

	c := appengine.NewContext(r)
	id := r.FormValue("id")
	if id == "" {
		serveError(c, w, errors.New("Missing ID parameter"))
		return
	}

	conf := &tweetlib.Config{
		ConsumerKey:    appConfig.TwitterConsumerKey,
		ConsumerSecret: appConfig.TwitterConsumerSecret,
		Callback:       "http://" + appConfig.AppHost + "/twitter?action=temp&id=" + id}
	tok := &tweetlib.Token{}
	tr := &tweetlib.Transport{Config: conf,
		Token:     tok,
		Transport: &urlfetch.Transport{Context: c}}

	tt, err := tr.TempToken()
	if err != nil {
		c := appengine.NewContext(r)
		serveError(c, w, err)
		c.Errorf("%v", err)
		return
	}
	item := &memcache.Item{
		Key:   tt.Token,
		Value: []byte(tt.Secret),
	}
	// Add the item to the memcache, if the key does not already exist
	memcache.Add(c, item)

	http.Redirect(w, r, tt.AuthURL(), http.StatusFound)
}
Пример #4
0
func cacheSafeSet(c appengine.Context, item *memcache.Item) error {
	var buf bytes.Buffer
	err := gob.NewEncoder(&buf).Encode(item.Object)
	if err != nil {
		return err
	}

	swap := item.Value != nil
	item.Value = buf.Bytes()

	if swap {
		err = memcache.CompareAndSwap(c, item)
		switch err {
		case memcache.ErrCASConflict:
			// OK, cache item set by another request
			return nil
		case memcache.ErrNotStored:
			// Item expired. Try adding below.
		default:
			return err
		}
	}

	err = memcache.Add(c, item)
	if err == memcache.ErrNotStored {
		// OK, cache item set by another request
		err = nil
	}
	return err
}
Пример #5
0
Файл: app.go Проект: 0x7cc/rsc
func updateCacheTime(c appengine.Context, seq int64) {
	const key = rootMemcacheKey
	bseq := []byte(strconv.FormatInt(seq, 10))
	for tries := 0; tries < 10; tries++ {
		item, err := memcache.Get(c, key)
		if err != nil {
			c.Infof("memcache.Get %q: %v", key, err)
			err = memcache.Add(c, &memcache.Item{Key: key, Value: bseq})
			if err == nil {
				c.Infof("memcache.Add %q %q ok", key, bseq)
				return
			}
			c.Infof("memcache.Add %q %q: %v", key, bseq, err)
		}
		v, err := strconv.ParseInt(string(item.Value), 10, 64)
		if err != nil {
			c.Criticalf("memcache.Get %q = %q (%v)", key, item.Value, err)
			return
		}
		if v >= seq {
			return
		}
		item.Value = bseq
		err = memcache.CompareAndSwap(c, item)
		if err == nil {
			c.Infof("memcache.CAS %q %d->%d ok", key, v, seq)
			return
		}
		c.Infof("memcache.CAS %q %d->%d: %v", key, v, seq, err)
	}
	c.Criticalf("repeatedly failed to update root key")
}
Пример #6
0
func MemoAdd(w http.ResponseWriter, r *http.Request) {
	c := appengine.NewContext(r)

	if err := r.ParseForm(); err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}

	key := r.PostFormValue("key")
	value := r.PostFormValue("value")
	c.Debugf("key = %s, value = %s", key, value)

	if key == "" || value == "" {
		http.Error(w, "key or value param error", http.StatusInternalServerError)
		return
	}

	// 有効期限1分
	item := &memcache.Item{
		Key:        key,
		Value:      bytes.NewBufferString(value).Bytes(),
		Expiration: (time.Minute * 1),
	}
	if err := memcache.Add(c, item); err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
	} else {
		http.Redirect(w, r, "/", http.StatusSeeOther)
	}
}
Пример #7
0
func sendReminder(c appengine.Context, date time.Time) {
	addr := "*****@*****.**"

	tag := fmt.Sprintf("diaryentry%dtag", rand.Int63())

	item := &memcache.Item{
		Key:   tag,
		Value: []byte(date.Format(time.RFC850)),
	}

	// Add the item to the memcache, if the key does not already exist
	if err := memcache.Add(c, item); err == memcache.ErrNotStored {
		c.Infof("item with key %q already exists", item.Key)
	} else if err != nil {
		c.Errorf("error adding item: %v", err)
	}

	msg := &mail.Message{
		Sender:  "Automatic Diary <*****@*****.**>",
		To:      []string{addr},
		Subject: "Entry reminder",
		Body:    fmt.Sprintf(reminderMessage, tag),
	}
	if err := mail.Send(c, msg); err != nil {
		c.Errorf("Couldn't send email: %v", err)
		return
	}
	c.Infof("Reminder mail sent for %v", date)
	c.Infof("body: %v", msg.Body)
}
Пример #8
0
// Lista entidades
func ListEnt(c appengine.Context, ent string) *[]Entidad {
	estados := make([]Entidad, 0, 32)
	if item, err := memcache.Get(c, "estados"); err == memcache.ErrCacheMiss {
		q := datastore.NewQuery("Entidad")
		if _, err := q.GetAll(c, &estados); err != nil {
			return nil
		}
		b, _ := json.Marshal(estados)
		item := &memcache.Item{
			Key:   "estados",
			Value: b,
		}
		if err := memcache.Add(c, item); err == memcache.ErrNotStored {
			c.Errorf("memcache.Add Entidad : %v", err)
		}
	} else {
		//c.Infof("Memcache activo: %v", item.Key)
		if err := json.Unmarshal(item.Value, &estados); err != nil {
			c.Errorf("Memcache Unmarshalling item: %v", err)
		}
	}
	for i, _ := range estados {
		if ent == estados[i].CveEnt {
			estados[i].Selected = `selected`
		}
	}
	return &estados
}
Пример #9
0
func cat(w http.ResponseWriter, r *http.Request) {
	c := appengine.NewContext(r)
	w.Header().Set("Content-Type", "application/json")
	switch r.Method {
	case "GET":
		var jsonResult []byte
		if item, err := memcache.Get(c, CAT); err == memcache.ErrCacheMiss {
			q := datastore.NewQuery("Package").Filter("Accepted =", true)
			var packages []*Package
			if _, err := q.GetAll(c, &packages); err != nil {
				http.Error(w, err.Error(), http.StatusInternalServerError)
				return
			}
			var categories Categories
			for _, pkg := range packages {
				if strings.TrimSpace(pkg.Category) != "" {
					categories = categories.Add(pkg.Category)
				}
			}
			if jsonResult, err = json.Marshal(categories); err != nil {
				http.Error(w, err.Error(), http.StatusInternalServerError)
				return
			}
			memcache.Add(c, &memcache.Item{Key: CAT, Value: jsonResult})
		} else {
			jsonResult = item.Value
		}
		w.Write(jsonResult)
	}
}
Пример #10
0
func ListCat(c appengine.Context, IdCat int) *[]Categoria {
	var categorias []Categoria
	if item, err := memcache.Get(c, "categorias"); err == memcache.ErrCacheMiss {
		q := datastore.NewQuery("Categoria")
		//n, _ := q.Count(c)
		//cats := make([]Categoria, 0, n)
		if _, err := q.GetAll(c, &categorias); err != nil {
			return nil
		}
		b, _ := json.Marshal(categorias)
		item := &memcache.Item{
			Key:   "categorias",
			Value: b,
		}
		if err := memcache.Add(c, item); err == memcache.ErrNotStored {
			c.Errorf("memcache.Add Categoria : %v", err)
		}
	} else {
		c.Infof("Memcache activo: %v", item.Key)
		if err := json.Unmarshal(item.Value, &categorias); err != nil {
			c.Errorf("Memcache Unmarchalling item: %v", err)
		}
	}
	for i, _ := range categorias {
		if IdCat == categorias[i].IdCat {
			categorias[i].Selected = `selected`
		}
	}
	return &categorias
}
Пример #11
0
func SetTokenHandler(rw http.ResponseWriter, req *http.Request) {
	c := appengine.NewContext(req)
	authorizationCode := req.FormValue("code")

	transport := &oauth.Transport{
		Config: config,
		Transport: &urlfetch.Transport{
			Context: c,
		},
	}
	token, err := transport.Exchange(authorizationCode)
	if err != nil {
		fmt.Fprintf(rw, "%s", err.Error())
		return
	}

	// トークン情報の一時保存
	value, err := json.Marshal(token)
	if err != nil {
		fmt.Fprintf(rw, "%s", err.Error())
		return
	}
	item := &memcache.Item{
		Key:   tokenCacheKey,
		Value: value,
	}
	err = memcache.Add(c, item)
	if err != nil {
		fmt.Fprintf(rw, "%s", err.Error())
		return
	}

	fmt.Fprintf(rw, "Success.")
}
Пример #12
0
func (fi *FileInfo) CreateThumbnail(r io.Reader, c appengine.Context) (data []byte, err os.Error) {
	defer func() {
		if rec := recover(); rec != nil {
			log.Println(rec)
			// 1x1 pixel transparent GIf, bas64 encoded:
			s := "R0lGODlhAQABAIAAAP///////yH5BAEKAAEALAAAAAABAAEAAAICTAEAOw=="
			data, _ = base64.StdEncoding.DecodeString(s)
			fi.ThumbnailUrl = "data:image/gif;base64," + s
		}
		memcache.Add(c, &memcache.Item{
			Key:        string(fi.Key),
			Value:      data,
			Expiration: EXPIRATION_TIME,
		})
	}()
	img, _, err := image.Decode(r)
	check(err)
	if bounds := img.Bounds(); bounds.Dx() > THUMBNAIL_MAX_WIDTH ||
		bounds.Dy() > THUMBNAIL_MAX_HEIGHT {
		w, h := THUMBNAIL_MAX_WIDTH, THUMBNAIL_MAX_HEIGHT
		if bounds.Dx() > bounds.Dy() {
			h = bounds.Dy() * h / bounds.Dx()
		} else {
			w = bounds.Dx() * w / bounds.Dy()
		}
		img = resize.Resize(img, img.Bounds(), w, h)
	}
	var b bytes.Buffer
	err = png.Encode(&b, img)
	check(err)
	data = b.Bytes()
	fi.ThumbnailUrl = "data:image/png;base64," +
		base64.StdEncoding.EncodeToString(data)
	return
}
Пример #13
0
func municipios(w http.ResponseWriter, r *http.Request) {
	c := appengine.NewContext(r)
	_, ok := sess.IsSess(w, r, c)
	if ok {
		if item, err := memcache.Get(c, "msp_"+r.FormValue("CveEnt")); err == memcache.ErrCacheMiss {
			if entidad, err := model.GetEntidad(c, r.FormValue("CveEnt")); err == nil {
				if municipios, _ := entidad.GetMunicipios(c); err == nil {
					b, _ := json.Marshal(municipios)
					item := &memcache.Item{
						Key:   "msp_" + r.FormValue("CveEnt"),
						Value: b,
					}
					if err := memcache.Add(c, item); err == memcache.ErrNotStored {
						c.Infof("item with key %q already exists", item.Key)
					} else if err != nil {
						c.Errorf("Error memcache.Add Municipio : %v", err)
					}
					c.Infof("CveMun generado: %v", item.Key)
					htmlComboMuns(w, municipios, r.FormValue("CveMun"))
				}
			}
		} else {
			//c.Infof("Memcache activo: %v", item.Key)
			var municipios []model.Municipio
			if err := json.Unmarshal(item.Value, &municipios); err != nil {
				c.Errorf("error adding item: %v", err)
			}
			htmlComboMuns(w, &municipios, r.FormValue("CveMun"))
		}
	}
	return
}
Пример #14
0
func updateHandler(w http.ResponseWriter, r *http.Request) {
	c := gae.NewContext(r)
	g := goon.FromContext(c)
	if r.Header.Get("X-AppEngine-Cron") != "true" {
		http.NotFound(w, r)
		return
	}
	var ups []UpdateSchedule
	if cache, err := memcache.Get(c, "UpdateSchedule"); err == memcache.ErrCacheMiss {
		q := datastore.NewQuery("UpdateSchedule")
		if _, err := g.GetAll(q, &ups); err != nil {
			http.Error(w, err.Error(), http.StatusInternalServerError)
			return
		}
		if val, err := serialize(&ups); err == nil {
			item := memcache.Item{Key: "UpdateSchedule", Value: val, Expiration: time.Hour * 12}
			memcache.Add(c, &item)
		}
	} else if err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	} else {
		deserialize(cache.Value, &ups)
	}
	now := time.Now()
	for _, us := range ups {
		if us.IsUpdateTime(now) {
			if err := updateVillage(us, g); err != nil {
				c.Debugf("%v", err)
			}
			memcache.Delete(c, "UpdateSchedule")
		}
	}
	return
}
Пример #15
0
func abstractHandler(w http.ResponseWriter, r *http.Request) {
	// If the requested pic with the requested logo has already been generated, then use the cached version
	ctx := appengine.NewContext(r)
	originalImageUrl := r.URL.Query().Get("img")
	logoImageUrl := r.URL.Query().Get("logo")
	if logoImageUrl == "" {
		logoImageUrl = "brazz"
	}

	if MEMCACHE_ENABLED {
		item, err := memcache.Get(ctx, logoImageUrl+":"+originalImageUrl)
		if err != nil {
			ctx.Infof("Retrieved [%s:%s] from memcache\n", logoImageUrl, originalImageUrl)
			w.Header().Set("Content-Type", "image/png")
			w.Write(item.Value)
			return
		}
	}
	// Load the logo image
	logoImage, err := imageutils.FetchLogoImage(logoImageUrl)
	if err != nil {
		logoImage, err = imageutils.FetchImage(ctx, logoImageUrl, imageutils.TARGET_LOGO_WIDTH)
	}
	if err != nil {
		message := fmt.Sprintf("Unable to load logo image file: %s\n", err)
		ctx.Errorf(message)
		fmt.Fprintf(w, message)
		return
	}
	// Fetch the source image
	originalImage, err := imageutils.FetchImage(ctx, originalImageUrl, imageutils.TARGET_IMAGE_WIDTH)
	if err != nil {
		message := fmt.Sprintf("An error occurred: %s\n", err)
		ctx.Errorf(message)
		fmt.Fprintf(w, message)
		return
	}
	// Generate and return an image with logo over the source
	generatedImage := imageutils.GenerateImageWithLogo(originalImage, logoImage)
	generatedImageBytes, err := imageutils.ImageToBytes(generatedImage)
	if err != nil {
		message := fmt.Sprintf("An error occured: %s\n", err)
		ctx.Errorf(message)
		fmt.Fprintf(w, message)
		return
	}
	// Cache the generated image bytes before sending them in the HTTP response
	if MEMCACHE_ENABLED {
		item := &memcache.Item{
			Key:   logoImageUrl + ":" + originalImageUrl,
			Value: generatedImageBytes,
		}
		memcache.Add(ctx, item)
		ctx.Infof("Caching [%s:%s]\n", logoImageUrl, originalImageUrl)
	}
	ctx.Infof("Serving up [%s:%s]\n", logoImageUrl, originalImageUrl)
	w.Header().Set("Content-Type", "image/png")
	w.Write(generatedImageBytes)
}
Пример #16
0
/*
 * This exported functions returns (and if needed, generates) a token
 */
func GetToken(r *http.Request) string {
	c := appengine.NewContext(r)
	u := user.Current(c)

	// CSRF Token name
	csrfTokenUserID := "CSRF" + u.ID

	// Get the item from the memcache
	if item, err := memcache.Get(c, csrfTokenUserID); err == nil {
		// Return the token
		return string(item.Value)
	}
	// Get the item from the datastore
	key := datastore.NewKey(c, "SecurityToken", csrfTokenUserID, 0, nil)
	var datastoreSecurityToken SecurityToken
	if err := datastore.Get(c, key, &datastoreSecurityToken); err == nil {
		// Add it to memcache
		item := &memcache.Item{
			Key:   csrfTokenUserID,
			Value: []byte(datastoreSecurityToken.Token),
		}
		memcache.Add(c, item)
		// Return the token
		return datastoreSecurityToken.Token
	}

	// Generate a item
	csrfToken := makeRandomString(256)
	// Save it to the memcache
	item := &memcache.Item{
		Key:   csrfTokenUserID,
		Value: []byte(csrfToken),
	}
	memcache.Add(c, item)
	// Save it to the datastore
	SecurityToken := SecurityToken{
		Token: csrfToken,
	}
	datastore.Put(c, datastore.NewKey(c, "SecurityToken", u.ID, 0, nil), &SecurityToken)

	return csrfToken
}
Пример #17
0
func (session *Session) CheckCache(id string) bool {
	item := &memcache.Item{
		Key:   id,
		Value: []byte(""),
	}
	// Add the item to the memcache, if the key does not already exist
	if err := memcache.Add(session.context, item); err == memcache.ErrNotStored {
		return true
	}
	return false
}
Пример #18
0
// PostAndTimeByID returns a PostAndTime for the requested id
func PostAndTimeByID(c appengine.Context, id int64) PostAndTime {
	memcacheKey := "posts_and_time" + strconv.FormatInt(id, 10)
	c.Infof("cs253: Post and time by id memcache key is: %v ", memcacheKey)

	var postAndTime PostAndTime
	c.Infof("cs253: query cache first with memcache key")
	if item, err := memcache.Get(c, memcacheKey); err == memcache.ErrCacheMiss {
		c.Infof("cs253: item not in the cache :%v will perform query instead", err)

		key := datastore.NewKey(c, "Post", "", id, nil)
		c.Infof("cs253: key to use: v%", key)

		if err := datastore.Get(c, key, &postAndTime.Post); err != nil {
			c.Errorf("cs253: post not found : %v", err)
		}
		c.Infof("cs253: get time:")
		postAndTime.Cache_hit_time = time.Now()
		c.Infof("cs253: time is: %v", postAndTime.Cache_hit_time)
		// record information in cache for next time
		mCache := new(bytes.Buffer)
		encCache := gob.NewEncoder(mCache)
		c.Infof("cs253: New Encoder done")
		encCache.Encode(postAndTime)
		c.Infof("cs253: Encode done")

		postItem := &memcache.Item{
			Key:   memcacheKey,
			Value: mCache.Bytes(),
		}
		c.Infof("cs253: memcache Item ready")
		if err := memcache.Add(c, postItem); err == memcache.ErrNotStored {
			c.Errorf("cs253: postAndTime with key %q already exists", item.Key)
		} else if err != nil {
			c.Errorf("error adding item: %v", err)
		}
		c.Infof("cs253: item read to be returned")

	} else if err != nil {
		c.Errorf("cs253: Memcache error getting item: %v", err)
	} else {
		c.Infof("cs253: Memcache item found")

		pCache := bytes.NewBuffer(item.Value)
		c.Infof("cs253: New Buffer ready")
		decCache := gob.NewDecoder(pCache)
		c.Infof("cs253: decoder ready")
		decCache.Decode(&postAndTime)
		c.Infof("cs253: gob decoder of item done")
	}
	return postAndTime
}
Пример #19
0
func servePresentation(w http.ResponseWriter, r *http.Request) error {
	c := appengine.NewContext(r)
	importPath := r.URL.Path[1:]

	item, err := memcache.Get(c, importPath)
	if err == nil {
		writeHTMLHeader(w, 200)
		w.Write(item.Value)
		return nil
	} else if err != memcache.ErrCacheMiss {
		return err
	}

	c.Infof("Fetching presentation %s.", importPath)
	pres, err := gosrc.GetPresentation(httpClient(r), importPath)
	if err != nil {
		return err
	}

	ctx := &present.Context{
		ReadFile: func(name string) ([]byte, error) {
			if p, ok := pres.Files[name]; ok {
				return p, nil
			}
			return nil, presFileNotFoundError(name)
		},
	}

	doc, err := ctx.Parse(bytes.NewReader(pres.Files[pres.Filename]), pres.Filename, 0)
	if err != nil {
		return err
	}

	var buf bytes.Buffer
	if err := renderPresentation(&buf, importPath, doc); err != nil {
		return err
	}

	if err := memcache.Add(c, &memcache.Item{
		Key:        importPath,
		Value:      buf.Bytes(),
		Expiration: time.Hour,
	}); err != nil {
		return err
	}

	writeHTMLHeader(w, 200)
	_, err = w.Write(buf.Bytes())
	return err
}
Пример #20
0
/*
 * This function will save the data in the memcache and the datastore
 */
func AddCache(r *http.Request, identifier string, data []byte) {
	c := appengine.NewContext(r)

	// Add it to the memcache
	item := &memcache.Item{
		Key:   identifier,
		Value: data,
	}
	memcache.Add(c, item)

	// Add it to the Datastore
	cache := Cache{
		Cached: data,
	}
	datastore.Put(c, datastore.NewKey(c, "Cache", identifier, 0, nil), &cache)
}
Пример #21
0
func ShowEmpSucs(w http.ResponseWriter, r *http.Request) {
	var timetolive = 7200 //seconds
	c := appengine.NewContext(r)
	var b []byte
	cachename := "sucs_" + r.FormValue("id")
	if item, err := memcache.Get(c, cachename); err == memcache.ErrCacheMiss {
		emsucs := model.GetEmpSucursales(c, r.FormValue("id"))
		if emsucs == nil {
			w.WriteHeader(http.StatusNotFound)
			return
		}
		wssucs := make([]wssucursal, len(*emsucs), cap(*emsucs))
		for i, v := range *emsucs {
			wssucs[i].IdSuc = v.IdSuc
			wssucs[i].Sucursal = v.Nombre
			wssucs[i].DirCalle = v.DirCalle
			wssucs[i].DirCol = v.DirCol
			wssucs[i].DirEnt = v.DirEnt
			wssucs[i].Latitud, _ = strconv.ParseFloat(v.Geo1, 64)
			wssucs[i].Longitud, _ = strconv.ParseFloat(v.Geo2, 64)
		}
		sortutil.AscByField(wssucs, "Sucursal")
		b, _ = json.Marshal(wssucs)
		item := &memcache.Item{
			Key:        cachename,
			Value:      b,
			Expiration: time.Duration(timetolive) * time.Second,
		}
		if err := memcache.Add(c, item); err == memcache.ErrNotStored {
			c.Errorf("memcache.Add %v : %v", cachename, err)
			if err := memcache.Set(c, item); err == memcache.ErrNotStored {
				c.Errorf("Memcache.Set %v : %v", cachename, err)
			} else {
				c.Infof("memcached %v", cachename)
			}
		} else {
			c.Infof("memcached %v", cachename)
		}
	} else {
		c.Infof("memcache retrieve sucs_idemp : %v", r.FormValue("id"))
		b = item.Value
	}
	w.Header().Set("Content-Type", "application/json")
	w.Header().Set("Access-Control-Allow-Origin", "*")
	w.Write(b)
}
Пример #22
0
func (l *RateLimited) Run(key string, f func()) error {
	item := mc.Item{
		Key:        key,
		Value:      []byte{0xff},
		Expiration: l.rate,
	}

	if err := mc.Add(l.context, &item); err != mc.ErrNotStored {
		l.context.Debugf("Executing function (err: %v)", err)
		f()
		return nil
	} else {
		l.context.Infof("Rejected function (err: %v)", err)
		return ErrOverQuota
	}

}
Пример #23
0
// Store content in memcache, logging and discarding errors.
func storeInCache(c appengine.Context, key string, content []byte, ttl int) error {
	if appengine.IsDevAppServer() {
		c.Infof("This is a dev appserver, ignoring TTL of %d", ttl)
		ttl = 1
	}
	item := &memcache.Item{
		Key:        key,
		Value:      content,
		Expiration: time.Duration(ttl) * time.Second,
	}
	c.Infof("Caching contents of %s for %s", item.Key, item.Expiration)
	err := memcache.Add(c, item)
	if err != nil {
		c.Errorf("error adding %v to cache: %v", item, err)
	}
	return err
}
Пример #24
0
func ShowMicrositio(w http.ResponseWriter, r *http.Request) {
	w.Header().Set("Content-Type", "application/json")
	w.Header().Set("Access-Control-Allow-Origin", "*")
	var timetolive = 7200 //seconds
	c := appengine.NewContext(r)
	var b []byte
	var m micrositio
	cachename := "m_" + r.FormValue("id")
	if item, err := memcache.Get(c, cachename); err == memcache.ErrCacheMiss {
		if e := model.GetEmpresa(c, r.FormValue("id")); e != nil {
			m.IdEmp = e.IdEmp
			m.Name = e.Nombre
			if imgo := model.GetLogo(c, r.FormValue("id")); imgo != nil {
				m.IdImg = imgo.IdImg
				m.Url = imgo.Url
				m.Sp1 = imgo.Sp1
				m.Sp2 = imgo.Sp2
				m.Sp4 = imgo.Sp4
				m.Desc = imgo.Desc
			}
		}

		b, _ = json.Marshal(m)
		item := &memcache.Item{
			Key:        cachename,
			Value:      b,
			Expiration: time.Duration(timetolive) * time.Second,
		}
		if err := memcache.Add(c, item); err == memcache.ErrNotStored {
			c.Errorf("memcache.Add %v : %v", cachename, err)
			if err := memcache.Set(c, item); err == memcache.ErrNotStored {
				c.Errorf("Memcache.Set %v : %v", cachename, err)
			} else {
				c.Infof("memcached %v", cachename)
			}
		} else {
			c.Infof("memcached %v", cachename)
		}
	} else {
		//c.Infof("memcache retrieve m_idoft : %v", r.FormValue("id"))
		b = item.Value
	}
	w.Write(b)
}
Пример #25
0
func bucketNotificationHandler(w http.ResponseWriter, r *http.Request) error {
	c := appengine.NewContext(r)

	// Decode the name and bucket of the notification
	var n struct {
		Name   string `json:"name"`
		Bucket string `json:"bucket"`
	}
	err := json.NewDecoder(r.Body).Decode(&n)
	if err != nil {
		return fmt.Errorf("invalid notification: %v", err)
	}

	// handle duplicated notifications, drop keys after 5 minutes
	item := &memcache.Item{
		Key:        "notify:" + n.Bucket + "/" + n.Name,
		Expiration: 5 * time.Minute,
		Value:      []byte{},
	}
	switch err := memcache.Add(c, item); err {
	case memcache.ErrNotStored:
		// the key existed already, dup notification
		c.Infof("duplicated notification for %q", item.Key)
		return nil
	case nil:
		c.Infof("first time notification for %q", item.Key)
		// first time we see this key
	default:
		c.Errorf("add notification to memcache: %v", err)
	}

	// Add a new task to the queue
	t := taskqueue.NewPOSTTask("/notice/incoming-image", map[string][]string{
		"bucket": {n.Bucket},
		"name":   {n.Name},
	})
	_, err = taskqueue.Add(c, t, "new-images")
	if err != nil {
		return fmt.Errorf("add task to queue: %v", err)
	}

	fmt.Fprintln(w, "OK")
	return nil
}
Пример #26
0
func getStars(w http.ResponseWriter, r *http.Request) {
	w.Header().Set("Access-Control-Allow-Origin", "*")
	//w.Header().Set("SUPER-HACK", "@GDGNANTES")
	//w.WriteHeader(http.StatusFound)
	c := appengine.NewContext(r)

	login, _ := url.QueryUnescape(r.FormValue("login"))
	//favs := r.FormValue("favs")
	// Mise en cache direct du spreadsheet
	keyMemCache := "favs" + login
	item, err := memcache.Get(c, keyMemCache)
	var strJson []byte
	if err == memcache.ErrCacheMiss || err != nil {

		// Si c'est pas en cache, on regarde si c'est en base
		var user User
		keyUser := datastore.NewKey(c, "User", keyMemCache, 0, nil)
		if err := datastore.Get(c, keyUser, &user); err != nil {
			// Si c'est pas présent en base, alors on renvoie un tableau vide
			user = User{
				Login: login,
				Favs:  []string{},
			}
		}

		tmpStrJson, _ := json.Marshal(user)
		strJson = tmpStrJson
		itemValue := &memcache.Item{
			Key:   keyMemCache,
			Value: strJson,
		}
		// On ajoute dans le memcache
		if err := memcache.Add(c, itemValue); err == memcache.ErrNotStored {
			c.Infof("itemValue with key %q already exists", itemValue.Key)
		} else if err != nil {
			c.Errorf("error adding itemValue: %v", err)
		}

	} else {
		strJson = item.Value
	}
	fmt.Fprintf(w, "%s\n", strJson)

}
Пример #27
0
func handler_thumb(w http.ResponseWriter, r *http.Request) {
	r.ParseForm()
	c := appengine.NewContext(r)
	name := r.FormValue("id")

	var thumb Thumbnail
	if item, err := memcache.Get(c, name); err == nil {
		thumb = Thumbnail{Name: item.Key, Pic: item.Value}
	} else {
		q := datastore.NewQuery("Thumbnail").Filter("Name =", name)
		for t := q.Run(c); ; {
			_, err := t.Next(&thumb)
			if err != nil {
				fmt.Fprint(w, "<html><head> ", style, "<title>;(</title></head><body>")
				fmt.Fprint(w, "<h4>can't find thumb for ", name)
				return
			}

			// Add the item to the memcache, if the key does not already exist
			item := &memcache.Item{
				Key:   name,
				Value: thumb.Pic,
			}
			if err := memcache.Add(c, item); err == memcache.ErrNotStored {
				c.Warningf("item with key %q already exists", item.Key)
			} else if err != nil {
				c.Warningf("error adding item: %v", err)
			}
			break
		}
	}

	if thumb.Pic == nil {
		fmt.Fprint(w, "<html><head> ", style, "<title>;(</title></head><body>")
		fmt.Fprint(w, "<h4>can't find thumb for ", name)
		return
	}

	w.Header().Set("Content-Type", "image/jpeg")
	w.Header().Set("Content-Length", fmt.Sprintf("%v", len(thumb.Pic)))

	io.Copy(w, bytes.NewBuffer(thumb.Pic))
}
Пример #28
0
// Save saves the session in the response.
func (s *MemcacheSessionStore) Save(r *http.Request, w http.ResponseWriter,
	key string, info *sessions.SessionInfo) (flag bool, err error) {
	sid, serialized, error := getIdAndData(info)
	if error != nil {
		err = error
		return
	}

	// Add the item to the memcache, if the key does not already exist.
	err = memcache.Add(appengine.NewContext(r), &memcache.Item{
		Key:   sessionKey(sid),
		Value: serialized,
	})
	if err != nil {
		return
	}

	return sessions.SetCookie(s, w, key, cloneInfo(info, sid))
}
Пример #29
0
func handler(w http.ResponseWriter, r *http.Request) {

	c := appengine.NewContext(r)

	client := urlfetch.Client(c)
	roomId := ROOM_ID
	cwClient := chatwork.NewClient(TOKEN)
	cwClient.HttpClient = client
	messages := cwClient.RoomMessages(roomId, map[string]string{
		"force": "1",
	})
	//contents, _ := ioutil.ReadAll(resp.Body)
	//fmt.Fprint(w, string(contents))

	//if err != nil {
	//        w.WriteHeader(http.StatusInternalServerError)
	//        fmt.Fprint(w, err.Error())
	//        return
	//    }

	item := &memcache.Item{
		Key:   "lastTime",
		Value: []byte("0"),
	}
	memcache.Add(c, item)
	lastTimeItem, _ := memcache.Get(c, "lastTime")
	lastTime, _ := strconv.ParseInt(string(lastTimeItem.Value[:]), 10, 64)

	for i, _ := range messages {
		message := messages[i]
		if lastTime != 0 && message.SendTime > lastTime {
			// message := messages[len(messages) - 1]
			body, _ := domain(message, "^画像 (.*)", client, handlerSearchGoogle)
			if body != nil {
				cwClient.PostRoomMessage(roomId, *body)
			}
		}
	}
	lastMessage := messages[len(messages)-1]
	item.Value = []byte(strconv.FormatInt(lastMessage.SendTime, 10))
	memcache.Set(c, item)
}
Пример #30
0
func parseDate(c appengine.Context, feed *Feed, ds ...string) (t time.Time, err error) {
	for _, d := range ds {
		d = strings.TrimSpace(d)
		if d == "" {
			continue
		}
		for _, f := range dateFormats {
			if t, err = time.Parse(f, d); err == nil {
				return
			}
		}
		df := memcache.Item{
			Key:   fmt.Sprintf("_dateformat-%v", rand.Int63n(dateFormatCount)),
			Value: []byte(fmt.Sprintf("%v|%v", d, feed.Url)),
		}
		memcache.Add(c, &df)
	}
	err = fmt.Errorf("could not parse date: %v", strings.Join(ds, ", "))
	return
}