LoginSignup
1
1

More than 5 years have passed since last update.

redis-py を安全に使うために gxredis を書いた

Last updated at Posted at 2016-12-21

概要

python には redis-py という便利な redis ライブラリがあるのですが、安全に使うのが難しかったので、シンプルで直感的なラッパーライブラリを書きました。

インストール方法

$ pip install gxredis

設計思想

  • 無理に Active Record 仕様にしないで、素直に redis の特性を活かす
  • 誤った操作が発生しにくい構成にする
  • redis-py で提供されているメソッドをそのまま活かす

使い方

DAO の定義

DAO には key の書式と種類を指定します。

import redis
from gxredis import *

class ItemDao(RedisDao):
       item = RedisString("device:{device_id}:item:{item_id}")
       item_list = RedisList("device:{device_id}:list")
       item_set = RedisSet("device:{device_id}:set")
       item_hash = RedisHash("device:{device_id}:hash")
       item_zset = RedisSortedSet("device:{device_id}:zset")

DAO の初期化

redis-py の StrictRedis を第1引数に渡してください。第2引数には、key を構成するためのパラメータを渡します。足りないパラメータは後で補完できるので、 DAO の生成時点で確定しているパラメータだけを渡せばよいです。

client = redis.StrictRedis("localhost", 6379, 15)
dao = ItemDao(client, key_params={"device_id": "GX123"})

DAO の属性

DAO の属性は redis にアクセスするためのアクセサです。

>>> dao.item
RedisString(key="device:{device_id}:item:{item_id}", key_params={'device_id': 'GX123'})

>>> dao.item_list
RedisList(key="device:{device_id}:list", key_params={'device_id': 'GX123'})

基本的な使い方

アクセサに対して、型に見合った操作が実行できます。key はアクセサに対応したものが使われるので、redis コマンドの第2引数以降を指定して下さい。

>>> dao.item_list.lpush("a")
>>> dao.item_list.lpush("b")
>>> dao.item_list.lpush("c")
>>> dao.item_list.lrange(0, 3)
['c', 'b', 'a']

key に対するパラメータが十分に提供されていない場合は、例外が発生します。

>>> dao.item.get()
...
AttributeError: Not enough keys are provided for redis operation

パラメータを補完する

アクセサに追加のパラメータを渡して実行することで、パラメータの補完された新しいアクセサが得られます。

>>> dao.item(item_id=1)
RedisString(key="device:{device_id}:item:{item_id}", key_params={'item_id': 1, 'device_id': 'GX123'})

新規に生成されたアクセサに対して redis コマンドが発行できます。

>>> accr = dao.item(item_id=1)
>>> accr.set("abc")
>>> accr.get()
'abc'

pipeline を利用する

pipeline も利用できます。

>>> pipe = dao.pipeline()
>>> accr1 = pipe.item(item_id=1)     # accessor for item01
>>> accr2 = pipe.item(item_id=2)     # accessor for item02
>>> accr1.set("item01")
>>> accr2.set("item02")
>>> pipe.item_list.rpush(accr1.key)
>>> pipe.item_list.rpush(accr2.key)
>>> pipe.execute()
>>> dao.item_list.lrange(0, 100)
['device:GX123:item:1', 'device:GX123:item:2',]

JSON を利用する

JSON で入出力するために、一部、便利関数を用意しています。

>>> dao.item(item_id=1).set_json({'hello': 'world'})
>>> dao.item(item_id=1).get_json()
{u'hello': u'world'}

SET や LIST に格納された key で MGET する

こちらも便利メソッドを用意しています。

>>> dao.item_list.lrange_mget(0, 100)
({'device:GX123:item:1', 'device:GX123:item:2'}, ['{"hello": "world"}', 'item02'])
>>> dao.item_set.smembers_mget_json(0, 0)
(['device:GX123:item:1'], [{u'hello': u'world'}])

smembers_mget, smembers_mget_json も使えます。

まとめ

redis-py を安全に使うための、軽めのラッパーライブラリを実装しました。コード行数は短めなので、良かったら読んでみて下さい。

今後、key の validation 機能などを追加する予定です。

あ、その前に pypi に登録しなきゃですね。

PyPi 登録 Done !

1
1
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
1