Python has a handy redis library called redis-py, but it was difficult to use safely, so it's simple and intuitive. I wrote a wrapper library.
Installation method
$ pip install gxredis
――Do not force Active Record specifications, but take advantage of the characteristics of redis obediently --Make the configuration less likely to cause erroneous operations --Use the methods provided by redis-py as they are
For DAO, specify the format and type of 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")
Pass the redis-py StrictRedis
as the first argument. In the second argument, pass the parameters to configure the key. You only need to pass the parameters that are fixed at the time of DAO generation, as you can fill in the missing parameters later.
client = redis.StrictRedis("localhost", 6379, 15)
dao = ItemDao(client, key_params={"device_id": "GX123"})
The DAO attribute is an accessor to access 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'})
You can perform operations on the accessor that match the type. Since the key corresponding to the accessor is used, specify the second and subsequent arguments of the redis command.
>>> dao.item_list.lpush("a")
>>> dao.item_list.lpush("b")
>>> dao.item_list.lpush("c")
>>> dao.item_list.lrange(0, 3)
['c', 'b', 'a']
If you don't provide enough parameters for the key, you'll get an exception.
>>> dao.item.get()
...
AttributeError: Not enough keys are provided for redis operation
By passing additional parameters to the accessor and executing it, you get a new accessor with complemented parameters.
>>> dao.item(item_id=1)
RedisString(key="device:{device_id}:item:{item_id}", key_params={'item_id': 1, 'device_id': 'GX123'})
You can issue the redis command to the newly generated accessor.
>>> accr = dao.item(item_id=1)
>>> accr.set("abc")
>>> accr.get()
'abc'
A pipeline is also available.
>>> 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',]
Some convenient functions are provided for input / output in JSON.
>>> dao.item(item_id=1).set_json({'hello': 'world'})
>>> dao.item(item_id=1).get_json()
{u'hello': u'world'}
This also has a convenient method.
>>> 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'}])
You can also use smembers_mget
, members_mget_json
.
We have implemented a light wrapper library for safe use of redis-py. The number of lines of code is short, so please read it if you like.
We plan to add key validation functions in the future.
~~ Oh, I have to register with pypi before that. ~~
PyPi Registration Done!
Recommended Posts