1
0

More than 1 year has passed since last update.

[python] redisでよく使うコマンド

Last updated at Posted at 2022-04-10

redis-py を使うよ!

インストール

pip install redis

使い方

前提条件

import redis
connection_pool = redis.ConnectionPool(
    host=host,
    port=port,
    db=db,
    password=password
)
r = redis.StrictRedis(connection_pool=connection_pool)
key = 'key'
value_string = 'value'
value_hash = {'hoge': 'A', 'foo': 'B'}
ttl = 60

コマンド説明

type コマンド 説明
共通 r.type(key) keyのタイプを確認
共通 r.expire(key, ttl) 生存期間を秒で設定
共通 r.keys(key) keyの有無確認
共通 r.dbsize() レコード数
共通 r.delete(key) keyの削除
string r.set(key, value_string) key, value_string を生存期間なしで登録
string r.set(key, value_string, ex=ttl) key, value を生存期間を指定して登録
string r.get(key) keyの値を取得
hash r.hset(key, mapping=value_hash) key, value を生存期間なしで登録
hash r.hgetall(key) keyの値を取得
hash r.hlen(key) keyの辞書内のリスト長を取得
hash r.hget(key, 'hoge') keyの辞書内'hoge'の値を取得
hash r.hexists(key, 'hoge') keyの辞書内'hoge'の有無確認
hash r.hdel(key, 'hoge') keyの辞書内'hoge'の値を削除
hash r.hkeys(key) keyの辞書内のkey一覧を取得
hash r.hvals(key) keyの辞書内のvalue一覧を取得
list r.lpush(key, value_string) keyのリストの最初にvalue_stringを追加
list r.rpush(key, value_string) keyのリストの最後にvalue_stringを追加
list r.lpop(key) keyのリストの最初の値を取得
list r.rpop(key) keyのリストの最後の値を取得
list r.lrange(key, 0, -1) keyの全リスト出力
list r.llen(key) keyのリスト長

TIPS

リストを一度に登録できないので、一度づつ登録する必要があるが、登録するときは pipeline を使うと使わないより 78倍速いらしい。(SQLのトランザクションもそんな感じだったなぁ)

value_list = [1, 2, 3]
pipe = r.pipeline()
for value in value_list:
    pipe.rpush(key, value)
pipe.execute()

ここに書いてあった

検証用

print('-'*80)
print('TYPE: string')
print('-'*80)
print('set', r.set(key, value_string))
print('ttl', r.ttl(key))
print('set', r.set(key, value_string, ex=ttl))
print('type', r.type(key))
print('get', r.get(key))
print('expire', r.expire(key, ttl))
print('ttl', r.ttl(key))
print('delete', r.delete(key))

print('-'*80)
print('TYPE: hash')
print('-'*80)
print('hset', r.hset(key, mapping=value_hash))
print('hset', r.hset(key, mapping=value_hash))
print('hset', r.hset(key, mapping={'bar': 'c'}))
print('type', r.type(key))
print('hget', r.hget(key, 'hoge'))
print('hgetall', r.hgetall(key))
print('hkeys', r.hkeys(key))
print('hvals', r.hvals(key))
print('ttl', r.ttl(key))
print('expire', r.expire(key, ttl))
print('ttl', r.ttl(key))
print('hlen', r.hlen(key))
print('hexists', r.hexists(key, 'hoge'))
print('hdel', r.hdel(key, 'hoge'))
print('delete', r.delete(key))

print('-'*80)
print('TYPE: list')
print('-'*80)
print('rpus', r.rpush(key, '1'))
print('rpus', r.rpush(key, '2'))
print('rpus', r.rpush(key, '3'))
print('lpus', r.lpush(key, 'A'))
print('lpus', r.lpush(key, 'B'))
print('lpus', r.lpush(key, 'C'))
print('lrange', r.lrange(key, 0, -1))
print('lpop', r.lpop(key))
print('rpop', r.rpop(key))
print('lrange', r.lrange(key, 0, -1))
print('llen', r.llen(key))
print('ttl', r.ttl(key))
print('expire', r.expire(key, ttl))
print('ttl', r.ttl(key))
print('delete', r.delete(key))

出力結果

--------------------------------------------------------------------------------
TYPE: string
--------------------------------------------------------------------------------
set True
ttl -1
set True
type b'string'
get b'value'
expire True
ttl 60
delete 1
--------------------------------------------------------------------------------
TYPE: hash
--------------------------------------------------------------------------------
hset 2
hset 0
hset 1
type b'hash'
hget b'A'
hgetall {b'hoge': b'A', b'foo': b'B', b'bar': b'c'}
hkeys [b'hoge', b'foo', b'bar']
hvals [b'A', b'B', b'c']
ttl -1
expire True
ttl 60
hlen 3
hexists True
hdel 1
delete 1
--------------------------------------------------------------------------------
TYPE: list
--------------------------------------------------------------------------------
rpus 1
rpus 2
rpus 3
lpus 4
lpus 5
lpus 6
lrange [b'C', b'B', b'A', b'1', b'2', b'3']
lpop b'C'
rpop b'3'
lrange [b'B', b'A', b'1', b'2']
llen 4
ttl -1
expire True
ttl 60
delete 1
1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0