function testDeleteFromHash() { $scope = __FUNCTION__; Redis::config(array('format' => $scope)); $this->assertTrue($this->redis->hMset("{$scope}:foo", array('name' => 'Joe', 'salary' => 2000))); $this->assertTrue($this->redis->hExists("{$scope}:foo", 'name')); $this->assertTrue($this->redis->hExists("{$scope}:foo", 'salary')); $this->assertEqual(1, Redis::deleteFromHash('foo', 'name')); $this->assertFalse($this->redis->hExists("{$scope}:foo", 'name')); $this->assertTrue($this->redis->hExists("{$scope}:foo", 'salary')); $this->assertTrue($this->redis->hMset("{$scope}:foo", array('name' => 'Joe', 'salary' => 2000))); $expected = array('name' => true, 'salary' => true); $this->assertEqual($expected, Redis::deleteFromHash('foo', array('name', 'salary'))); $this->assertFalse($this->redis->hExists("{$scope}:foo", 'name')); $this->assertFalse($this->redis->hExists("{$scope}:foo", 'salary')); $this->assertTrue($this->redis->hMset("{$scope}:foo", array('name' => 'Joe', 'salary' => 2000))); $expected = array('name' => true, 'salary' => true, 'non-existent' => false); $this->assertEqual($expected, Redis::deleteFromHash('foo', array('name', 'salary', 'non-existent'))); $this->assertFalse($this->redis->hExists("{$scope}:foo", 'name')); $this->assertFalse($this->redis->hExists("{$scope}:foo", 'salary')); $this->assertFalse($this->redis->hExists("{$scope}:foo", 'non-existent')); }
/** * delete given values from Stats, or a whole stats set at once * * {{{ * Stats::delete('foo'); // will remove the whole set * Stats::delete('foo', 'field'); // will remove `field` from `foo` * Stats::delete('foo', array('field')); // same as above * Stats::delete('foo', array('field1', 'field2')); // will remove both fields from `foo` * }}} * * * @see li3_redis\storage\Stats::inc() * @see li3_redis\storage\Redis::getKey() * @param string $key redis key which identifies the hash * @param string|array $fields a string to remove a field from given hash or an array thereof. * @param string|array $buckets an array of additional prefixes, can be a numerical indexed * array with strings, or an associated array, in which the key and value will be glued * together by a separater or just a string, for one additional prefix. * @param array $options array with additional options, see Redis::getKey() * @return integer count of values that has been removed * @filter */ public static function delete($key, $fields = null, $buckets = 'global', array $options = array()) { $defaults = array('namespace' => static::$namespace); $options += $defaults; $params = compact('key', 'fields', 'buckets', 'options'); return static::_filter(__METHOD__, $params, function ($self, $params) { extract($params); $result = array(); $buckets = !is_array($buckets) ? array($buckets) : $buckets; foreach ($buckets as $prefix => $val) { $options['prefix'] = !is_numeric($prefix) ? Redis::addPrefix($val, $prefix) : $val; if (empty($fields)) { $result[] = Redis::delete($key, $options); } else { $return = Redis::deleteFromHash($key, $fields, $options); $result[] = is_numeric($return) ? $return : array_sum($return); } } return array_sum($result); }); }