哈希(hash)函數redis
hSet 命令/方法/函數 Adds a value to the hash stored at key. If this value is already in the hash, FALSE is returned. 添加一個VALUE到HASH中。若是VALUE已經存在於HASH中,則返回FALSE。 Parameters key hashKey value Return value LONG 1 if value didn't exist and was added successfully, 0 if the value was already present and was replaced, FALSE if there was an error. Example $redis->delete('h') $redis->hSet('h', 'key1', 'hello'); /* 1, 'key1' => 'hello' in the hash at "h" */ $redis->hGet('h', 'key1'); /* returns "hello" */ $redis->hSet('h', 'key1', 'plop'); /* 0, value was replaced. */ $redis->hGet('h', 'key1'); /* returns "plop" */
hSetNx 命令/方法/函數 Adds a value to the hash stored at key only if this field isn't already in the hash. 添加一個VALUE到HASH STORE中,若是FIELD不存在。 Return value BOOL TRUE if the field was set, FALSE if it was already present. Example $redis->delete('h') $redis->hSetNx('h', 'key1', 'hello'); /* TRUE, 'key1' => 'hello' in the hash at "h" */ $redis->hSetNx('h', 'key1', 'world'); /* FALSE, 'key1' => 'hello' in the hash at "h". No change since the field wasn't replaced. */
hGet 命令/方法/函數 Gets a value from the hash stored at key. If the hash table doesn't exist, or the key doesn't exist, FALSE is returned. 取得HASH中的VALUE,如何HASH不存在,或者KEY不存在返回FLASE。 Parameters key hashKey Return value STRING The value, if the command executed successfully BOOL FALSE in case of failure
hLen 命令/方法/函數 Returns the length of a hash, in number of items 取得HASH表的長度。 Parameters key Return value LONG the number of items in a hash, FALSE if the key doesn't exist or isn't a hash. Example $redis->delete('h') $redis->hSet('h', 'key1', 'hello'); $redis->hSet('h', 'key2', 'plop'); $redis->hLen('h'); /* returns 2 */
hDel 命令/方法/函數 Removes a value from the hash stored at key. If the hash table doesn't exist, or the key doesn't exist, FALSE is returned. 刪除指定的元素。 Parameters key hashKey Return value BOOL TRUE in case of success, FALSE in case of failure
hKeys 命令/方法/函數 Returns the keys in a hash, as an array of strings. 取得HASH表中的KEYS,以數組形式返回。 Parameters Key: key Return value An array of elements, the keys of the hash. This works like PHP's array_keys(). Example $redis->delete('h'); $redis->hSet('h', 'a', 'x'); $redis->hSet('h', 'b', 'y'); $redis->hSet('h', 'c', 'z'); $redis->hSet('h', 'd', 't'); var_dump($redis->hKeys('h')); Output: array(4) { [0]=> string(1) "a" [1]=> string(1) "b" [2]=> string(1) "c" [3]=> string(1) "d" }
hVals 命令/方法/函數 Returns the values in a hash, as an array of strings. 取得HASH表中全部的VALUE,以數組形式返回。 Parameters Key: key Return value An array of elements, the values of the hash. This works like PHP's array_values(). Example $redis->delete('h'); $redis->hSet('h', 'a', 'x'); $redis->hSet('h', 'b', 'y'); $redis->hSet('h', 'c', 'z'); $redis->hSet('h', 'd', 't'); var_dump($redis->hVals('h')); Output: array(4) { [0]=> string(1) "x" [1]=> string(1) "y" [2]=> string(1) "z" [3]=> string(1) "t" }
hGetAll 命令/方法/函數 Returns the whole hash, as an array of strings indexed by strings. 取得整個HASH表的信息,返回一個以KEY爲索引VALUE爲內容的數組。 Parameters Key: key Return value An array of elements, the contents of the hash. Example $redis->delete('h'); $redis->hSet('h', 'a', 'x'); $redis->hSet('h', 'b', 'y'); $redis->hSet('h', 'c', 'z'); $redis->hSet('h', 'd', 't'); var_dump($redis->hGetAll('h')); Output: array(4) { ["a"]=> string(1) "x" ["b"]=> string(1) "y" ["c"]=> string(1) "z" ["d"]=> string(1) "t" }
hExists 命令/方法/函數 Verify if the specified member exists in a key. 驗證HASH表中是否存在指定的KEY-VALUE Parameters key memberKey Return value BOOL: If the member exists in the hash table, return TRUE, otherwise return FALSE. Examples $redis->hSet('h', 'a', 'x'); $redis->hExists('h', 'a'); /* TRUE */ $redis->hExists('h', 'NonExistingKey'); /* FALSE */
hIncrBy 命令/方法/函數 Increments the value of a member from a hash by a given amount. 根據HASH表的KEY,爲KEY對應的VALUE自增參數VALUE。 Parameters key member value: (integer) value that will be added to the member's value Return value LONG the new value Examples $redis->delete('h'); $redis->hIncrBy('h', 'x', 2); /* returns 2: h[x] = 2 now. */ $redis->hIncrBy('h', 'x', 1); /* h[x] ← 2 + 1. Returns 3 */
hIncrByFloat 命令/方法/函數 Increments the value of a hash member by the provided float value 根據HASH表的KEY,爲KEY對應的VALUE自增參數VALUE。浮點型 Parameters key member value: (float) value that will be added to the member's value Return value FLOAT the new value Examples $redis->delete('h'); $redis->hIncrByFloat('h','x', 1.5); /* returns 1.5: h[x] = 1.5 now */ $redis->hIncrByFLoat('h', 'x', 1.5); /* returns 3.0: h[x] = 3.0 now */ $redis->hIncrByFloat('h', 'x', -3.0); /* returns 0.0: h[x] = 0.0 now */
hMset 命令/方法/函數 Fills in a whole hash. Non-string values are converted to string, using the standard (string) cast. NULL values are stored as empty strings. 批量填充HASH表。不是字符串類型的VALUE,自動轉換成字符串類型。使用標準的值。NULL值將被儲存爲一個空的字符串。 Parameters key members: key → value array Return value BOOL Examples $redis->delete('user:1'); $redis->hMset('user:1', array('name' => 'Joe', 'salary' => 2000)); $redis->hIncrBy('user:1', 'salary', 100); // Joe earns 100 more now.
hMGet 命令/方法/函數 Retrieve the values associated to the specified fields in the hash. 批量取得HASH表中的VALUE。 Parameters key memberKeys Array Return value Array An array of elements, the values of the specified fields in the hash, with the hash keys as array keys. Examples $redis->delete('h'); $redis->hSet('h', 'field1', 'value1'); $redis->hSet('h', 'field2', 'value2'); $redis->hmGet('h', array('field1', 'field2')); /* returns array('field1' => 'value1', 'field2' => 'value2') */