# Redisworks 0.4.0
![Python Versions](https://img.shields.io/pypi/pyversions/redisworks.svg?style=flat)
![License](https://img.shields.io/pypi/l/redisworks.svg?version=latest)
[![Build Status](https://travis-ci.org/seperman/redisworks.svg?branch=master)](https://travis-ci.org/seperman/redisworks)
[![Coverage Status](https://coveralls.io/repos/github/seperman/redisworks/badge.svg?branch=master)](https://coveralls.io/github/seperman/redisworks?branch=master)
**The Pythonic Redis Client**
Why Redisworks?
- Lazy Redis Queries
- Dynamic Typing
- Ease of use
Have you ever used PyRedis and wondered why you have to think about types all the time? That you have to constantly convert objects to strings and back and forth since Redis keeps most things as strings?
Redis works provides a Pythonic interface to Redis. Let Redisworks take care of type conversions for you.
Behind the scene, Redisworks uses [DotObject](https://github.com/seperman/dotobject) to provide beautiful dot notation objects and lazy Redis queries.
# Install
`pip install redisworks`
Note that RedisWorks needs Redis server 2.4+.
# Setup
let's say if you want all the keys in Redis to start with the word `root`.
Then you:
```py
root = Root() # connects to Redis on local host by default
```
Or if you want to be more specific:
```py
root = Root(host='localhost', port=6379, db=0)
```
## password
Any other parameter that you pass to Root will be passed down to PyRedis. For example:
```py
root = Root(host='localhost', port=6379, db=0, password='mypass')
```
# Saving to Redis
Saving to Redis is as simple as assigning objects to attributes of root or attributes of attributes of root (you can go as deep as you want.)
Make sure you are not using any Python's reserved words in the key's name.
Example:
```py
>>> from redisworks import Root
>>> import datetime
>>> root = Root()
>>> root.my.list = [1, 3, 4]
>>> root.my.other.list = [1, [2, 2]]
>>>
>>> some_date = datetime.datetime(2016, 8, 22, 10, 3, 19)
>>> root.time = some_date
>>>
>>> root.the.mapping.example = {1:1, "a": {"b": 10}}
```
Redis works will automatically convert your object to the proper Redis type and immediately write it to Redis as soon as you assign an element!
The respective keys for the above items will be just like what you type: `root.my.list`, `root.time`, `root.the.mapping.example`:
If you use redis-cli, you will notice that the data is saved in the proper Redis data type:
```
127.0.0.1:6379> scan 0
1) "0"
2) 1) "root.the.mapping.example"
2) "root.time"
3) "root.my.list"
127.0.0.1:6379> type root.the.mapping.example
hash
127.0.0.1:6379> type root.time
string
127.0.0.1:6379> type root.my.list
list
```
# Reading from Redis
Reading the data is as simple as if it was just saved in Python memory!
Redis works returns Lazy queries just like how Django returns lazy queries. In fact the lazy objects code is borrowed from Django!
If you ran the example from [Saving to Redis](#saving-to-redis), run a flush `root.flush()` to empty Redisworks Cache. This is so it goes and gets the objects from Redis instead of reading its own current copy of data:
```py
>>> from redisworks import Root
>>> root = Root()
>>> thetime = root.time
>>> thelist = root.my.list
>>> mydict = root.the.mapping.example
>>> mydict # is not evalurated yet!
<Lazy object: root.the.mapping.example>
>>> print(mydict)
{1:1, "a": {"b": 10}} # Now all the 3 objects are read from Redis!
>>> mydict
{1:1, "a": {"b": 10}}
>>> root.my.list
[1, 3, 4]
>>> root.my.other.list
[1, [2, 2]]
>>> root.time
2016-08-22 10:03:19
```
# Changing root key name
Every key name by default starts with the word `root`.
If you want to use another name, you have two options:
Option 1, pass a namespace:
```py
>>> mynamespace = Root(conn=redis_conn, namespace='mynamespace')
>>> mynamespace.foo = 'bar'
```
Option 2, simply subclass `Root`:
```py
>>> from redisworks import Root
>>> class Post(Root):
... pass
>>> post=Post()
>>> post.item1 = "something" # saves to Redis
...
>>> print(post.item1) # loads from Redis
something
```
# Numbers as attribute names
Let's say you want `root.1` as a key name.
Python does not allow attribute names start with numbers.
All you need to do is start the number with the character `i` so Redisworks takes care of it for you:
```py
>>> root.i1 = 10
>>> print(root.i1)
10
```
The actual key in Redis will be `root.1`
# Dynamic key names
```py
>>> path1 = 'blah'
>>> path2 = 'blah.here`'
>>> root[path1] = 'foo'
>>> root[path2] = 'foo bar'
>>> root.blah
foo
>>> root.blah.here
foo bar
```
# Passing TTL to the keys
You can use the `with_ttl` helper.
```py
>>> from redisworks import Root, with_ttl
>>> self.root.myset = with_ttl([1, 2, 3], ttl=1)
>>> self.root.flush()
>>> self.root.myset
[1, 2, 3]
>>> time.sleep(1.2)
>>> self.root.flush()
>>> self.root.myset
```
# Other examples
Take a look at [example.py](example.py)
# Primary Author
Seperman (Sep Dehpour)
- [Github](https://github.com/seperman)
- [Linkedin](http://www.linkedin.com/in/sepehr)
- [ZepWorks](https://zepworks.com)
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
Redisworks 0.4.0 Pythonic Redis 客户端为什么选择 Redisworks?惰性 Redis 查询动态类型易于使用您是否曾经使用过 PyRedis,并想知道为什么您必须一直考虑类型?您是否必须不断地将对象转换为字符串并反复转换,因为 Redis 将大多数内容保存为字符串?Redis works 为 Redis 提供了 Pythonic 接口。让 Redisworks 为您处理类型转换。在后台,Redisworks 使用DotObject提供漂亮的点符号对象和惰性 Redis 查询。安装pip install redisworks请注意,RedisWorks 需要 Redis 服务器 2.4+。设置假设您希望 Redis 中的所有键都以单词 开头root。那么你root = Root() # connects to Redis on local host by default或者如果你想要更具体一点root = Root(host='localhost', port=6379, db=0)
资源推荐
资源详情
资源评论
收起资源包目录
Pythonic Redis 客户端.zip (24个子文件)
redisworks
__init__.py 187B
helper.py 182B
redisworks.py 8KB
.travis.yml 220B
setup.py 2KB
.coveragerc 81B
.github
workflows
main.yaml 1KB
FUNDING.yml 35B
标签.txt 45B
pytest.ini 58B
AUTHORS 31B
LICENSE 3KB
tests
__init__.py 0B
test_redisworks.py 8KB
README.txt 6KB
资源内容.txt 873B
example.py 628B
requirements-dev.txt 103B
readthedocs-requirements.txt 13B
requirements.txt 30B
.gitignore 795B
setup.cfg 328B
README.md 5KB
conftest.py 106B
共 24 条
- 1
资源评论
徐浪老师
- 粉丝: 8317
- 资源: 1万+
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功