Getting started with Redis

in #redis6 years ago

I had used Redis in the past for my personal portfolio which is encapsulated by the ORM layer provided by Ruby on Rails, therefore I decided to spend a bit of time to learn on the CLI command. For me personally, Redis is just like MongoDB as there is a key value store pair, but the advantage of using Redis over MongoDB is the Read and Write performance.

1200px-Redis_Logo.svg.png

Video on learning Redis

Traversy Media provided a really great video tutorial on how to get started with Redis! This is just my notes on learning Redis.

Installation

Redis support cross-platform, check https://redis.io/download for more details.

For mac user, simply just run brew install redis. For linux user, apt-get install redis. For windows, you need to download the .msi file on the official github repo.

Start redis server

Start the server with just the command redis-server

Screen Shot 2018-07-20 at 2.00.46 PM.png

Start CLI

To start the CLI, simply just run redis-cli, where in this Command Line Interface (CLI), the command (the key value being set are case-sensitive) are not case sensitive as shown in the image below.

Screen Shot 2018-07-20 at 2.08.54 PM.png

Useful command

  • clear
  • flushall - clear the database

Redis Setting up data and getting data.

In redis, to set data, we use SET <variable_name> <value>, and you can get the data with GET <variable_name> simple as that.

Screen Shot 2018-07-20 at 2.11.09 PM.png

Set data to be expired

One cool feature of Redis database is that it can set a data to be expired in a certain amount of time.

Screen Shot 2018-07-20 at 2.23.23 PM.png
Set a key to be expire

Screen Shot 2018-07-20 at 2.25.35 PM.png
Initialize a key to expire in some time

TTL is a command to check how long the time left for the value to expire.

PERSIST

By using PERSIST command, you can make a value that is set to be expired to be persist data.

Screen Shot 2018-07-20 at 2.28.17 PM.png

MSET

Set multiple key-value pairs

Screen Shot 2018-07-20 at 2.25.35 PM.png

APPEND

Append operation will append the new value after the old value.

Screen Shot 2018-07-20 at 2.32.04 PM.png

RENAME

change name of a key

RENAME test test1

LIST

Screen Shot 2018-07-20 at 2.39.29 PM.png
Push from the LHS

Screen Shot 2018-07-20 at 2.42.02 PM.png
Push from the RHS

POP

POP operation can be chosen from LHS or RHS with LPOP or RPOP, where it will remove the leftmost or the rightmost value in the list.

e.g.

> LPOP people
lai
> RPOP people
test

EXISTS

exist command works in both string and list.

> SET a 1
> LPUSH b 2
> EXISTS a
> EXISTS b

Conclusion

This is enough for me to work on Redis server already, the command seems to be much more simpler and cleaner than MongoDB.

superoo7.png

Sort:  

你好!才子,才女,你参加月旦评了吗?有才的人都去参加了呢。倘若你想让我隐形,请回复“取消”。

You’ve been upvoted by TeamMalaysia community. Do checkout other posts made by other TeamMalaysia authors at http://steemit.com/created/teammalaysia

To support the growth of TeamMalaysia Follow our upvotes by using steemauto.com and follow trail of @myach

Vote TeamMalaysia witness bitrocker2020 using this link vote for witness

Coin Marketplace

STEEM 0.20
TRX 0.15
JST 0.030
BTC 65185.94
ETH 2630.94
USDT 1.00
SBD 2.83