• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

Golang memcache.Get函数代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

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



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

示例1: 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,
		}
	}
	fmt.Fprintln(res, string(item.Value))
}
开发者ID:RaviTezu,项目名称:GolangTraining,代码行数:26,代码来源:main.go


示例2: 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


示例3: 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


示例4: fromCache

func fromCache(ctx context.Context, bucket, name string) (io.ReadCloser, error) {
	item, err := memcache.Get(ctx, key(bucket, name))
	if err != nil {
		return nil, err
	}
	return ioutil.NopCloser(bytes.NewReader(item.Value)), nil
}
开发者ID:flowlo,项目名称:coduno-api,代码行数:7,代码来源:storage.go


示例5: noConfusion

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

	var html string

	// get cookie value
	var id string
	cookie, _ := req.Cookie("session-id")
	if cookie != nil {
		id = cookie.Value
		html += `
				<br>
				<p>Value from cookie: ` + id + `</p>
				`
	}

	// get memcache value
	ctx := appengine.NewContext(req)
	item, _ := memcache.Get(ctx, id)
	if item != nil {
		html += `
			<br>
			<p>
			Value from memcache: ` + string(item.Value) + `
			</p>
		`
	}

	res.Header().Set("Content-Type", "text/html; charset=utf-8")
	fmt.Fprint(res, html)
}
开发者ID:GoesToEleven,项目名称:golang-web,代码行数:30,代码来源:main.go


示例6: 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


示例7: genCookie

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

	cookie, err := req.Cookie("session-id")
	if err != nil {
		cookie = newVisitor(req)
		http.SetCookie(res, cookie)
		return cookie
	}

	// make sure set cookie uses our current structure
	if strings.Count(cookie.Value, "|") != 2 {
		cookie = newVisitor(req)
		http.SetCookie(res, cookie)
		return cookie
	}

	if tampered(cookie.Value) {
		cookie = newVisitor(req)
		http.SetCookie(res, cookie)
		return cookie
	}

	ctx := appengine.NewContext(req)
	id := strings.Split(cookie.Value, "|")[0]
	item, _ := memcache.Get(ctx, id)
	if item == nil {
		cookie = newVisitor(req)
		http.SetCookie(res, cookie)
		return cookie
	}

	return cookie
}
开发者ID:GoesToEleven,项目名称:golang-web,代码行数:33,代码来源:cookie.go


示例8: 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


示例9: 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


示例10: get

func get(w http.ResponseWriter, r *http.Request) {
	if r.URL.Path == "/" {
		http.Redirect(w, r, WEBSITE, http.StatusFound)
		return
	}
	// Use RequestURI instead of r.URL.Path, as we need the encoded form:
	key := extractKey(r)
	parts := strings.Split(key, "/")
	if len(parts) == 3 {
		context := appengine.NewContext(r)
		item, err := memcache.Get(context, key)
		if err == nil {
			w.Header().Add("X-Content-Type-Options", "nosniff")
			contentType, _ := url.QueryUnescape(parts[0])
			if !imageTypes.MatchString(contentType) {
				contentType = "application/octet-stream"
			}
			w.Header().Add("Content-Type", contentType)
			w.Header().Add(
				"Cache-Control",
				fmt.Sprintf("public,max-age=%d", EXPIRATION_TIME),
			)
			w.Write(item.Value)
			return
		}
	}
	http.Error(w, "404 Not Found", http.StatusNotFound)
}
开发者ID:hongtien510,项目名称:cakephp-routing,代码行数:28,代码来源:main.go


示例11: index

func index(res http.ResponseWriter, req *http.Request) {
	ctx := appengine.NewContext(req)
	// get cookie UUID, or set
	cookie, _ := req.Cookie("sessionid")
	if cookie == nil {
		cookie = createCookie()
		http.SetCookie(res, cookie)
	}
	// get memcache session data
	item, _ := memcache.Get(ctx, cookie.Value)
	if item == nil {
		http.Redirect(res, req, "/login", 302)
		return
	}
	// unmarshal
	var m map[string]string
	json.Unmarshal(item.Value, &m)
	// authenticate
	if m["loggedin"] == "false" || m["loggedin"] == nil {
		http.Redirect(res, req, "/login", 302)
		return
	}
	// upload photo
	src, hdr, err := req.FormFile("data")
	if req.Method == "POST" && err == nil {
		uploadPhoto(src, hdr, m)
	}
	// save session
	session.Save(req, res)
	// get photos
	data := getPhotos(session)
	// execute template
	tpl.ExecuteTemplate(res, "index.html", data)
}
开发者ID:ardan-bkennedy,项目名称:GolangTraining,代码行数:34,代码来源:main.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: 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


示例14: retrieveMemc

func retrieveMemc(id string, req *http.Request) (model, error) {

	var m model
	ctx := appengine.NewContext(req)
	item, err := memcache.Get(ctx, id)
	if err != nil {
		// get data from datastore
		log.Infof(ctx, "VALUE FROM DATASTORE: %s", err)
		m, err = retrieveDstore(id, req)
		if err != nil {
			return m, err
		}
		// put data in memcache
		storeMemc(m, id, req)
		return m, nil
	}
	// unmarshal from JSON
	log.Infof(ctx, "VALUE FROM MEMCACHE")
	err = json.Unmarshal(item.Value, &m)
	if err != nil {
		log.Errorf(ctx, "ERROR retrieveMemc unmarshal: %s", err)
		return m, err
	}
	return m, nil
}
开发者ID:GoesToEleven,项目名称:golang-web,代码行数:25,代码来源:smemc.go


示例15: fromCache

func fromCache(ctx context.Context, key *datastore.Key) (p *Passenger, err error) {
	item, err := memcache.Get(ctx, key.Encode())
	if err != nil {
		return nil, err
	}
	p = new(Passenger)
	err = gob.NewDecoder(bytes.NewReader(item.Value)).Decode(&p)
	return
}
开发者ID:tudorgergely,项目名称:api,代码行数:9,代码来源:passenger.go


示例16: get

func (mc *gaeMemcache) get(c context.Context, key string) ([]byte, error) {
	item, err := memcache.Get(c, key)
	if err == memcache.ErrCacheMiss {
		return nil, errCacheMiss
	} else if err != nil {
		return nil, err
	}
	return item.Value, nil
}
开发者ID:CadeLaRen,项目名称:ioweb2016,代码行数:9,代码来源:cache.go


示例17: GetTeamIDbyProviderID

func GetTeamIDbyProviderID(c context.Context, providerID int) int64 {
	key := fmt.Sprintf("teamID:byproviderID:%d", providerID)
	id := int64(0)
	if item, err := memcache.Get(c, key); err == nil {
		id, _ = strconv.ParseInt(string(item.Value), 10, 0)
	}

	return id
}
开发者ID:jittuu,项目名称:hivepeek,代码行数:9,代码来源:util.go


示例18: Get

// Get returns the response corresponding to key if present.
func (c *Cache) Get(key string) (resp []byte, ok bool) {
	item, err := memcache.Get(c.Context, cacheKey(key))
	if err != nil {
		if err != memcache.ErrCacheMiss {
			log.Errorf(c.Context, "error getting cached response: %v", err)
		}
		return nil, false
	}
	return item.Value, true
}
开发者ID:PalmStoneGames,项目名称:httpcache,代码行数:11,代码来源:appengine.go


示例19: lookupCache

func lookupCache(ctx context.Context, key datastore.Key) *MaybeError {
	maybeItem, err := memcache.Get(ctx, key.String())
	var result = new(MaybeError)
	if err != nil { // treat all errors as "cache miss"
		*result = CacheMiss{}
	} else {
		result = jsonToTodoItem(maybeItem.Value)
	}
	return result
}
开发者ID:catamorphism,项目名称:tada,代码行数:10,代码来源:tada.go


示例20: getCache

func getCache(req *http.Request) (audio []byte) {
	c := appengine.NewContext(req)
	item, err := memcache.Get(c, cacheKey(req))
	if err != nil {
		if err != memcache.ErrCacheMiss {
			c.Errorf("getCache: %v", err)
		}
		return nil
	}
	return item.Value
}
开发者ID:nf,项目名称:cacophon,代码行数:11,代码来源:main_appengine.go



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


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Golang memcache.Set函数代码示例发布时间:2022-05-28
下一篇:
Golang mail.Send函数代码示例发布时间:2022-05-28
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap