当前位置: 首页>>代码示例>>Golang>>正文


Golang memcache.Set函数代码示例

本文整理汇总了Golang中google/golang.org/appengine/memcache.Set函数的典型用法代码示例。如果您正苦于以下问题:Golang Set函数的具体用法?Golang Set怎么用?Golang Set使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。


在下文中一共展示了Set函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。

示例1: handler

func handler(w http.ResponseWriter, r *http.Request) {
	ctx := appengine.NewContext(r)

	// [START intro_2]
	// Create an Item
	item := &memcache.Item{
		Key:   "lyric",
		Value: []byte("Oh, give me a home"),
	}
	// Add the item to the memcache, if the key does not already exist
	if err := memcache.Add(ctx, item); err == memcache.ErrNotStored {
		log.Infof(ctx, "item with key %q already exists", item.Key)
	} else if err != nil {
		log.Errorf(ctx, "error adding item: %v", err)
	}

	// Change the Value of the item
	item.Value = []byte("Where the buffalo roam")
	// Set the item, unconditionally
	if err := memcache.Set(ctx, item); err != nil {
		log.Errorf(ctx, "error setting item: %v", err)
	}

	// Get the item from the memcache
	if item, err := memcache.Get(ctx, "lyric"); err == memcache.ErrCacheMiss {
		log.Infof(ctx, "item not in the cache")
	} else if err != nil {
		log.Errorf(ctx, "error getting item: %v", err)
	} else {
		log.Infof(ctx, "the lyric is %q", item.Value)
	}
	// [END intro_2]

}
开发者ID:GoogleCloudPlatform,项目名称:golang-samples,代码行数:34,代码来源:memcache.go

示例2: index

func index(res http.ResponseWriter, req *http.Request) {

	if req.URL.Path != "/" {
		http.NotFound(res, req)
		return
	}

	// set cookie
	id, _ := uuid.NewV4()
	cookie := &http.Cookie{
		Name:  "session-id",
		Value: id.String(),
		// Secure: true,
		HttpOnly: true,
	}
	http.SetCookie(res, cookie)

	// set memcache
	ctx := appengine.NewContext(req)
	item1 := memcache.Item{
		Key:   id.String(),
		Value: []byte("McLeod"),
	}
	memcache.Set(ctx, &item1)

	fmt.Fprint(res, "EVERYTHING SET ID:"+id.String())
}
开发者ID:GoesToEleven,项目名称:golang-web,代码行数:27,代码来源:main.go

示例3: logout

func logout(res http.ResponseWriter, req *http.Request, _ httprouter.Params) {
	ctx := appengine.NewContext(req)

	cookie, err := req.Cookie("session")
	// cookie is not set
	if err != nil {
		http.Redirect(res, req, "/", 302)
		return
	}

	// clear memcache
	sd := memcache.Item{
		Key:        cookie.Value,
		Value:      []byte(""),
		Expiration: time.Duration(1 * time.Microsecond),
	}
	memcache.Set(ctx, &sd)

	// clear the cookie
	cookie.MaxAge = -1
	http.SetCookie(res, cookie)

	// redirect
	http.Redirect(res, req, "/", 302)
}
开发者ID:RaviTezu,项目名称:GolangTraining,代码行数:25,代码来源:api.go

示例4: getResources

func getResources(w http.ResponseWriter, r *http.Request) *appError {
	//omitted code to get top, filter et cetera from the Request

	c := appengine.NewContext(r)
	h := fnv.New64a()
	h.Write([]byte("rap_query" + top + filter + orderby + skip))
	cacheKey := h.Sum64()

	cv, err := memcache.Get(c, fmt.Sprint(cacheKey))
	if err == nil {
		w.Header().Set("Content-Type", "application/json; charset=utf-8")
		w.Write(cv.Value)
		return nil
	}

	//omitting code that get resources from DataStore

	//cache this result with it's query as the key
	memcache.Set(c, &memcache.Item{
		Key:   fmt.Sprint(cacheKey),
		Value: result,
	})
	//return the json version
	w.Header().Set("Content-Type", "application/json; charset=utf-8")
	w.Write(result)
	return nil
}
开发者ID:ramjac,项目名称:homeless-services,代码行数:27,代码来源:memcache.go

示例5: index

func index(res http.ResponseWriter, req *http.Request) {
	html := ``
	//Build Cookie
	id, _ := uuid.NewV4()
	cookie := &http.Cookie{
		Name:     "my-cookie",
		Value:    id.String(),
		HttpOnly: true,
	}
	http.SetCookie(res, cookie)

	//Store memcache
	ctx := appengine.NewContext(req)
	item := memcache.Item{
		Key:   id.String(),
		Value: []byte("Matthew"),
	}
	memcache.Set(ctx, &item)

	//Get uuid from cookie
	cookieGet, _ := req.Cookie("my-cookie")
	if cookieGet != nil {
		html += `UUID from cookie: ` + cookieGet.Value + `<br>`
	}

	//Get uuid and value from memcache
	ctx = appengine.NewContext(req)
	item0, _ := memcache.Get(ctx, id.String())
	if item0 != nil {
		html += `Value from Memcache using uuid: ` + string(item0.Value) + `<br>`
	}
	res.Header().Set("Content-Type", "text/html; charset=utf-8")
	fmt.Fprint(res, html)

}
开发者ID:shadon09,项目名称:CSCI130,代码行数:35,代码来源:hello.go

示例6: createSession

func createSession(res http.ResponseWriter, req *http.Request, user User) {
	ctx := appengine.NewContext(req)
	// SET COOKIE
	id, _ := uuid.NewV4()
	cookie := &http.Cookie{
		Name:  "session",
		Value: id.String(),
		Path:  "/",
		// twenty minute session:
		MaxAge: 60 * 20,
		//		UNCOMMENT WHEN DEPLOYED:
		//		Secure: true,
		//		HttpOnly: true,
	}
	http.SetCookie(res, cookie)

	// SET MEMCACHE session data (sd)
	json, err := json.Marshal(user)
	if err != nil {
		log.Errorf(ctx, "error marshalling during user creation: %v", err)
		http.Error(res, err.Error(), 500)
		return
	}
	sd := memcache.Item{
		Key:   id.String(),
		Value: json,
	}
	memcache.Set(ctx, &sd)
}
开发者ID:RaviTezu,项目名称:GolangTraining,代码行数:29,代码来源:api.go

示例7: TestHook

func TestHook(t *testing.T) {
	body := `{"bucket": "dummy", "name": "path/obj"}`
	cacheKey := storage.CacheKey("dummy", "path/obj")
	req, _ := testInstance.NewRequest("POST", "/-/hook/gcs", strings.NewReader(body))
	ctx := appengine.NewContext(req)
	item := &memcache.Item{Key: cacheKey, Value: []byte("ignored")}
	if err := memcache.Set(ctx, item); err != nil {
		t.Fatal(err)
	}

	// must remove cached item
	res := httptest.NewRecorder()
	http.DefaultServeMux.ServeHTTP(res, req)
	if res.Code != http.StatusOK {
		t.Errorf("res.Code = %d; want %d", res.Code, http.StatusOK)
	}
	if _, err := memcache.Get(ctx, cacheKey); err != memcache.ErrCacheMiss {
		t.Fatalf("memcache.Get(%q): %v; want ErrCacheMiss", cacheKey, err)
	}

	// cache misses must not respond with an error code
	req, _ = testInstance.NewRequest("POST", "/-/hook/gcs", strings.NewReader(body))
	res = httptest.NewRecorder()
	http.DefaultServeMux.ServeHTTP(res, req)
	if res.Code != http.StatusOK {
		t.Errorf("res.Code = %d; want %d", res.Code, http.StatusOK)
	}
}
开发者ID:goadesign,项目名称:goa.design,代码行数:28,代码来源:server_test.go

示例8: Close

func (c cachingCloser) Close() error {
	go memcache.Set(c.ctx, &memcache.Item{
		Key:   c.key,
		Value: c.buf.Bytes(),
	})
	return c.rc.Close()
}
开发者ID:flowlo,项目名称:coduno-api,代码行数:7,代码来源:storage.go

示例9: createThumb

func (fi *FileInfo) createThumb(buffer *bytes.Buffer, c context.Context) {
	if imageTypes.MatchString(fi.Type) {
		src, _, err := image.Decode(bytes.NewReader(buffer.Bytes()))
		check(err)
		filter := gift.New(gift.ResizeToFit(
			THUMB_MAX_WIDTH,
			THUMB_MAX_HEIGHT,
			gift.LanczosResampling,
		))
		dst := image.NewNRGBA(filter.Bounds(src.Bounds()))
		filter.Draw(dst, src)
		buffer.Reset()
		bWriter := bufio.NewWriter(buffer)
		switch fi.Type {
		case "image/jpeg", "image/pjpeg":
			err = jpeg.Encode(bWriter, dst, nil)
		case "image/gif":
			err = gif.Encode(bWriter, dst, nil)
		default:
			err = png.Encode(bWriter, dst)
		}
		check(err)
		bWriter.Flush()
		thumbnailKey := fi.Key + thumbSuffix + filepath.Ext(fi.Name)
		item := &memcache.Item{
			Key:   thumbnailKey,
			Value: buffer.Bytes(),
		}
		err = memcache.Set(c, item)
		check(err)
		fi.ThumbnailKey = thumbnailKey
	}
}
开发者ID:hongtien510,项目名称:cakephp-routing,代码行数:33,代码来源:main.go

示例10: handleIndex

func handleIndex(res http.ResponseWriter, req *http.Request) {
	cookie, _ := req.Cookie("sessionid")
	if cookie == nil {
		id, _ := uuid.NewV4()
		cookie = &http.Cookie{
			Name:  "sessionid",
			Value: id.String(),
		}
		http.SetCookie(res, cookie)
	}

	ctx := appengine.NewContext(req)
	item, _ := memcache.Get(ctx, cookie.Value)
	if item == nil {
		m := map[string]string{
			"email": "[email protected]",
		}
		bs, _ := json.Marshal(m)

		item = &memcache.Item{
			Key:   cookie.Value,
			Value: bs,
		}
		memcache.Set(ctx, item)
	}

	var m map[string]string
	json.Unmarshal(item.Value, &m)

	fmt.Fprintln(res, m)

}
开发者ID:RobertoSuarez,项目名称:GolangTraining,代码行数:32,代码来源:main.go

示例11: Save

// Save will persist the passenger to Datastore and send it to Memcache.
func (p *Passenger) Save(ctx context.Context) (*datastore.Key, error) {
	now := time.Now()

	key, err := p.AccessToken.SaveWithParent(ctx, p.UserKey)
	if err != nil {
		return nil, err
	}

	buf := new(bytes.Buffer)
	if err = gob.NewEncoder(buf).Encode(p); err != nil {
		return nil, err
	}

	item := &memcache.Item{
		Key:        key.Encode(),
		Value:      buf.Bytes(),
		Expiration: p.AccessToken.Expiry.Sub(now) + 10*time.Second,
	}

	if err = memcache.Set(ctx, item); err != nil {
		return nil, err
	}

	return key, nil
}
开发者ID:tudorgergely,项目名称:api,代码行数:26,代码来源:passenger.go

示例12: getUser

func getUser(req *http.Request, name string) model {
	ctx := appengine.NewContext(req)
	//Dank Memcache
	item, _ := memcache.Get(ctx, name)
	var m model
	if item != nil {
		err := json.Unmarshal(item.Value, &m)
		if err != nil {
			fmt.Printf("error unmarhsalling: %v", err)
			return model{}
		}
	}
	//Datastore
	var m2 model
	key := datastore.NewKey(ctx, "Users", name, 0, nil)
	err := datastore.Get(ctx, key, &m2)
	if err == datastore.ErrNoSuchEntity {
		return model{}
	} else if err != nil {
		return model{}
	}

	//Reset Dank Memecache
	bs := marshalModel(m2)
	item1 := memcache.Item{
		Key:   m2.Name,
		Value: bs,
	}
	memcache.Set(ctx, &item1)
	return m2
}
开发者ID:yash0690,项目名称:Golang,代码行数:31,代码来源:file.go

示例13: PrimaryPublicCertificates

// PrimaryPublicCertificates returns primary's PublicCertificates.
func PrimaryPublicCertificates(c context.Context, primaryURL string) (*PublicCertificates, error) {
	cacheKey := fmt.Sprintf("pub_certs:%s", primaryURL)
	var pubCerts []byte
	setCache := false
	item, err := memcache.Get(c, cacheKey)
	if err != nil {
		setCache = true
		if err != memcache.ErrCacheMiss {
			log.Warningf(c, "failed to get cert from cache: %v", err)
		}
		pubCerts, err = downloadCert(urlfetch.Client(c), primaryURL)
		if err != nil {
			log.Errorf(c, "failed to download cert: %v", err)
			return nil, err
		}
	} else {
		pubCerts = item.Value
	}
	pc := &PublicCertificates{}
	if err = json.Unmarshal(pubCerts, pc); err != nil {
		log.Errorf(c, "failed to unmarshal cert: %v %v", string(pubCerts), err)
		return nil, err
	}
	if setCache {
		err = memcache.Set(c, &memcache.Item{
			Key:        cacheKey,
			Value:      pubCerts,
			Expiration: time.Hour,
		})
		if err != nil {
			log.Warningf(c, "failed to set cert to cache: %v", err)
		}
	}
	return pc, nil
}
开发者ID:shishkander,项目名称:luci-go,代码行数:36,代码来源:publiccert_appengine.go

示例14: handleMessage

// return a message based on its id
func handleMessage(res http.ResponseWriter, req *http.Request) {
	ctx := appengine.NewContext(req)
	// get key from URL
	key := strings.SplitN(req.URL.Path, "/", 3)[2]
	// get item from memcache
	item, err := memcache.Get(ctx, key)
	if err != nil {
		http.NotFound(res, req)
		return
	}

	var secretKey [32]byte
	bs, err := hex.DecodeString(req.FormValue("secret"))
	if err != nil || len(bs) != 32 {
		http.NotFound(res, req)
		return
	}
	copy(secretKey[:], bs)

	msg, err := decrypt(string(item.Value), secretKey)
	if err != nil {
		http.NotFound(res, req)
		return
	}

	// if this is the first time the message is viewed
	if item.Flags == 0 {
		item.Expiration = 30 * time.Second
		item.Flags = 1
		memcache.Set(ctx, item)
	}

	res.Write([]byte(msg))
}
开发者ID:RaviTezu,项目名称:GolangTraining,代码行数:35,代码来源:routes.go

示例15: storeMemc

func storeMemc(bs []byte, id string, req *http.Request) {
	ctx := appengine.NewContext(req)
	item1 := memcache.Item{
		Key:   id,
		Value: bs,
	}
	memcache.Set(ctx, &item1)
}
开发者ID:GoesToEleven,项目名称:golang-web,代码行数:8,代码来源:memc.go


注:本文中的google/golang.org/appengine/memcache.Set函数示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。