예제 #1
0
func delete(w http.ResponseWriter, r *http.Request) {
	key := extractKey(r)
	parts := strings.Split(key, "/")
	if len(parts) == 3 {
		result := make(map[string]bool, 1)
		context := appengine.NewContext(r)
		err := memcache.Delete(context, key)
		if err == nil {
			result[key] = true
			contentType, _ := url.QueryUnescape(parts[0])
			if imageTypes.MatchString(contentType) {
				thumbnailKey := key + thumbSuffix + filepath.Ext(parts[2])
				err := memcache.Delete(context, thumbnailKey)
				if err == nil {
					result[thumbnailKey] = true
				}
			}
		}
		w.Header().Set("Content-Type", "application/json")
		b, err := json.Marshal(result)
		check(err)
		fmt.Fprintln(w, string(b))
	} else {
		http.Error(w, "405 Method not allowed", http.StatusMethodNotAllowed)
	}
}
//Remove removes a card from the datastore, memcache, and stripe
func Remove(w http.ResponseWriter, r *http.Request) {
	//get form values
	datastoreId := r.FormValue("customerId")
	datastoreIdInt, _ := strconv.ParseInt(datastoreId, 10, 64)

	//make sure an id was given
	if len(datastoreId) == 0 {
		output.Error(ErrMissingInput, "A customer's datastore ID must be given but was missing. This value is different from your \"Customer ID\" and should have been submitted automatically.", w, r)
		return
	}

	//init stripe
	c := appengine.NewContext(r)
	sc := createAppengineStripeClient(c)

	//delete customer on stripe
	custData, err := findByDatastoreId(c, datastoreIdInt)
	if err != nil {
		output.Error(err, "An error occured while trying to look up customer's Stripe information.", w, r)
	}
	stripeCustId := custData.StripeCustomerToken
	sc.Customers.Del(stripeCustId)

	//delete custome from datastore
	completeKey := getCustomerKeyFromId(c, datastoreIdInt)
	err = datastore.Delete(c, completeKey)
	if err != nil {
		output.Error(err, "There was an error while trying to delete this customer. Please try again.", w, r)
		return
	}

	//delete customer from memcache
	//delete list of cards in memcache since this list is now stale
	//all memcache.Delete operations are listed first so error handling doesn't return if one fails...each call does not depend on another so this is safe
	//obviously, if the card is not in the cache it cannot be removed
	err1 := memcache.Delete(c, datastoreId)
	err2 := memcache.Delete(c, custData.CustomerId)
	err3 := memcache.Delete(c, listOfCardsKey)
	if err1 != nil && err1 != memcache.ErrCacheMiss {
		output.Error(err1, "There was an error flushing this card's data from the cache (by datastore id). Please contact an administrator and have them flush the cache manually.", w, r)
		return
	}
	if err2 != nil && err2 != memcache.ErrCacheMiss {
		output.Error(err2, "There was an error flushing this card's data from the cache (by customer id). Please contact an administrator and have them flush the cache manually.", w, r)
		return
	}
	if err3 != nil && err3 != memcache.ErrCacheMiss {
		output.Error(err3, "There was an error flushing the cached list of cards.", w, r)
		return
	}

	//customer removed
	//return to client
	output.Success("removeCustomer", nil, w)
	return
}
예제 #3
0
파일: app.go 프로젝트: garycarr/go-oauth
// serveTwitterCallback handles callbacks from the Twitter OAuth server.
func serveTwitterCallback(c *context) error {
	token := c.r.FormValue("oauth_token")
	var ci connectInfo
	_, err := memcache.Gob.Get(c.c, token, &ci)
	if err != nil {
		return err
	}
	memcache.Delete(c.c, token)
	tempCred := &oauth.Credentials{
		Token:  token,
		Secret: ci.Secret,
	}

	httpClient := urlfetch.Client(c.c)
	tokenCred, _, err := oauthClient.RequestToken(httpClient, tempCred, c.r.FormValue("oauth_verifier"))
	if err != nil {
		return err
	}

	if err := c.updateUserInfo(func(u *userInfo) { u.TwitterCred = *tokenCred }); err != nil {
		return err
	}
	http.Redirect(c.w, c.r, ci.Redirect, 302)
	return nil
}
예제 #4
0
func purgeCache(ctx context.Context, key string) error {
	err := memcache.Delete(ctx, key)
	if err == memcache.ErrCacheMiss {
		err = nil
	}
	return err
}
예제 #5
0
// DELETE http://localhost:8080/users/1
//
func (u *UserService) removeUser(request *restful.Request, response *restful.Response) {
	c := appengine.NewContext(request.Request)
	id := request.PathParameter("user-id")
	err := memcache.Delete(c, id)
	if err != nil {
		response.WriteError(http.StatusInternalServerError, err)
	}
}
예제 #6
0
// removeObjectCache removes cached object from memcache.
// It returns nil in case where memcache.Delete would result in ErrCacheMiss.
func removeObjectCache(ctx context.Context, bucket, obj string) error {
	k := path.Join(bucket, obj)
	err := memcache.Delete(ctx, k)
	if err == memcache.ErrCacheMiss {
		err = nil
	}
	return err
}
예제 #7
0
파일: memcache.go 프로젝트: deepthi261/130
// Deletes a record from memcache for the given id
func Delete(id string, req *http.Request) error {
	ctx := appengine.NewContext(req)
	err := memcache.Delete(ctx, id)
	if err != memcache.ErrCacheMiss {
		return err
	}
	return nil
}
예제 #8
0
파일: tada.go 프로젝트: catamorphism/tada
func invalidateCache(ctx context.Context, key datastore.Key) *MaybeError {
	// delete key from memcache
	err := memcache.Delete(ctx, key.String())
	var result = new(MaybeError)
	if err != nil {
		*result = E(err.Error())
	} else {
		*result = Ok{}
	}
	return result
}
예제 #9
0
func submissionsAddHandler(w http.ResponseWriter, r *http.Request) {
	ctx := appengine.NewContext(r)
	if err := r.ParseForm(); err != nil {
		serveErr(ctx, err, w)
		return
	}

	ID, _, err := datastore.AllocateIDs(ctx, "Podcast", nil, 1)
	if err != nil {
		serveErr(ctx, err, w)
		return
	}

	date, err := time.Parse(yyyymmdd, r.FormValue("date"))
	if err != nil {
		serveErr(ctx, err, w)
		return
	}

	podcast := Podcast{
		ID:         ID,
		Show:       r.FormValue("show"),
		Title:      r.FormValue("title"),
		Desc:       r.FormValue("desc"),
		URL:        template.URL(r.FormValue("url")),
		MediaURL:   template.URL(r.FormValue("media_url")),
		RuntimeSec: r.FormValue("runtime"),
		Size:       r.FormValue("size"),
		Date:       date,
		Added:      time.Now(),
	}

	if _, err := datastore.Put(ctx, datastore.NewKey(ctx, "Podcast", "", ID, nil), &podcast); err != nil {
		serveErr(ctx, err, w)
		return
	}

	key, err := datastore.DecodeKey(r.FormValue("key"))
	if err != nil {
		serveErr(ctx, err, w)
		return
	}

	if err := datastore.Delete(ctx, key); err != nil {
		serveErr(ctx, err, w)
		return
	}

	if err := memcache.Delete(ctx, cacheKey); err != nil {
		log.Errorf(ctx, "memcache delete error %v", err)
	}

	successTmpl.ExecuteTemplate(w, "base", nil)
}
//Delete removes a key value pair from memcache
func Delete(c context.Context, key string) error {
	err := memcache.Delete(c, key)
	if err == memcache.ErrCacheMiss {
		//key does not exist
		//this is not an error
		return nil
	} else if err != nil {
		return err
	}

	//delete successful
	return nil
}
예제 #11
0
파일: server.go 프로젝트: jchorl/til_newtab
func deleteSavedPosts(c context.Context, key string) error {
	// flush db
	q := datastore.NewQuery(key).Ancestor(getParentKey(c, key)).KeysOnly()
	keys, err := q.GetAll(c, nil)
	if err != nil {
		return err
	}
	if err = datastore.DeleteMulti(c, keys); err != nil {
		return err
	}
	// flush cache
	return memcache.Delete(c, key)
}
func (a *MemcacheDatastoreAccessor) dismissMessage(c context.Context, key *datastore.Key) (*MessageForUser, error) {
	msg, err := a.GaeDatastoreAccessor.dismissMessage(c, key)
	if err != nil {
		return nil, err
	}

	cacheKey := "getMessagesForUser(" + msg.Username + ")"
	err = memcache.Delete(c, cacheKey)
	if err == memcache.ErrCacheMiss {
		// No problem if wasn't in cache anyway
		err = nil
	}
	return msg, err
}
func (a *MemcacheDatastoreAccessor) saveNewMessage(c context.Context, msg *MessageForUser) (*datastore.Key, error) {
	key, err := a.GaeDatastoreAccessor.saveNewMessage(c, msg)
	if err != nil {
		return key, err
	}

	cacheKey := "getMessagesForUser(" + msg.Username + ")"
	err = memcache.Delete(c, cacheKey)
	if err == memcache.ErrCacheMiss {
		// No problem if wasn't in cache anyway
		err = nil
	}
	return key, err
}
예제 #14
0
파일: dl.go 프로젝트: barthy1/s3cli
func uploadHandler(w http.ResponseWriter, r *http.Request) {
	if r.Method != "POST" {
		http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
		return
	}
	c := appengine.NewContext(r)

	// Authenticate using a user token (same as gomote).
	user := r.FormValue("user")
	if !validUser(user) {
		http.Error(w, "bad user", http.StatusForbidden)
		return
	}
	if builderKey == "" {
		http.Error(w, "no builder-key found in project metadata", http.StatusInternalServerError)
		return
	}
	if r.FormValue("key") != userKey(c, user) {
		http.Error(w, "bad key", http.StatusForbidden)
		return
	}

	var f File
	defer r.Body.Close()
	if err := json.NewDecoder(r.Body).Decode(&f); err != nil {
		log.Errorf(c, "error decoding upload JSON: %v", err)
		http.Error(w, "Something broke", http.StatusInternalServerError)
		return
	}
	if f.Filename == "" {
		http.Error(w, "Must provide Filename", http.StatusBadRequest)
		return
	}
	if f.Uploaded.IsZero() {
		f.Uploaded = time.Now()
	}
	k := datastore.NewKey(c, "File", f.Filename, 0, rootKey(c))
	if _, err := datastore.Put(c, k, &f); err != nil {
		log.Errorf(c, "putting File entity: %v", err)
		http.Error(w, "could not put File entity", http.StatusInternalServerError)
		return
	}
	if err := memcache.Delete(c, cacheKey); err != nil {
		log.Errorf(c, "cache delete error: %v", err)
	}
	io.WriteString(w, "OK")
}
예제 #15
0
func createSearchItem(con *data.Context, values url.Values, namespace string, URL string) (*search.Item, error) {
	DSKey := values["DSKey"][0]

	// We can't do anything without a DSKey
	if DSKey == "" || namespace == "" || URL == "" {
		con.Log.Errorf("Something is wrong with the input data. DSKey: %v, Namespace: %v, URL: %v.", DSKey, namespace, URL)
		return nil, fmt.Errorf("Something is wrong with the input data. DSKey: %v, Namespace: %v, URL: %v.", DSKey, namespace, URL)
	}

	memI, err := memcache.Get(con.C, DSKey)
	_ = memcache.Delete(con.C, DSKey) // ignore possible errors

	var searchItem search.Item
	searchItem.Description = values["Caption"][0] + " " + values["Description"][0]

	if err == nil {
		searchItem.HTMLforSearch = gaesearch.HTML(string(memI.Value))
	} else {
		// ok, no data in the memcache.
		// we need to re-query the URL to get the HTML data
		c := urlextractclient.Get(con)
		info, err := c.Extract(URL)
		if err != nil {
			con.Log.Errorf("Error in URLextract.Extract(). Error: %v", err)
			return nil, err
		}
		var i data.Item
		i = data.ItemFromWebpageInfo(info)

		searchItem.HTMLforSearch = gaesearch.HTML(i.HTMLforSearch)
	}

	if s, err := strconv.ParseInt(values["CreatedAt"][0], 10, 64); err == nil {
		searchItem.CreatedAt = time.Unix(s, 0)
	} else {
		searchItem.CreatedAt = time.Now()
	}

	return &searchItem, nil
}
예제 #16
0
func (mp *AppEngineProvider) SessionDestroy(c context.Context, sid string) error {
	done := make(chan bool, 2)

	go func() {
		k := datastore.NewKey(c, "BeegoSessionStore", sid, 0, nil)
		if ds_err := datastore.Delete(c, k); ds_err != nil {
			log.Errorf(c, "session-appengine: error deleting session data from datastore: %v", ds_err)
		}
		done <- true
	}()

	go func() {
		mem_err := memcache.Delete(c, sid)
		if mem_err != nil {
			log.Errorf(c, "session-appengine: error deleting session data from memcache: %v", mem_err)
		}
		done <- true
	}()

	_, _ = <-done, <-done
	return nil
}
예제 #17
0
// Data changes should lead to cache entries invalidation.
func htmlCacheEvict(c context.Context, key string) {
	_ = memcache.Delete(c, key)
}
예제 #18
0
// Delete removes the response with key from the cache.
func (c *Cache) Delete(key string) {
	if err := memcache.Delete(c.Context, cacheKey(key)); err != nil {
		log.Errorf(c.Context, "error deleting cached response: %v", err)
	}
}
예제 #19
0
파일: mcache.go 프로젝트: aarzilli/tools
func (d *DsDir) MemCacheDelete() {
	err := memcache.Delete(d.fSys.Ctx(), d.Dir+d.BName)
	if err != nil {
		aelog.Errorf(d.fSys.Ctx(), "memcache delete dir %v => err %v", d.Dir+d.BName, err)
	}
}
예제 #20
0
// Delete deletes the key
func (d *Driver) Delete(key string) {
	memcache.Delete(d.ctx, key)
}
예제 #21
0
// adminHandler serves an administrative interface.
func adminHandler(w http.ResponseWriter, r *http.Request) {
	c := appengine.NewContext(r)

	if !user.IsAdmin(c) {
		http.Error(w, "forbidden", http.StatusForbidden)
		return
	}

	var newLink *Link
	var doErr error
	if r.Method == "POST" {
		key := r.FormValue("key")
		switch r.FormValue("do") {
		case "Add":
			newLink = &Link{key, r.FormValue("target")}
			doErr = putLink(c, newLink)
		case "Delete":
			k := datastore.NewKey(c, kind, key, 0, nil)
			doErr = datastore.Delete(c, k)
		default:
			http.Error(w, "unknown action", http.StatusBadRequest)
		}
		err := memcache.Delete(c, cacheKey(key))
		if err != nil && err != memcache.ErrCacheMiss {
			log.Warningf(c, "%q: %v", key, err)
		}
	}

	var links []*Link
	_, err := datastore.NewQuery(kind).Order("Key").GetAll(c, &links)
	if err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		log.Errorf(c, "%v", err)
		return
	}

	// Put the new link in the list if it's not there already.
	// (Eventual consistency means that it might not show up
	// immediately, which might be confusing for the user.)
	if newLink != nil && doErr == nil {
		found := false
		for i := range links {
			if links[i].Key == newLink.Key {
				found = true
				break
			}
		}
		if !found {
			links = append([]*Link{newLink}, links...)
		}
		newLink = nil
	}

	var data = struct {
		BaseURL string
		Prefix  string
		Links   []*Link
		New     *Link
		Error   error
	}{baseURL, prefix, links, newLink, doErr}
	if err := adminTemplate.Execute(w, &data); err != nil {
		log.Criticalf(c, "adminTemplate: %v", err)
	}
}
예제 #22
0
func TestCachedCertsCacheMiss(t *testing.T) {
	rt := newTestRoundTripper()
	origTransport := httpTransportFactory
	defer func() { httpTransportFactory = origTransport }()
	httpTransportFactory = func(c context.Context) http.RoundTripper {
		return rt
	}

	req, _, closer := newTestRequest(t, "GET", "/", nil)
	defer closer()
	nc, err := appengine.Namespace(appengine.NewContext(req), certNamespace)
	if err != nil {
		t.Fatal(err)
	}
	ec := NewContext(req)

	tts := []*struct {
		respStatus                     int
		respContent, cacheControl, age string
		want                           *certsList
		shouldCache                    bool
	}{
		{200, `{"keyvalues":null}`, "max-age=3600", "600", &certsList{}, true},
		{-1, "", "", "", nil, false},
		{400, "", "", "", nil, false},
		{200, `{"keyvalues":null}`, "", "", &certsList{}, false},
	}

	for i, tt := range tts {
		if tt.respStatus > 0 {
			resp := &http.Response{
				Status:     fmt.Sprintf("%d", tt.respStatus),
				StatusCode: tt.respStatus,
				Body:       ioutil.NopCloser(strings.NewReader(tt.respContent)),
				Header:     make(http.Header),
			}
			resp.Header.Set("cache-control", tt.cacheControl)
			resp.Header.Set("age", tt.age)
			rt.Add(resp)
		}
		memcache.Delete(nc, DefaultCertURI)

		out, err := cachedCerts(ec)
		switch {
		case err != nil && tt.want != nil:
			t.Errorf("%d: cachedCerts() = %v", i, err)
		case err == nil && tt.want == nil:
			t.Errorf("%d: cachedCerts() = %#v; want error", i, out)
		default:
			if !reflect.DeepEqual(out, tt.want) {
				t.Errorf("%d: cachedCerts() = %#v; want %#v", i, out, tt.want)
			}
			if !tt.shouldCache {
				continue
			}
			item, err := memcache.Get(nc, DefaultCertURI)
			if err != nil {
				t.Errorf("%d: memcache.Get(%q) = %v", i, DefaultCertURI, err)
				continue
			}
			cert := string(item.Value)
			if tt.respContent != cert {
				t.Errorf("%d: memcache.Get(%q) = %q; want %q",
					i, DefaultCertURI, cert, tt.respContent)
			}
		}
	}
}