Skip to content

Instantly share code, notes, and snippets.

@Integralist
Last active April 2, 2024 11:14
Show Gist options
  • Save Integralist/4a7b9efe971c416ee56b4e0025174e9c to your computer and use it in GitHub Desktop.
Save Integralist/4a7b9efe971c416ee56b4e0025174e9c to your computer and use it in GitHub Desktop.
[Redis Install and Examples] #redis #install #macos
brew install redis
brew services start redis
$ redis-server --port 7777 # OVERRIDING DEFAULT PORT 6379
60717:C 05 Mar 2020 12:54:01.873 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
60717:C 05 Mar 2020 12:54:01.873 # Redis version=5.0.7, bits=64, commit=00000000, modified=0, pid=60717, just started
60717:C 05 Mar 2020 12:54:01.873 # Configuration loaded
60717:M 05 Mar 2020 12:54:01.874 * Increased maximum number of open files to 10032 (it was originally set to 256).
_._
_.-``__ ''-._
_.-`` `. `_. ''-._ Redis 5.0.7 (00000000/0) 64 bit
.-`` .-```. ```\/ _.,_ ''-._
( ' , .-` | `, ) Running in standalone mode
|`-._`-...-` __...-.``-._|'` _.-'| Port: 7777
| `-._ `._ / _.-' | PID: 60717
`-._ `-._ `-./ _.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' | http://redis.io
`-._ `-._`-.__.-'_.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' |
`-._ `-._`-.__.-'_.-' _.-'
`-._ `-.__.-' _.-'
`-._ _.-'
`-.__.-'
60717:M 05 Mar 2020 12:54:01.875 # Server initialized
60717:M 05 Mar 2020 12:54:01.875 * Ready to accept connections
# https://redis.io/commands
$ redis-cli -p 7777
# NOTE: If you need to connect to a remote Redis instance (e.g. via Kubernetes pod)
# redis-cli -h example-redis-us-east-2.aws.whatever.net -p 6379 --pass whatever_password PING
# If your application code needs an auth token (e.g. send `AUTH some_password`)
# https://redis.io/commands/auth/
# Then you can set that up locally using:
config set requirepass SECRET_SQUIRREL
# Also for the above you could do it this way using a Makefile:
#
#.PHONY: check-local-redis
#check-local-redis:
# @# If redis isn't running locally, then we start it, find the config and make sure a password is set.
# @# We then need to restart redis for the config changes to take effect (just in case the requirepass wasn't already set)
# @if ! lsof -i ":6379" | grep -qi "redis-ser"; then \
# brew services start redis; \
# redis-cli INFO | grep config_file | cut -d : -f 2 | tr -d '\r' | xargs -I % sed -i '' 's/# requirepass foobared/requirepass SECRET_SQUIRREL/' %; \
# brew services restart redis; \
# fi
# health check
127.0.0.1:7777> ping
PONG
# list all keys
127.0.0.1:7777> keys *
(empty list or set)
# scan iteratively (using a cursor)
127.0.0.1:7777> scan 0 MATCH foo*
1) "26"
2) 1) "foo008"
2) "foo005"
3) "foo4"
4) "foo001"
5) "foo006"
6) "foo003"
7) "foo7"
8) "foo5"
127.0.0.1:7777> scan 26 MATCH foo*
1) "19"
2) 1) "foo9"
2) "foo3"
3) "foo bar"
4) "foo8"
5) "foo1"
6) "foo009"
7) "foo004"
8) "foo2"
9) "foo6"
127.0.0.1:7777> scan 19 MATCH foo*
1) "0"
2) 1) "foo002"
2) "foo007"
# set a simple key with 30s TTL
# https://redis.io/commands/set
127.0.0.1:7777> set foo bar EX 30
OK
127.0.0.1:7777> get foo
"bar"
# ...wait for 30s...
127.0.0.1:7777> get foo
(nil)
# show the number of keys currently in redis (better than `keys *`)
127.0.0.1:7777> info keyspace
# Keyspace
db0:keys=5,expires=0,avg_ttl=0
# increment a simple counter
127.0.0.1:7777> set hits 0
OK
127.0.0.1:7777> get hits
"0"
127.0.0.1:7777> incr hits
(integer) 1
127.0.0.1:7777> incr hits
(integer) 2
127.0.0.1:7777> incrby hits 2
(integer) 4
# NOTE:
# in Redis 4+ hmset is deprecated for hset (which is now variadic)
#
# hget = return single field value
# hmget = return multiple field values
# set hashmap (key, field, field-value)
127.0.0.1:7777> hmset client1 counter 1
OK
127.0.0.1:7777> hmget client1 counter
1) "1"
127.0.0.1:7777> hmset client1 foo bar
OK
127.0.0.1:7777> hmget client1 foo
1) "bar"
# when creating a key and assigning a string you can set a ttl
# that will automatically delete the key when the ttl expires
# you can't set a ttl in the same set command when using a hashmap
# so to expire a key without a ttl already set you need to use
# the EXPIRE command (which automatically deletes the key)
127.0.0.1:7777> EXPIRE client2 10
(integer) 1
127.0.0.1:7777> TTL client2
(integer) 5
127.0.0.1:7777> TTL client2
(integer) 4
127.0.0.1:7777> TTL client2
(integer) 3
127.0.0.1:7777> TTL client2
(integer) 2
127.0.0.1:7777> TTL client2
(integer) 1
127.0.0.1:7777> TTL client2
(integer) 0
127.0.0.1:7777> TTL client2
(integer) -2
127.0.0.1:7777> type client2
(nil)
# get all hashmap fields at once
127.0.0.1:7777> hgetall client1
1) "counter"
2) "2"
3) "foo"
4) "bar"
5) "restrict"
6) "false"
127.0.0.1:7777> hgetall client2
(empty list or set)
# delete a specific field from a hashmap
# 1=OK, 0=FAIL
127.0.0.1:7777> hdel client1 foo
(integer) 1
127.0.0.1:7777> hgetall client1
1) "counter"
2) "2"
3) "restrict"
4) "false"
# increment hashkey field
127.0.0.1:7777> hincrby client1 counter 2
(integer) 4
127.0.0.1:7777> hgetall client1
1) "counter"
2) "4"
3) "restrict"
4) "false"
127.0.0.1:7777>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment