1. Redis Hash 데이터 사용 샘플 소스
- <?php
- $redis = new Redis();
- try {
- $redis->connect('127.0.0.1', 6379, 2.5);//2.5 sec timeout
- //Auth Password(redis.conf requirepass)
- //$redis->auth('foobared');
- } catch (Exception $e) {
- exit( "Cannot connect to redis server : ".$e->getMessage() );
- }
- // //DB Select
- if ( !$redis->select(0) ){
- exit( "NOT DB Select");
- }
- $redis->delete('user_list');
- $redis->hSet('user_list', 'uid1', 1);
- $redis->hSet('user_list', 'uid2', 2);
- $redis->hSet('user_list', 'uid3', 3);
- if ( $redis->hExists('user_list','uid1') ){
- echo "uid1 : ".$redis->hGet('user_list','uid1')."\n";
- }else{
- echo 'NOT_DATA';
- }
- //여러개 HASH 데이터 한번에 입력
- $redis->hMset('user_list', array('uid4' => 4, 'uid5' =>5));
- //여러개 HASH 데이터를 한번에 가져옴
- var_dump($redis->hMGet('user_list', array('uid4', 'uid5')));
- var_dump($redis->hGetAll('user_list'));
- /*
- uid1 : 1
- array(2) {
- ["uid4"]=>
- string(1) "4"
- ["uid5"]=>
- string(1) "5"
- }
- array(5) {
- ["uid1"]=>
- string(1) "1"
- ["uid2"]=>
- string(1) "2"
- ["uid3"]=>
- string(1) "3"
- ["uid4"]=>
- string(1) "4"
- ["uid5"]=>
- string(1) "5"
- }
- */
- ?>
2. Redis 들어있는 데이터
3. 추가 명령어 메뉴얼 참조
- https://github.com/nicolasff/phpredis#hashes
hDel - Delete one or more hash fields
hExists - Determine if a hash field exists
hGet - Get the value of a hash field
hGetAll - Get all the fields and values in a hash
hIncrBy - Increment the integer value of a hash field by the given number
hIncrByFloat - Increment the float value of a hash field by the given amount
hKeys - Get all the fields in a hash
hLen - Get the number of fields in a hash
hMGet - Get the values of all the given hash fields
hMSet - Set multiple hash fields to multiple values
hSet - Set the string value of a hash field
hSetNx - Set the value of a hash field, only if the field does not exist
hVals - Get all the values in a hash
hScan - Scan a hash key for members