Redis is an in-memory key-value store that can be used as a cache or database. It supports varied data structures like Strings, Lists, Sets, Hashes and Sorted Sets. While it stores data in memory for high speed, it can also optionally write data to disk for persistence. Redis is useful for caching, real-time analytics or any use case requiring fast access to structured data.
4. What is "redis"?What is "redis"?
remote dictionary server
Key-Value Store (KVS)
Varied Data Structure
in-memory
having persistence
easy and fast
compatible w/Python, Ruby, …
→web engineers should learn redis!
6. Comparison with RDBMSsComparison with RDBMSs
Function RDBMS Redis
using simply △ ◎
high speed processing △ ◎
horizontal distribution × ◎
high availability △ ◎
persistency ◎ ○
complex query ◎ ×
transaction ◎ △
consistency ◎ △
7. Comparison with memcachedComparison with memcached
memcached redis
good at Cache Cache
data structure only string varied structure
persistency × ○
Disk I/O NOT DO can be disable
speed high high
multi thread ○ ×
memory efficiency △ ○
10. StringString
redis python
> SET hoge fugafuga
OK
> GET hoge
"fugafuga"
> SET point 10
OK
> GET point
"10"
> INCR point
(integer) 11
> GET point
"11"
from redis import Redis
redis = Redis()
redis.set('hoge', 'fugafuga')
print(redis.get('hoge'))
#=> b'fugafuga'
redis.set('point', 10)
print(redis.get('point'))
#=> b'10'
redis.incr('point')
print(redis.get('point'))
#=> b'11'
20. data having expirationdata having expiration
can set expiration to key
EXPIRE key seconds
`key` is expired after `seconds` seconds
EXPIREAT key timestamp
`key` is expired on `timestamp`
23. Real Time RankingReal Time Ranking
sorted set
zadd key score member
keyにscore点を持ったmemberを追加する add
a `member` that has `score` to `key`
zincrby key increment member
increment score of `member` of `key`
zrange key start stop
get `key`s members from `start` to `stop`
24. Sample CodeSample Code
from redis import Redis
redis = Redis()
while True:
print('input member:score> ', end='')
ipt = input()
if ipt == 'show': # command 'show'
ranking = redis.zrange('ranking', 0, 5, withscores=True)[::-1]
for i, m in enumerate(ranking):
values = {
'rank': i+1,
'member': m[0].decode(),
'point': m[1]
}
print('{rank}: {member} ({point}pt)'.format(**values))
continue
member, score = args.split(':')
redis.zadd('ranking', member, int(score))
print('good bye')
https://github.com/nasa9084/samples.git
28. in conclusionin conclusion
in-memory KVS
having persistency
very varied data structure
String, List, Set, Hash, SortedSet
you can try to use redis with `try redis`