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
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 "
# To delete :
# To insert multiples key values
SET <key> <value> <key> <value> … <key> <value>
# To list keys
Filters to Search
# To list keys by filter *
Example : KEY “total_*” –> It will return all keys that started with total_
# To list keys by filter ?
Example : KEY “t?tal_*” –> It will return all keys that started with t<1 n value>tal_
# To list keys by filter [ ]
Example : KEY<string_key_name_position>–> 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 )
" > "23"
## To Remove / Delete 😦 HDEL )
> (integer) 1
## Set All in the same command :
"BRA, ARG" "
## To get All : (HGETALL)
> 1) "
" > 2) "
" > 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 : (
HMSET "session:user:1345" "name" "camila" "email" "firstname.lastname@example.org"
** Time in seconds.
# To get the time : (TTL)
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
To increase the value 1:
INCR "total_access" >
INCR "total_access" >
## To increase a specific value:INCRBY
## To decrease a specific value: DECRBY
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"
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
> (integer) 20