Redis
Redis is a command-based in-memory no-sql database, has sufficient data structure and useful in caching things.
Is it similar to MongoDB?
- yes, Redis is no-sql and could be used for similar key/value cases
- difference:
- MongoDB is a "disk-based" document store while Redis is "memory-based"
- Redis is used for caching
Is it similar to Memcached?
- yes, for both, the data is stored in memory,
- difference:
- Memcached uses volatile cache, and data does not get persisted or is lost with a restart, while Redis uses built-in persistence, and data will not disappear with a restart.
More datatypes is a great feature for Redis: String / Hashes / lists / sets / sorted sets / bitmaps / ...
Redis is not for maximum security but for maximum performance and simplicity
setup
- refer to the Redis tutorial
Commands
some basic commands
- set foo 100
- get foo
- incr foo/ incrby
- decr foo => returns
- exists foo => returns 1 if it exists or 0 if it doesn't
- del foo => remove a value
- monitor => we can observe the process of redis
- flushall => clear everything in memory
- expire foo 120 // set foo hello // TTL foo => you can look at how many seconds it left for the value
- TTL foo => return -2 which means it has been expired
- after perform persist foo, TTL foo => return -1 which means that there is no time running on it.
more advanced redis commands
- mset => set multiple key/value
- msetnx => set multiple key/value when the key is not set
- append mykey "stringtoappend" => just append the string to the end of the original value
- getrange counter 0 -1 => returns the substring of counter from 0 to the end (-1 represent the end of the value)
- rename => rename a key, and will return error if the key doesn't exist
- getset => set the new value and returns the old value
- setex mykey 10 "hello" <=> set mykey "hello" expire mykey 10
- persist mykey => just get rid of the expire setting
refer to detailed data types