예제 #1
0
 function testWriteHash()
 {
     $scope = __FUNCTION__;
     Redis::config(array('format' => $scope));
     $expected = array('name' => 'Joe', 'salary' => '2000');
     $this->assertEqual($expected, Redis::writeHash('foo', array('name' => 'Joe', 'salary' => '2000')));
     $this->assertEqual($expected, $this->redis->hGetAll("{$scope}:foo"));
     $expected = array('name' => 'John', 'title' => 'manager', 'salary' => '2000');
     $this->assertEqual($expected, Redis::writeHash('foo', array('name' => 'John', 'title' => 'manager')));
     $this->assertEqual($expected, $this->redis->hGetAll("{$scope}:foo"));
 }
예제 #2
0
 /**
  * sets given values
  *
  * {{{
  *   Stats::set('foo', array('field' => 'value'));
  *   Stats::set('foo', array('field1' => 'value1', 'field2' => 'value2'));
  * }}}
  *
  * @see li3_redis\storage\Stats::inc()
  * @see li3_redis\storage\Redis::getKey()
  * @param string $key redis key in which to store the hash
  * @param array $values an array of values to store, the format is
  *        array($name => $value) and can contain an unlimited number of stats to save.
  * @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 mixed the new value that has been stored or an array of values
  * @filter
  */
 public static function set($key, $values, $buckets = 'global', array $options = array())
 {
     $defaults = array('namespace' => static::$namespace);
     $options += $defaults;
     $params = compact('key', 'values', 'buckets', 'options');
     return static::_filter(__METHOD__, $params, function ($self, $params) {
         extract($params);
         $result = array();
         $values = is_string($values) ? array($values => 1) : $values;
         $buckets = !is_array($buckets) ? array($buckets) : $buckets;
         foreach ($buckets as $prefix => $val) {
             $options['prefix'] = !is_numeric($prefix) ? Redis::addPrefix($val, $prefix) : $val;
             $result[$options['prefix']] = Redis::writeHash($key, $values, $options);
         }
         return count($result) > 1 ? $result : array_shift($result);
     });
 }