15
11

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

redis-pyを使って、PythonからRedisにアクセスする

Last updated at Posted at 2019-06-07

TL;DR

  • PythonからRedisにアクセスするためのライブラリを知りたい
  • redis-pyというものを使うらしいので、ちょっと試してみた

redis-py

redis-py(GitHub)

Redis

Key Value Storeの有名どころですね。

Redis

値として取れるものも、単純なものだけではなくList、Set、Sorted set、Hash、HyperLogLogs、Streamなどを扱うことができます。

An introduction to Redis data types and abstractions

今回は、Redisの5.0.5を使用します。Dockerイメージで。

$ docker container run -it --rm --name redis redis:5.0.5

$ docker inspect redis | grep IPAddress
            "SecondaryIPAddresses": null,
            "IPAddress": "172.17.0.2",
                    "IPAddress": "172.17.0.2",

PythonからRedisへアクセスするためには、redis-pyというものがメジャーなようなので、こちらを使用して試してみることにします。

Clients / Python

環境およびredis-pyのインストール

利用するPythonのバージョン。

$ python3 -V
Python 3.6.7

redis-pyのインストールとバージョン。

$ pip3 install redis

$ pip3 freeze
...
redis==3.2.1

redis-pyを使って、PythonからRedisにアクセスする

とりあえず、Getting Startedを見ながら書いてみましょう。

Getting Started

あんまり使い方が書かれていないのですが、コードを見つつ、なんとなく書いてみます…。

hello-redis.py

import redis

redis_client = redis.Redis(host = '172.17.0.2', port = 6379)


## Simple
redis_client.set('key1', 'value1')

assert redis_client.get('key1') == b'value1'
assert redis_client.get('key1').decode('utf-8') == 'value1'


## Hash
redis_client.hmset('hash_key1', { 'member1': 'hash_value1', 'member2': 'hash_value2' })

assert redis_client.hget('hash_key1', 'member1') == b'hash_value1'
assert redis_client.hgetall('hash_key1') == { b'member1': b'hash_value1', b'member2': b'hash_value2' }

実行。

$ python3 hello-redis.py

動きました。

ところで、レスポンスがbyteで返ってきているので、ひたすらdecodeすることになります。

ここで、ドキュメントをちゃんと見ると、decode_responsesTrueにすればよいとあるのでこちらを使ってみましょう。

Getting Started

If all string responses from a client should be decoded, the user can specify decode_responses=True to Redis.init. In this case, any Redis command that returns a string type will be decoded with the encoding specified.

hello-redis-decoded.py

import redis

redis_client = redis.Redis(host = '172.17.0.2', port = 6379, decode_responses = True)


## Simple
redis_client.set('key1', 'value1')

assert redis_client.get('key1') == 'value1'


## Hash
redis_client.hmset('hash_key1', { 'member1': 'hash_value1', 'member2': 'hash_value2' })

assert redis_client.hget('hash_key1', 'member1') == 'hash_value1'
assert redis_client.hgetall('hash_key1') == { 'member1': 'hash_value1', 'member2': 'hash_value2' }

スッキリしましたね。

とりあえず、基本的な使い方はこんな感じでしょうか。

接続時のオプションなどは、ちゃんと見た方が良さそうです。

その他の機能

ドキュメントを見ると、コネクションプールなどの機能もあるようです。

More Detail

Hiredisとか、面白いですね

Parsers

RedisチームがCで実装したパーサーで、Hiredisをインストールするとredis-pyのパース処理が早くなるようです。

Hiredis

$ pip3 install hiredis

redis-py自身が、Hiredisを意識したソースコードになっています。

あと、ドキュメントには記載がなさそうですが、Redis Clusterも使えそうなので、そのうち試してみたいところ。

Redis Sentinelのサポートは書かれているんですけどね。

Redis Sentinel

今回はこれくらいで。

15
11
1

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
15
11

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?