Redisのおぼえがき

Tiny memo for my personal use of Redis. Be careful it's optimized only for me so the format probably doesn't make sense to you.

About persistence

Redis provides 2 types of persistence setting: RDB and AOF.

  • RDB:
    • Redis Database File. Stored and compressed once in a while; it can loose some data after persistence and before unexpected shutdown.
  • AOF:
    • Append Only File. A file includes all info of appending operation. A file size and startup time will be big; it almost never loose data.
    • Set appendonly to yes in redis.conf

Default Setting

https://raw.githubusercontent.com/antirez/redis/4.0/redis.conf

Use in Docker

  • Port
    • 6379
  • Data file will be:
    • /data in a container
  • Config file
    • We have to specify when to boot
docker run -it --name redis \
    -p 6379:6379 \
    -v "$PWD"/data:/data \
    -v "$PWD"/redis.conf:/usr/local/etc/redis/redis.conf \
    redis redis-server /usr/local/etc/redis/redis.conf

From https://hub.docker.com/_/redis/

Commands

Command reference – Redis

String

SET, GET

List

  • LPUSH ... Insert value from left side
  • RPUSH .. Insert from right side
  • LPOP
  • RPOP
  • LLEN ... Get length
  • LRANGE key_of_list from_index to_index # Specify -1 as to_index to get to the end

Set

  • SADD
  • SCARD ... Get length
  • SMEMBERS key ... Get all

Sorted Set

  • ZADD key_of_sorted_set score value ... The score will be a key of being sorted
  • ZRANK key value ... Returns an index (ranking order)

Hash

  • HSET user:pig name "pig"
  • HSET user:pig email "pig@gmail.com" ... Now the "user:pig" contains a hash
  • HGET user:pig name
  • HGETALL user:pig