Programming/PHP2014. 9. 16. 18:04

1. Redis List 데이터 사용 샘플소스


  1. <?php
  2. $redis = new Redis();
  3. try {
  4. $redis->connect('127.0.0.1', 6379, 2.5);//2.5 sec timeout
  5. //Auth Password(redis.conf requirepass)
  6. //$redis->auth('foobared');
  7. } catch (Exception $e) {
  8. exit( "Cannot connect to redis server : ".$e->getMessage() );
  9. }
  10. // //DB Select
  11. if ( !$redis->select(0) ){
  12. exit( "NOT DB Select");
  13. }
  14. $redis->delete('weblog:ws1');
  15. //rPush 맨아래 배열로 입력
  16. $redis->rPush('weblog:ws1', 'log1');
  17. $redis->rPush('weblog:ws1', 'log2');
  18. $redis->rPush('weblog:ws1', 'log3');
  19. $redis->rPush('weblog:ws1', 'log4');
  20. //lPush 첫번째 배열로 입력
  21. $redis->lPush('weblog:ws1', 'log5');
  22. //0~2번까지배열 가져오기
  23. var_dump($redis->lRange('weblog:ws1', 0, 2));
  24. //전체배열데이터 가져오기
  25. var_dump($redis->lRange('weblog:ws1', 0, -1));
  26. ?>

2. Redis 들어있는 데이터



3. 추가명령어 메뉴얼참조

https://github.com/nicolasff/phpredis#lists

blPop, brPop - Remove and get the first/last element in a list

brpoplpush - Pop a value from a list, push it to another list and return it

lIndex, lGet - Get an element from a list by its index

lInsert - Insert an element before or after another element in a list

lLen, lSize - Get the length/size of a list

lPop - Remove and get the first element in a list

lPush - Prepend one or multiple values to a list

lPushx - Prepend a value to a list, only if the list exists

lRange, lGetRange - Get a range of elements from a list

lRem, lRemove - Remove elements from a list

lSet - Set the value of an element in a list by its index

lTrim, listTrim - Trim a list to the specified range

rPop - Remove and get the last element in a list

rpoplpush - Remove the last element in a list, append it to another list and return it (redis >= 1.1)

rPush - Append one or multiple values to a list

rPushx - Append a value to a list, only if the list exists

Posted by 시니^^
Programming/PHP2014. 9. 16. 16:26


1. Redis Hash 데이터 사용 샘플 소스


  1. <?php
  2. $redis = new Redis();
  3. try {
  4. $redis->connect('127.0.0.1', 6379, 2.5);//2.5 sec timeout
  5. //Auth Password(redis.conf requirepass)
  6. //$redis->auth('foobared');
  7. } catch (Exception $e) {
  8. exit( "Cannot connect to redis server : ".$e->getMessage() );
  9. }
  10. // //DB Select
  11. if ( !$redis->select(0) ){
  12. exit( "NOT DB Select");
  13. }
  14. $redis->delete('user_list');
  15. $redis->hSet('user_list', 'uid1', 1);
  16. $redis->hSet('user_list', 'uid2', 2);
  17. $redis->hSet('user_list', 'uid3', 3);
  18. if ( $redis->hExists('user_list','uid1') ){
  19. echo "uid1 : ".$redis->hGet('user_list','uid1')."\n";
  20. }else{
  21. echo 'NOT_DATA';
  22. }
  23. //여러개 HASH 데이터 한번에 입력
  24. $redis->hMset('user_list', array('uid4' => 4, 'uid5' =>5));
  25. //여러개 HASH 데이터를 한번에 가져옴
  26. var_dump($redis->hMGet('user_list', array('uid4', 'uid5')));
  27. var_dump($redis->hGetAll('user_list'));
  28. /*
  29. * Print
  30. uid1 : 1
  31. array(2) {
  32. ["uid4"]=>
  33. string(1) "4"
  34. ["uid5"]=>
  35. string(1) "5"
  36. }
  37. array(5) {
  38. ["uid1"]=>
  39. string(1) "1"
  40. ["uid2"]=>
  41. string(1) "2"
  42. ["uid3"]=>
  43. string(1) "3"
  44. ["uid4"]=>
  45. string(1) "4"
  46. ["uid5"]=>
  47. string(1) "5"
  48. }
  49. */
  50. ?>


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

Posted by 시니^^
Programming/PHP2014. 9. 16. 16:07

Connect와 기본 keys-and-strings 샘플자료


  1. <?php
  2. $redis = new Redis();
  3. try {
  4. $redis->connect('127.0.0.1', 6379, 2.5);//2.5 sec timeout
  5. //Auth Password(redis.conf requirepass)
  6. //$redis->auth('foobared');
  7. } catch (Exception $e) {
  8. exit( "Cannot connect to redis server : ".$e->getMessage() );
  9. }
  10. // //DB Select
  11. if ( !$redis->select(0) ){
  12. exit( "NOT DB Select");
  13. }
  14. $redis->set('key1','value1',10); //유효기간 10초
  15. echo $redis->get('key1');
  16. /*
  17. * print : value1
  18. */
  19. ?>


자세한 명령어 사용법은 아래 메뉴얼참조

https://github.com/nicolasff/phpredis#keys-and-strings


Strings

append - Append a value to a key

bitcount - Count set bits in a string

bitop - Perform bitwise operations between strings

decr, decrBy - Decrement the value of a key

get - Get the value of a key

getBit - Returns the bit value at offset in the string value stored at key

getRange - Get a substring of the string stored at a key

getSet - Set the string value of a key and return its old value

incr, incrBy - Increment the value of a key

incrByFloat - Increment the float value of a key by the given amount

mGet, getMultiple - Get the values of all the given keys

mSet, mSetNX - Set multiple keys to multiple values

set - Set the string value of a key

setBit - Sets or clears the bit at offset in the string value stored at key

setex, psetex - Set the value and expiration of a key

setnx - Set the value of a key, only if the key does not exist

setRange - Overwrite part of a string at key starting at the specified offset

strlen - Get the length of the value stored in a key


Keys

del, delete - Delete a key

dump - Return a serialized version of the value stored at the specified key.

exists - Determine if a key exists

expire, setTimeout, pexpire - Set a key's time to live in seconds

expireAt, pexpireAt - Set the expiration for a key as a UNIX timestamp

keys, getKeys - Find all keys matching the given pattern

scan - Scan for keys in the keyspace (Redis >= 2.8.0)

migrate - Atomically transfer a key from a Redis instance to another one

move - Move a key to another database

object - Inspect the internals of Redis objects

persist - Remove the expiration from a key

randomKey - Return a random key from the keyspace

rename, renameKey - Rename a key

renameNx - Rename a key, only if the new key does not exist

type - Determine the type stored at key

sort - Sort the elements in a list, set or sorted set

ttl, pttl - Get the time to live for a key

restore - Create a key using the provided serialized value, previously obtained with dump.

Posted by 시니^^