Redis
What is Redis?
Redis is key-value store and we call it kind of NoSQL.
It is simple to use.
To share session within servers, or to save easy key-value data in server.
Install
Mac
brew install redis
After running this command, some instruction is shown.
To have launchd start redis at login: ln -sfv /usr/local/opt/redis/*.plist ~/Library/LaunchAgents Then to load redis now: launchctl load ~/Library/LaunchAgents/homebrew.mxcl.redis.plist Or, if you don't want/need launchctl, you can just run: redis-server /usr/local/etc/redis.conf
To start redis in local
redis-server /usr/local/etc/redis.conf
To stop redis, Ctrl+C or
redis-cli shutdown
Access redis from redis-cli
redis-cli
Ubuntu
Install
sudo apt-get install redis-server
Start
sudo /etc/init.d/redis-server start
Configuration file is /etc/redis/redis.conf
Configuration and User Auth
Access limitation
This limitation is by redis.conf
Please look this file
bind 127.0.0.1
By default, only local machine can access.
If comment out this line, we can access this from anywhere
If you want to add additional server acesss
bind 127.0.0.1, 192.168.100.104
Connect and operation
Start
redis-server redis.conf
redis.conf is configuration file
default port is 6379.
Access
To access redis-server, you redis-cli command
redis-cli
Access specific host
redis-cli -h 192.168.122.104
Check Redis sever is alive
redis-cli ping
If alive, PONG will back
Test
After
set key val get key
Set value and get value
Set
SET key1 "Hello"
Del
del key
Return (nil)
Delete all keys
flushdb
All keys
redis-cli keys '*'
Redis expires
Add expiration
set key pekepeke expire key 5 get key ttl key get key
expire time is 5s
ttl is check rest time
Data type
Redis has several types of data
- String
- List
- Sets
- Sorted Sets
- Hashes
- Bit array
- HyperLogLogs
List
rpush mylist A lpush mylist first lrange mylist 0 -1 rpop mylist lpop mylist
rpush : Push data from the last
lpush : Push data from the beginning
lrange : To show data
Hash
hmset user:1000 username daiji nickname dj point 100 hget user:1000 username hgetall user:1000
Ruby
Use from ruby, redis gem was prepared
gem install redis
In rails, resque, sidekiq etc… background task library uses redis
Ref
MacにRedisをインストールする
https://codeforgeek.com/2015/07/using-redis-to-handle-session-in-node-js/
Command reference
Redisのインストールと基本設定