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

Golang hdis.Conn类代码示例

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

本文整理汇总了Golang中github.com/armen/hdis.Conn的典型用法代码示例。如果您正苦于以下问题:Golang Conn类的具体用法?Golang Conn怎么用?Golang Conn使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。



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

示例1: JobsCount

func JobsCount(userid string) int {
	c := redisPool.Get()
	defer c.Close()

	hc := hdis.Conn{c}
	count, _ := redis.Int(hc.Do("HGET", "u:"+userid+":jobs"))

	return count
}
开发者ID:armen,项目名称:splst,代码行数:9,代码来源:project.go


示例2: Profile

func (user *User) Profile(attribute string) string {
	conn := RedisPool.Get()
	defer conn.Close()

	hc := hdis.Conn{conn}

	attr, _ := redis.String(hc.Get("u:" + user.Id + ":" + strings.ToLower(attribute)))

	return attr
}
开发者ID:armen,项目名称:gapp,代码行数:10,代码来源:user.go


示例3: PreLogin

func (user *User) PreLogin(c *Context) error {

	if user.Id != "" {
		conn := RedisPool.Get()
		defer conn.Close()

		hc := hdis.Conn{conn}
		previousPicture, _ = redis.String(hc.Get("u:" + user.Id + ":picture"))
	}

	return nil
}
开发者ID:armen,项目名称:gapp,代码行数:12,代码来源:user.go


示例4: save

func (p *Project) save() error {

	c := redisPool.Get()
	defer c.Close()

	hc := hdis.Conn{c}

	// Decrease number of jobs and remove it from the queue
	defer hc.Do("HINCRBY", "u:"+p.OwnerId+":jobs", -1)
	defer c.Do("SREM", "jobs-in-queue", p.Id)

	log.Printf("Saving project %q, %q", p.Id, p.URL)

	err := p.generateThumbnail()
	if err != nil {
		log.Println(err)
	}
	p.Thumb = err == nil

	var buffer bytes.Buffer
	enc := gob.NewEncoder(&buffer)
	err = enc.Encode(p)
	if err != nil {
		return err
	}

	key := "p:" + p.Id

	_, err = hc.Set(key, buffer.Bytes())
	if err != nil {
		return err
	}

	_, err = c.Do("RPUSH", "u:"+p.OwnerId, p.Id)
	if err != nil {
		return err
	}

	_, err = c.Do("LPUSH", "recent-projects", p.Id)
	if err != nil {
		return err
	}

	for _, tag := range p.Tags {
		tag = strings.ToLower(strings.TrimSpace(tag))
		c.Do("SADD", "t:"+tag, p.Id)
	}

	log.Printf("Saved project %q, %q", p.Id, p.URL)

	return nil
}
开发者ID:armen,项目名称:splst,代码行数:52,代码来源:project.go


示例5: Fetch

func Fetch(pId string) (*Project, error) {

	c := redisPool.Get()
	defer c.Close()

	var project Project

	hc := hdis.Conn{c}

	p, err := redis.Bytes(hc.Get("p:" + pId))
	if err != nil {
		return nil, err
	}

	buffer := bytes.NewBuffer(p)
	dec := gob.NewDecoder(buffer)
	dec.Decode(&project)

	return &project, nil
}
开发者ID:armen,项目名称:splst,代码行数:20,代码来源:project.go


示例6: Delete

func (p *Project) Delete() error {

	projectsPath := path.Join(appRoot, "static", "projects")
	imgPath := path.Join(projectsPath, p.OwnerId, p.Id)
	err := os.RemoveAll(imgPath)
	if err != nil {
		return err
	}

	// Delete the owner directory if it's empty, so any errors should be ignored
	ownerPath := path.Join(projectsPath, p.OwnerId)
	os.Remove(ownerPath)

	c := redisPool.Get()
	defer c.Close()

	hc := hdis.Conn{c}

	_, err = c.Do("LREM", "u:"+p.OwnerId, 1, p.Id)
	if err != nil {
		return err
	}

	_, err = c.Do("LREM", "recent-projects", 1, p.Id)
	if err != nil {
		return err
	}

	for _, tag := range p.Tags {
		tag = strings.ToLower(strings.TrimSpace(tag))
		c.Do("SREM", "t:"+tag, p.Id)
	}

	key := "p:" + p.Id
	if deleted, _ := redis.Bool(hc.Do("HDEL", key)); deleted {
		return nil
	}

	return ProjectDeleteError
}
开发者ID:armen,项目名称:splst,代码行数:40,代码来源:project.go


示例7: Save

func (p *Project) Save() error {

	_, err := url.ParseRequestURI(p.URL)
	if err != nil {
		return InvalidUrlError
	}

	c := redisPool.Get()
	defer c.Close()

	hc := hdis.Conn{c}

	// Use a short project id and double checked for it's existance
	for {
		p.Id = utils.GenId(3)
		key := "p:" + p.Id

		if exists, _ := redis.Bool(hc.Do("HEXISTS", key)); !exists {
			break
		}
	}

	if ok, _ := redis.Bool(c.Do("SADD", "jobs-in-queue", p.Id)); ok {

		hc.Do("HINCRBY", "u:"+p.OwnerId+":jobs", 1)

		j := job{project: p, err: make(chan error)}

		// Put the job in the queue, this will block if it's full
		saveQueue <- j

		// Read the result from err channel
		return <-j.err
	}

	return ProjectSaveError
}
开发者ID:armen,项目名称:splst,代码行数:37,代码来源:project.go


示例8: newContext

func newContext(w http.ResponseWriter, r *http.Request) (*Context, error) {

	context := &Context{Response: w, Request: r}

	// Create a session and store it in cookie so that we can recognize the user when he/she gets back
	// TODO: read session/cookie name from config
	var err error
	context.Session, err = sessionStore.Get(r, "gapp")
	if err != nil {
		log.Printf("error in getting session store: %q", err)
	}

	// Changed maximum age of the session to one month
	context.Session.Options = &sessions.Options{
		Path:   "/",
		MaxAge: 86400 * 30,
	}

	// Generate new userid if there isn't any
	userid, ok := context.Session.Values["userid"].(string)
	if !ok {
		// It's an anonymous user
		userid = utils.GenId(16)
		context.Session.Values["userid"] = userid

	} else {
		// Let's fetch user's profile

		conn := RedisPool.Get()
		defer conn.Close()

		hc := hdis.Conn{conn}

		key := "u:" + userid + ":gob"

		if ok, _ := redis.Bool(hc.Do("HEXISTS", key)); ok {
			u, err := redis.Bytes(hc.Get(key))
			if err == nil {
				var user *User

				buffer := bytes.NewBuffer(u)
				dec := gob.NewDecoder(buffer)
				err = dec.Decode(&user)

				if err == nil {
					context.User = user
				} else {
					log.Printf("error in decoding user's (%s) profile: %q", userid, err)
				}
			} else {
				log.Printf("error in fetching user's (%s) profile from redis: %q", userid, err)
			}
		}
	}

	if context.User == nil {
		context.User = &User{Id: userid, Name: "Anonymous"}
	}

	// Saving session everytime it gets access helps to push expiry date further
	err = context.Session.Save(r, w)

	return context, err
}
开发者ID:armen,项目名称:gapp,代码行数:64,代码来源:context.go


示例9: ExampleGet

func ExampleGet() {

	c, _ := redis.Dial("tcp", ":6379")
	defer c.Close()

	hc := hdis.Conn{c}

	key := "object:1234567"
	hc.Set(key, "The value")
	value, _ := redis.String(hc.Get(key))

	fmt.Printf("%v\n", value)

	// Delete the hash if it exists
	if exists, _ := redis.Bool(hc.Do("HEXISTS", key)); exists {
		_, err := hc.Do("HDEL", key)
		if err != nil {
			fmt.Println(err)
		}
	}

	// Execute invalid hash command
	_, err := hc.Do("GET", "sample-key")
	if err == hdis.NotAHashCommandError {
		fmt.Println("Invalid hash command")
	} else {
		fmt.Println(err)
	}

	// Output:
	// The value
	// Invalid hash command
}
开发者ID:armen,项目名称:hdis,代码行数:33,代码来源:example_test.go


示例10: Login

func (user *User) Login(c *Context) error {

	conn := RedisPool.Get()
	defer conn.Close()

	hc := hdis.Conn{conn}

	user.Id, _ = redis.String(conn.Do("ZSCORE", "users", user.Email))

	if user.Id == "" {

		// Yay another newcomer!
		nextuserid, err := conn.Do("INCR", "next-user-id")
		if err != nil {
			return err
		}

		// Pad userid with 0
		user.Id = fmt.Sprintf("%03d", nextuserid.(int64))

		_, err = conn.Do("ZADD", "users", user.Id, user.Email)
		if err != nil {
			return err
		}

	} else {
		// Pad user.Id with 0
		user.Id = fmt.Sprintf("%03s", user.Id)
	}

	err := user.PreLogin(c)
	if err != nil {
		return err
	}

	// Signin the user
	user.SignedIn = true

	// Encode to gob
	var buffer bytes.Buffer
	enc := gob.NewEncoder(&buffer)
	err = enc.Encode(user)
	if err != nil {
		return err
	}

	// Every time set the user's info, actually it'll update the profile
	key := "u:" + user.Id + ":gob"
	_, err = hc.Set(key, buffer.Bytes())
	if err != nil {
		return err
	}

	keys := []string{"Name", "GivenName", "FamilyName", "Email", "Gender", "Picture", "Birthday"}
	for _, key := range keys {
		rediskey := "u:" + user.Id + ":" + strings.ToLower(key)
		_, err = hc.Set(rediskey, reflect.ValueOf(user).Elem().FieldByName(key).String())
		if err != nil {
			return err
		}
	}

	err = user.PostLogin(c)
	return err
}
开发者ID:armen,项目名称:gapp,代码行数:65,代码来源:user.go



注:本文中的github.com/armen/hdis.Conn类示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。


鲜花

握手

雷人

路过

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

请发表评论

全部评论

专题导读
上一篇:
Golang circbuf.NewBuffer函数代码示例发布时间:2022-05-24
下一篇:
Golang buildfile.Buildfile类代码示例发布时间:2022-05-24
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

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

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

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