背景:对于一些Redis里所提供的数据结构,用Hash可能是用得最多的,为何呢?因为日常中很多东西都可以用它来表示。
把一个复杂的需要序列化的东西变为一个HashTable进行存储,利用Hash存储节约内存,这样还可能更快更少的消耗实现高并发:
<?php
/** 每天下达指令的也就是x权限(每种鸡蛋的上下限),
而r就是只有看温度的权限,而w就是有设置温度权限 **/
$arr = array(
"tcp" =>array(
"ip"=>"192.168.1.1",
"fd"=>"2",
"mac"=>"00-50-56-C0-00-08",
"chineseName"=>"蛋壳108109",
"EnglishName"=>"LevooAllCanBeHatch"
),
"frame" =>array(
array(
"ip"=>"192.168.1.1",
"fd"=>"2",
"token"=>"jfdjfldjflkdjdjfkldf",
"isconnected"=>0,
"chmod"=>"rwx"
),
array(
"ip"=>"192.168.1.1",
"fd"=>"2",
"token"=>"jfdjfldjflkdjdj1fkl2df",
"isconnected"=>1,
"chmod"=>"rw"
),
array(
"ip"=>"192.168.1.1",
"fd"=>"2",
"token"=>"jfdjfldjflkdjdjfk33ld22f",
"isconnected"=>1,
"chmod"=>"x"
),
array(
"ip"=>"192.168.1.1",
"fd"=>"2",
"token"=>"jfdjfl323djdjfkld22f",
"isconnected"=>1,
"chmod"=>"rwx"
),
array(
"ip"=>"192.168.1.1",
"fd"=>"2",
"token"=>"jf1121fldjflkdjdjfkld22f",
"isconnected"=>1,
"chmod"=>"rwx"
)
)
);
print_r($arr);
echo json_encode($arr);
?>
命令示例:
1. HSET/HGET/HDEL/HEXISTS/HLEN/HSETNX:
#在Shell命令行启动Redis客户端程序
/> redis-cli
#给键值为myhash的键设置字段为field1,值为stephen。
redis 127.0.0.1:6379> hset myhash field1 "stephen"
(integer) 1
#获取键值为myhash,字段为field1的值。
redis 127.0.0.1:6379> hget myhash field1
"stephen"
#myhash键中不存在field2字段,因此返回nil。
redis 127.0.0.1:6379> hget myhash field2
(nil)
#给myhash关联的Hashes值添加一个新的字段field2,其值为liu。
redis 127.0.0.1:6379> hset myhash field2 "liu"
(integer) 1
#获取myhash键的字段数量。
redis 127.0.0.1:6379> hlen myhash
(integer) 2
#判断myhash键中是否存在字段名为field1的字段,由于存在,返回值为1。
redis 127.0.0.1:6379> hexists myhash field1
(integer) 1
#删除myhash键中字段名为field1的字段,删除成功返回1。
redis 127.0.0.1:6379> hdel myhash field1
(integer) 1
#再次删除myhash键中字段名为field1的字段,由于上一条命令已经将其删除,因为没有删除,返回0。
redis 127.0.0.1:6379> hdel myhash field1
(integer) 0
#判断myhash键中是否存在field1字段,由于上一条命令已经将其删除,因为返回0。
redis 127.0.0.1:6379> hexists myhash field1
(integer) 0
#通过hsetnx命令给myhash添加新字段field1,其值为stephen,因为该字段已经被删除,所以该命令添加成功并返回1。
redis 127.0.0.1:6379> hsetnx myhash field1 stephen
(integer) 1
#由于myhash的field1字段已经通过上一条命令添加成功,因为本条命令不做任何操作后返回0。
redis 127.0.0.1:6379> hsetnx myhash field1 stephen
(integer) 0
2. HINCRBY:
#删除该键,便于后面示例的测试。
redis 127.0.0.1:6379> del myhash
(integer) 1
#准备测试数据,该myhash的field字段设定值1。
redis 127.0.0.1:6379> hset myhash field 5
(integer) 1
#给myhash的field字段的值加1,返回加后的结果。
redis 127.0.0.1:6379> hincrby myhash field 1
(integer) 6
#给myhash的field字段的值加-1,返回加后的结果。
redis 127.0.0.1:6379> hincrby myhash field -1
(integer) 5
#给myhash的field字段的值加-10,返回加后的结果。
redis 127.0.0.1:6379> hincrby myhash field -10
(integer) -5
3. HGETALL/HKEYS/HVALS/HMGET/HMSET:
#删除该键,便于后面示例测试。
redis 127.0.0.1:6379> del myhash
(integer) 1
#为该键myhash,一次性设置多个字段,分别是field1 = "hello", field2 = "world"。
redis 127.0.0.1:6379> hmset myhash field1 "hello" field2 "world"
OK
#获取myhash键的多个字段,其中field3并不存在,因为在返回结果中与该字段对应的值为nil。
redis 127.0.0.1:6379> hmget myhash field1 field2 field3
1) "hello"
2) "world"
3) (nil)
#返回myhash键的所有字段及其值,从结果中可以看出,他们是逐对列出的。
redis 127.0.0.1:6379> hgetall myhash
1) "field1"
2) "hello"
3) "field2"
4) "world"
#仅获取myhash键中所有字段的名字。
redis 127.0.0.1:6379> hkeys myhash
1) "field1"
2) "field2"
#仅获取myhash键中所有字段的值。
redis 127.0.0.1:6379> hvals myhash
1) "hello"
2) "world"
来自:http://www.cnblogs.com/stephen-liu74/archive/2012/03/19/2352932.html
实践如下:
Redis学习手册(Hashes数据类型):
/usr/local/redis/bin/redis-cli -h 10.44.202.177 -p 6379
10.44.202.177:6379> hset myhash field1 "stephen"
(integer) 1
10.44.202.177:6379> hget myhash field1
"stephen"
10.44.202.177:6379> hget myhash field2
(nil)
10.44.202.177:6379> hset myhash field2 "liu"
(integer) 1
10.44.202.177:6379> hlen myhash
(integer) 2
10.44.202.177:6379> hexists myhash field1
(integer) 1
10.44.202.177:6379>
10.44.202.177:6379> hdel myhash field1
(integer) 1
10.44.202.177:6379> hdel myhash field1
(integer) 0
10.44.202.177:6379> hexists myhash field1
(integer) 0
10.44.202.177:6379> hsetnx myhash field1 stephen
(integer) 1
10.44.202.177:6379> hsetnx myhash field1 stephen
(integer) 0
10.44.202.177:6379> del myhash
(integer) 1
10.44.202.177:6379> hset myhash field 5
(integer) 1
10.44.202.177:6379> hincrby myhash field 1
(integer) 6
10.44.202.177:6379> hincrby myhash field -1
(integer) 5
10.44.202.177:6379> hincrby myhash field -10
(integer) -5
10.44.202.177:6379> del myhash
(integer) 1
10.44.202.177:6379> hmset myhash field1 "hello" field2 "world"
OK
10.44.202.177:6379> hmget myhash field1 field2 field3
1) "hello"
2) "world"
3) (nil)
10.44.202.177:6379> hgetall myhash
1) "field1"
2) "hello"
3) "field2"
4) "world"
10.44.202.177:6379> hkeys myhash
1) "field1"
2) "field2"
10.44.202.177:6379> hvals myhash
1) "hello"
2) "world"
PHP如何设置Hash:
<?php
$redis = new redis();
$redis->connect('10.51.77.34', 6379);
$redis->delete('test');
$redis->hset('test', 'key1', 'hello');
echo $redis->hget('test', 'key1'); //结果:hello
echo "<br>";
$redis->hSetNx('test', 'key1', 'world');
echo $redis->hget('test', 'key1'); //结果:hello
$redis->delete('test');
$redis->hSetNx('test', 'key1', 'world');
echo "<br>";
echo $redis->hget('test', 'key1'); //结果:world
echo $redis->hlen('test'); //结果:1
var_dump($redis->hdel('test','key1')); //结果:bool(true)
$redis->delete('test');
$redis->hSet('test', 'a', 'x');
$redis->hSet('test', 'b', 'y');
$redis->hSet('test', 'c', 'z');
print_r($redis->hkeys('test')); //结果:Array ( [0] => a [1] => b [2] => c )
print_r($redis->hvals('test')); //结果:Array ( [0] => x [1] => y [2] => z )
print_r($redis->hgetall('test')); //结果:Array ( [a] => x [b] => y [c] => z )
var_dump($redis->hExists('test', 'a')); //结果:bool(true)
$redis->delete('test');
echo $redis->hIncrBy('test', 'a', 3); //结果:3
echo $redis->hIncrBy('test', 'a', 1); //结果:4
$redis->delete('test');
var_dump($redis->hmset('test', array('name' =>'tank', 'sex'=>"man"))); //结果:bool(true)
print_r($redis->hmget('test', array('name', 'sex'))); //结果:Array ( [name] => tank [sex] => man )
$redis->hSet("hashA", "name", "iname");
$redis->hSet("hashA", "age", "age");
// 同时设置多个值
$redis->hMset("hashA", [
"gender" => "male",
"salary" => 12000
]);
$redis->hGet("hashA", "salary");
// 获得多个值
var_dump($redis->hMGet("hashA", ["name", "gender"]));
?>
php redishash.php
hello<br>hello<br>world1int(1)
Array
(
[0] => a
[1] => b
[2] => c
)
Array
(
[0] => x
[1] => y
[2] => z
)
Array
(
[a] => x
[b] => y
[c] => z
)
bool(true)
34bool(true)
Array
(
[name] => tank
[sex] => man
)
array(2) {
["name"]=>
string(5) "iname"
["gender"]=>
string(4) "male"
}
来自:http://blog.csdn.net/qjwcn/article/details/45293035
把一个复杂的需要序列化的东西变为一个HashTable进行存储,利用Hash存储节约内存,这样还可能更快更少的消耗实现高并发:
<?php
/** 每天下达指令的也就是x权限(每种鸡蛋的上下限),
而r就是只有看温度的权限,而w就是有设置温度权限 **/
$arr = array(
"tcp" =>array(
"ip"=>"192.168.1.1",
"fd"=>"2",
"mac"=>"00-50-56-C0-00-08",
"chineseName"=>"蛋壳108109",
"EnglishName"=>"LevooAllCanBeHatch"
),
"frame" =>array(
array(
"ip"=>"192.168.1.1",
"fd"=>"2",
"token"=>"jfdjfldjflkdjdjfkldf",
"isconnected"=>0,
"chmod"=>"rwx"
),
array(
"ip"=>"192.168.1.1",
"fd"=>"2",
"token"=>"jfdjfldjflkdjdj1fkl2df",
"isconnected"=>1,
"chmod"=>"rw"
),
array(
"ip"=>"192.168.1.1",
"fd"=>"2",
"token"=>"jfdjfldjflkdjdjfk33ld22f",
"isconnected"=>1,
"chmod"=>"x"
),
array(
"ip"=>"192.168.1.1",
"fd"=>"2",
"token"=>"jfdjfl323djdjfkld22f",
"isconnected"=>1,
"chmod"=>"rwx"
),
array(
"ip"=>"192.168.1.1",
"fd"=>"2",
"token"=>"jf1121fldjflkdjdjfkld22f",
"isconnected"=>1,
"chmod"=>"rwx"
)
)
);
print_r($arr);
echo json_encode($arr);
?>
命令示例:
1. HSET/HGET/HDEL/HEXISTS/HLEN/HSETNX:
#在Shell命令行启动Redis客户端程序
/> redis-cli
#给键值为myhash的键设置字段为field1,值为stephen。
redis 127.0.0.1:6379> hset myhash field1 "stephen"
(integer) 1
#获取键值为myhash,字段为field1的值。
redis 127.0.0.1:6379> hget myhash field1
"stephen"
#myhash键中不存在field2字段,因此返回nil。
redis 127.0.0.1:6379> hget myhash field2
(nil)
#给myhash关联的Hashes值添加一个新的字段field2,其值为liu。
redis 127.0.0.1:6379> hset myhash field2 "liu"
(integer) 1
#获取myhash键的字段数量。
redis 127.0.0.1:6379> hlen myhash
(integer) 2
#判断myhash键中是否存在字段名为field1的字段,由于存在,返回值为1。
redis 127.0.0.1:6379> hexists myhash field1
(integer) 1
#删除myhash键中字段名为field1的字段,删除成功返回1。
redis 127.0.0.1:6379> hdel myhash field1
(integer) 1
#再次删除myhash键中字段名为field1的字段,由于上一条命令已经将其删除,因为没有删除,返回0。
redis 127.0.0.1:6379> hdel myhash field1
(integer) 0
#判断myhash键中是否存在field1字段,由于上一条命令已经将其删除,因为返回0。
redis 127.0.0.1:6379> hexists myhash field1
(integer) 0
#通过hsetnx命令给myhash添加新字段field1,其值为stephen,因为该字段已经被删除,所以该命令添加成功并返回1。
redis 127.0.0.1:6379> hsetnx myhash field1 stephen
(integer) 1
#由于myhash的field1字段已经通过上一条命令添加成功,因为本条命令不做任何操作后返回0。
redis 127.0.0.1:6379> hsetnx myhash field1 stephen
(integer) 0
2. HINCRBY:
#删除该键,便于后面示例的测试。
redis 127.0.0.1:6379> del myhash
(integer) 1
#准备测试数据,该myhash的field字段设定值1。
redis 127.0.0.1:6379> hset myhash field 5
(integer) 1
#给myhash的field字段的值加1,返回加后的结果。
redis 127.0.0.1:6379> hincrby myhash field 1
(integer) 6
#给myhash的field字段的值加-1,返回加后的结果。
redis 127.0.0.1:6379> hincrby myhash field -1
(integer) 5
#给myhash的field字段的值加-10,返回加后的结果。
redis 127.0.0.1:6379> hincrby myhash field -10
(integer) -5
3. HGETALL/HKEYS/HVALS/HMGET/HMSET:
#删除该键,便于后面示例测试。
redis 127.0.0.1:6379> del myhash
(integer) 1
#为该键myhash,一次性设置多个字段,分别是field1 = "hello", field2 = "world"。
redis 127.0.0.1:6379> hmset myhash field1 "hello" field2 "world"
OK
#获取myhash键的多个字段,其中field3并不存在,因为在返回结果中与该字段对应的值为nil。
redis 127.0.0.1:6379> hmget myhash field1 field2 field3
1) "hello"
2) "world"
3) (nil)
#返回myhash键的所有字段及其值,从结果中可以看出,他们是逐对列出的。
redis 127.0.0.1:6379> hgetall myhash
1) "field1"
2) "hello"
3) "field2"
4) "world"
#仅获取myhash键中所有字段的名字。
redis 127.0.0.1:6379> hkeys myhash
1) "field1"
2) "field2"
#仅获取myhash键中所有字段的值。
redis 127.0.0.1:6379> hvals myhash
1) "hello"
2) "world"
来自:http://www.cnblogs.com/stephen-liu74/archive/2012/03/19/2352932.html
实践如下:
Redis学习手册(Hashes数据类型):
/usr/local/redis/bin/redis-cli -h 10.44.202.177 -p 6379
10.44.202.177:6379> hset myhash field1 "stephen"
(integer) 1
10.44.202.177:6379> hget myhash field1
"stephen"
10.44.202.177:6379> hget myhash field2
(nil)
10.44.202.177:6379> hset myhash field2 "liu"
(integer) 1
10.44.202.177:6379> hlen myhash
(integer) 2
10.44.202.177:6379> hexists myhash field1
(integer) 1
10.44.202.177:6379>
10.44.202.177:6379> hdel myhash field1
(integer) 1
10.44.202.177:6379> hdel myhash field1
(integer) 0
10.44.202.177:6379> hexists myhash field1
(integer) 0
10.44.202.177:6379> hsetnx myhash field1 stephen
(integer) 1
10.44.202.177:6379> hsetnx myhash field1 stephen
(integer) 0
10.44.202.177:6379> del myhash
(integer) 1
10.44.202.177:6379> hset myhash field 5
(integer) 1
10.44.202.177:6379> hincrby myhash field 1
(integer) 6
10.44.202.177:6379> hincrby myhash field -1
(integer) 5
10.44.202.177:6379> hincrby myhash field -10
(integer) -5
10.44.202.177:6379> del myhash
(integer) 1
10.44.202.177:6379> hmset myhash field1 "hello" field2 "world"
OK
10.44.202.177:6379> hmget myhash field1 field2 field3
1) "hello"
2) "world"
3) (nil)
10.44.202.177:6379> hgetall myhash
1) "field1"
2) "hello"
3) "field2"
4) "world"
10.44.202.177:6379> hkeys myhash
1) "field1"
2) "field2"
10.44.202.177:6379> hvals myhash
1) "hello"
2) "world"
PHP如何设置Hash:
<?php
$redis = new redis();
$redis->connect('10.51.77.34', 6379);
$redis->delete('test');
$redis->hset('test', 'key1', 'hello');
echo $redis->hget('test', 'key1'); //结果:hello
echo "<br>";
$redis->hSetNx('test', 'key1', 'world');
echo $redis->hget('test', 'key1'); //结果:hello
$redis->delete('test');
$redis->hSetNx('test', 'key1', 'world');
echo "<br>";
echo $redis->hget('test', 'key1'); //结果:world
echo $redis->hlen('test'); //结果:1
var_dump($redis->hdel('test','key1')); //结果:bool(true)
$redis->delete('test');
$redis->hSet('test', 'a', 'x');
$redis->hSet('test', 'b', 'y');
$redis->hSet('test', 'c', 'z');
print_r($redis->hkeys('test')); //结果:Array ( [0] => a [1] => b [2] => c )
print_r($redis->hvals('test')); //结果:Array ( [0] => x [1] => y [2] => z )
print_r($redis->hgetall('test')); //结果:Array ( [a] => x [b] => y [c] => z )
var_dump($redis->hExists('test', 'a')); //结果:bool(true)
$redis->delete('test');
echo $redis->hIncrBy('test', 'a', 3); //结果:3
echo $redis->hIncrBy('test', 'a', 1); //结果:4
$redis->delete('test');
var_dump($redis->hmset('test', array('name' =>'tank', 'sex'=>"man"))); //结果:bool(true)
print_r($redis->hmget('test', array('name', 'sex'))); //结果:Array ( [name] => tank [sex] => man )
$redis->hSet("hashA", "name", "iname");
$redis->hSet("hashA", "age", "age");
// 同时设置多个值
$redis->hMset("hashA", [
"gender" => "male",
"salary" => 12000
]);
$redis->hGet("hashA", "salary");
// 获得多个值
var_dump($redis->hMGet("hashA", ["name", "gender"]));
?>
php redishash.php
hello<br>hello<br>world1int(1)
Array
(
[0] => a
[1] => b
[2] => c
)
Array
(
[0] => x
[1] => y
[2] => z
)
Array
(
[a] => x
[b] => y
[c] => z
)
bool(true)
34bool(true)
Array
(
[name] => tank
[sex] => man
)
array(2) {
["name"]=>
string(5) "iname"
["gender"]=>
string(4) "male"
}
来自:http://blog.csdn.net/qjwcn/article/details/45293035
作者:jackxiang@向东博客 专注WEB应用 构架之美 --- 构架之美,在于尽态极妍 | 应用之美,在于药到病除
地址:https://jackxiang.com/post/9251/
版权所有。转载时必须以链接形式注明作者和原始出处及本声明!
评论列表