What is Redis?
Redis is a database that stores data in the form of key-value. We may store certain data that to be obtained in a SQL database would be necessary to sweep across the bank, for example, know the total number of something. When we use Redis, we can define a key associated with a value (total number of N). In this way, we can get the value faster.
The values do not need to be just numbers. They can be words, lists, sets, among others.
Official Site : http://redis.io/
How to install Redis ?
Follow : http://redis.io/download
The first steps
# To connect
- Starting:
After performing the installation, use the command prompt to enter the directory where the Redis is installed, and within the src folder run the redis-server file:
$ cd redis-redis-3.0.6/src
$ ./redis-server
- Connecting :
In another window, run the redis-cli file so that we can connect to the server.
$ cd src
$ ./redis-cli
- Testing :
$ ECHO “Hello World!”
# To set or update a key and value :
SET "total_number" 1000
SET "total_n" 144
# To get a key and value :
GET "
total_number
" GET "
total_n
"
# To delete :
DEL "key"
# To insert multiples key values
SET <key> <value> <key> <value> … <key> <value>
# To list keys
KEY *
Filters to Search
# To list keys by filter *
KEY <string_part_name*>
Example : KEY “total_*” –> It will return all keys that started with total_
# To list keys by filter ?
KEY <string_p?rt_name>
Example : KEY “t?tal_*” –> It will return all keys that started with t<1 n value>tal_
# To list keys by filter [ ]
KEY <string_key_name_position[value1value2]>
Example : KEY<string_key_name_position[19]>–> It will return all keys that started with string_key_name_position and these ends with 1 or 9
# To work with composite values ( hashes )
## To SET values : ( HSET )
HSET data:01-01-2016:total "places" "BRA, ARG"
HSET data:01-01-2016:total "access" 23
## To GET values: ( HGET )
HGET
data:01-01-2016:total "places"
>
"BRA, ARG"
HGET
data:01-01-2016:total
"
access
" > "23"
## To Remove / Delete 😦 HDEL )
HDEL
data:01-01-2016:total "places"
> (integer) 1
HGET
data:01-01-2016:total "places"
> (nil)
## Set All in the same command :
HSET
data:01-01-2016:total "places"
"BRA, ARG" "
access
" "23"
## To get All : (HGETALL)
HGETALL "
data:01-01-2016:total
"
> 1) "
places
" > 2) "
BRA, ARG
" > 3) "access" > 4) "23"
Using Redis to store a user’s session
Advantage of using this strategy are :
-> The information does not need to be shared between the servers themselves, but in the database, preventing replication of information on web servers;
-> Access to this information turns out to be quite fast;
Keys with expiration time
# To define : ( EXPIRE )
HMSET "session:user:1345" "name" "camila" "email" "cmacedo@email.com"
EXPIRE "
session:user:1345
" 2400
** Time in seconds.
# To get the time : (TTL)
TTL "
session:user:1345
" >
2400
Working with total / statistics
The INC and DEC commands are atomic alone, being equivalent to a “GET / SET / GET”, that is, they give the increase or decrease of some value then they will return the new result.
## To increase : INCR
## To decrease : DECR
Example :
To increase the value 1:
INCR "total_access" >
(integer) 1
INCR "total_access" >
(integer) 2
## To increase a specific value:INCRBY
## To decrease a specific value: DECRBY
Example :
INCRBY "total_access" 20
DECRBY "total_access" 20
## To increment and decrement decimal values: INCRBYFLOAT
INCRBYFLOAT "mykey" 25.60
> "88.6"
INCRBYFLOAT "mukey" -1.60
> "87.0"
SETBIT
The SET BIT command allows it to be shown a sequence of binary values, or bitmaps. A bitmap acts as an array which stores ones and zeros associated with an offset.
* Using bitmaps is able to perform logical operations with AND and OR.
SETBIT key offset value
#To get value : GETBIT
GETBIT access:01-01-2016 1
> (integer) 1
#To sum the total values: BITCOUNT
BITCOUNT
access:01-01-2016
> (integer) 20