Exemple #1
0
// 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
}
Exemple #2
0
// DeleteMultiple deletes multiple items i from the store. It returns the count
// of items successfully deleted. It returns an error if any of the items do
// not exist or can't be deleted. It will delete the other items, in that case.
func (s *Redis) DeleteMultiple(items []store.Item) (int, error) {
	c := s.pool.Get()
	defer c.Close()

	keys := make([]interface{}, len(items))
	for i, item := range items {
		value := reflect.ValueOf(item).Elem()
		if len(item.Key()) > 0 {
			keys[i] = fmt.Sprintf("%s:%s", s.typeName(value), item.Key())
		}
	}

	count, err := driver.Int(c.Do("DEL", keys...))
	if err != nil {
		return 0, err
	}
	if count != len(keys) {
		return count, store.ErrKeyNotFound
	}

	return count, nil
}