redis simple command


############## Expire ###########

-- 입력 -- 
set resource:lock "redis demo"    

-- 시간 타임 --
expire resource:lock 120
 - return 1 정상적으로 적용

-- 얼마나 남았는지 확인 --
ttl resource:lock 113 

113 second left

############ List ##############

command : rpush, lpush, llen, lrange, lpop, rpop

rpush  puts the new value at the end of the list 
-> rpush friends "alice" 

plush puts the new value at the start of the list
-> plush friends "sam"

large gives a subset of the list it takes the index of the first element you what to retrieve as its first parameter and the index of the last element you what to retrieve as tis second parameter. A value of -1 for the second parameter means to retrieve elements until the end of the list.
-> lrange friends 0 -1  (All)
-> lrange friends 0 1
-> lrange friends 1 2  

llen returns the current length of the list.
-> llen friends 

lpop removes the first element from the list and return it
-> lpop friends 

rpop removes the last element from the list and returns it
-> rpop friends 


이거는 java 에서 set 과 얼추 비슷한다.
하나의 set에는 동일한 데이터가 들어갈 수 없다.


############ SET #################

SADD adds the given value to the set.


    SADD superpowers "flight"
    SADD superpowers "x-ray vision"
    SADD superpowers "reflexes"
SREM removes the given value from the set.




SREM superpowers "reflexes" 



SISMEMBER tests if the given value is in the set. It returns 1 if the value is there and 0 if it is not.


    SISMEMBER superpowers "flight" => 1
    SISMEMBER superpowers "reflexes" => 0
SMEMBERS returns a list of all the members of this set.


    SMEMBERS superpowers => 1) "flight", 2) "x-ray vision"
SUNION combines two or more sets and returns the list of all elements.


    SADD birdpowers "pecking"
    SADD birdpowers "flight"
    SUNION superpowers birdpowers => 1) "pecking", 2) "x-ray vision", 3) "flight"

######## HASH ##########

Hashes are maps between string fields and string values, so they are the perfect data type to represent objects (eg: A User with a number of fields like name, surname, age, and so forth):


    HSET user:1000 name "John Smith"
    HSET user:1000 email "john.smith@example.com"
    HSET user:1000 password "s3cret"
To get back the saved data use HGETALL:


    HGETALL user:1000
You can also set multiple fields at once:


    HMSET user:1001 name "Mary Jones" password "hidden" email "mjones@example.com"
If you only need a single field value that is possible as well:


    HGET user:1001 name => "Mary Jones"



Numerical values in hash fields are handled exactly the same as in simple strings and there are operations to increment this value in an atomic way.


    HSET user:1000 visits 10
    HINCRBY user:1000 visits 1 => 11
    HINCRBY user:1000 visits 10 => 21
    HDEL user:1000 visits
    HINCRBY user:1000 visits 1 => 1

Check the full list of Hash commands for more information.

Comments

Popular posts from this blog

Project euler 169 found clue

Floyd-Warshall's algorithm