本文整理匯總了Golang中github.com/gosuri/go-store/store.Item類的典型用法代碼示例。如果您正苦於以下問題:Golang Item類的具體用法?Golang Item怎麽用?Golang Item使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
在下文中一共展示了Item類的3個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: Write
// Write writes the item to the store. It constructs the key using the i.Key()
// and prefixes it with the type of struct. When the key is empty, it assigns
// a unique universal id(UUID) using the SetKey method of the Item
func (s *Store) Write(i store.Item) error {
c := s.pool.Get()
defer c.Close()
value := reflect.ValueOf(i).Elem()
ri := &Item{
prefix: s.typeName(value),
data: make(map[string]interface{}),
}
// Use the Items id if set or generate
// a new UUID
ri.key = i.Key()
if len(ri.key) == 0 {
ri.key = uuid.New()
}
i.SetKey(ri.key)
// convert the item to redis item
if err := marshall(i, value, ri); err != nil {
return err
}
for key, val := range ri.data {
if err := c.Send("HSET", ri.Key(), key, val); err != nil {
return err
}
c.Flush()
}
return nil
}
示例2: Delete
// Delete deletes the item from the store. It constructs the key using i.Key().
// When the key is empty, it returns a store.ErrEmptyKey error. When the key
// does not exist, it returns a store.ErrKeyNotFound error.
func (s *Redis) Delete(i store.Item) error {
c := s.pool.Get()
defer c.Close()
value := reflect.ValueOf(i).Elem()
ri := &item{
prefix: s.typeName(value),
}
ri.key = i.Key()
if len(ri.key) == 0 {
return store.ErrEmptyKey
}
count, err := driver.Int(c.Do("DEL", ri.Key()))
if err != nil {
return err
}
if count == 0 {
return store.ErrKeyNotFound
}
return nil
}
示例3: Read
// Read reads the item from redis store and copies the values to item
// It Returns store.ErrKeyNotFound when no values are found for the key provided
// and store.ErrKeyMissing when key is not provided. Unmarshalling id done using
// driver provided redis.ScanStruct
func (s *Store) Read(i store.Item) error {
c := s.pool.Get()
defer c.Close()
value := reflect.ValueOf(i).Elem()
if len(i.Key()) == 0 {
return store.ErrEmptyKey
}
ri := &Item{
key: i.Key(),
prefix: value.Type().Name(),
}
reply, err := driver.Values(c.Do("HGETALL", ri.Key()))
if err != nil {
return err
}
if len(reply) == 0 {
return store.ErrKeyNotFound
}
if err := driver.ScanStruct(reply, i); err != nil {
return err
}
return nil
}