本文整理汇总了Golang中github.com/armen/hdis.Conn.Set方法的典型用法代码示例。如果您正苦于以下问题:Golang Conn.Set方法的具体用法?Golang Conn.Set怎么用?Golang Conn.Set使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类github.com/armen/hdis.Conn
的用法示例。
在下文中一共展示了Conn.Set方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: 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
}
示例2: 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
}
示例3: 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
}