# Pottery: Redis for Humans ððð
[Redis](http://redis.io/) is awesome, but [Redis
commands](http://redis.io/commands) are not always fun. Pottery is a Pythonic
way to access Redis. If you know how to use Python dicts, then you already
know how to use Pottery. Pottery is useful for accessing Redis more easily,
and also for implementing microservice resilience patterns; and it has been
battle tested in production at scale.
[![Build status](https://img.shields.io/github/workflow/status/brainix/pottery/Python%20package/master)](https://github.com/brainix/pottery/actions?query=branch%3Amaster)
![Dependencies up to date](https://img.shields.io/librariesio/github/brainix/pottery)
[![Latest released version](https://badge.fury.io/py/pottery.svg)](https://badge.fury.io/py/pottery)
![Supported Python versions](https://img.shields.io/pypi/pyversions/pottery)
![Number of lines of code](https://img.shields.io/tokei/lines/github/brainix/pottery)
[![Total number of downloads](https://pepy.tech/badge/pottery)](https://pepy.tech/project/pottery)
[![Downloads per month](https://pepy.tech/badge/pottery/month)](https://pepy.tech/project/pottery)
[![Downloads per week](https://pepy.tech/badge/pottery/week)](https://pepy.tech/project/pottery)
## Table of Contents
- [Dicts ð](#dicts)
- [Sets ðï¸](#sets)
- [Lists â](#lists)
- [Counters ð§®](#counters)
- [Deques ðï¸](#deques)
- [Redlock ð](#redlock)
- [synchronize() ð¯ââï¸](#synchronize)
- [NextId ð¢](#nextid)
- [redis_cache()](#redis_cache)
- [CachedOrderedDict](#cachedordereddict)
- [Bloom filters](#bloom-filters)
- [HyperLogLogs](#hyperloglogs)
- [ContextTimer â±ï¸](#contexttimer)
## Installation
```shell
$ pip3 install pottery
```
## Usage
First, set up your Redis client:
```python
>>> from redis import Redis
>>> redis = Redis.from_url('redis://localhost:6379/1')
>>>
```
## <a name="dicts"></a>Dicts ð
`RedisDict` is a Redis-backed container compatible with Python’s
[`dict`](https://docs.python.org/3/tutorial/datastructures.html#dictionaries).
Here is a small example using a `RedisDict`:
```python
>>> from pottery import RedisDict
>>> tel = RedisDict({'jack': 4098, 'sape': 4139}, redis=redis, key='tel')
>>> tel['guido'] = 4127
>>> tel
RedisDict{'jack': 4098, 'sape': 4139, 'guido': 4127}
>>> tel['jack']
4098
>>> del tel['sape']
>>> tel['irv'] = 4127
>>> tel
RedisDict{'jack': 4098, 'guido': 4127, 'irv': 4127}
>>> list(tel)
['jack', 'guido', 'irv']
>>> sorted(tel)
['guido', 'irv', 'jack']
>>> 'guido' in tel
True
>>> 'jack' not in tel
False
>>>
```
Notice the first two keyword arguments to `RedisDict()`: The first is your
Redis client. The second is the Redis key name for your dict. Other than
that, you can use your `RedisDict` the same way that you use any other Python
`dict`.
_Limitations:_
1. Keys and values must be JSON serializable.
## <a name="sets"></a>Sets ðï¸
`RedisSet` is a Redis-backed container compatible with Python’s
[`set`](https://docs.python.org/3/tutorial/datastructures.html#sets).
Here is a brief demonstration:
```python
>>> from pottery import RedisSet
>>> basket = RedisSet({'apple', 'orange', 'apple', 'pear', 'orange', 'banana'}, redis=redis, key='basket')
>>> sorted(basket)
['apple', 'banana', 'orange', 'pear']
>>> 'orange' in basket
True
>>> 'crabgrass' in basket
False
>>> a = RedisSet('abracadabra', redis=redis, key='magic')
>>> b = set('alacazam')
>>> sorted(a)
['a', 'b', 'c', 'd', 'r']
>>> sorted(a - b)
['b', 'd', 'r']
>>> sorted(a | b)
['a', 'b', 'c', 'd', 'l', 'm', 'r', 'z']
>>> sorted(a & b)
['a', 'c']
>>> sorted(a ^ b)
['b', 'd', 'l', 'm', 'r', 'z']
>>>
```
Notice the two keyword arguments to `RedisSet()`: The first is your Redis
client. The second is the Redis key name for your set. Other than that, you
can use your `RedisSet` the same way that you use any other Python `set`.
_Limitations:_
1. Elements must be JSON serializable.
## <a name="lists"></a>Lists â
`RedisList` is a Redis-backed container compatible with Python’s
[`list`](https://docs.python.org/3/tutorial/introduction.html#lists).
```python
>>> from pottery import RedisList
>>> squares = RedisList([1, 4, 9, 16, 25], redis=redis, key='squares')
>>> squares
RedisList[1, 4, 9, 16, 25]
>>> squares[0]
1
>>> squares[-1]
25
>>> squares[-3:]
[9, 16, 25]
>>> squares[:]
[1, 4, 9, 16, 25]
>>> squares + [36, 49, 64, 81, 100]
RedisList[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
>>>
```
Notice the two keyword arguments to `RedisList()`: The first is your Redis
client. The second is the Redis key name for your list. Other than that, you
can use your `RedisList` the same way that you use any other Python `list`.
_Limitations:_
1. Elements must be JSON serializable.
2. Under the hood, Python implements `list` using an array. Redis implements
list using a
[doubly linked list](https://redis.io/topics/data-types-intro#redis-lists).
As such, inserting elements at the head or tail of a `RedisList` is fast,
O(1). However, accessing `RedisList` elements by index is slow, O(n). So
in terms of performance and ideal use cases, `RedisList` is more similar to
Python’s `deque` than Python’s `list`. Instead of `RedisList`,
consider using [`RedisDeque`](#deques).
## <a name="counters"></a>Counters ð§®
`RedisCounter` is a Redis-backed container compatible with Python’s
[`collections.Counter`](https://docs.python.org/3/library/collections.html#collections.Counter).
```python
>>> from pottery import RedisCounter
>>> c = RedisCounter(redis=redis, key='my-counter')
>>> c = RedisCounter('gallahad', redis=redis, key='my-counter')
>>> c.clear()
>>> c = RedisCounter({'red': 4, 'blue': 2}, redis=redis, key='my-counter')
>>> c.clear()
>>> c = RedisCounter(redis=redis, key='my-counter', cats=4, dogs=8)
>>> c.clear()
>>> c = RedisCounter(['eggs', 'ham'], redis=redis, key='my-counter')
>>> c['bacon']
0
>>> c['sausage'] = 0
>>> del c['sausage']
>>> c.clear()
>>> c = RedisCounter(redis=redis, key='my-counter', a=4, b=2, c=0, d=-2)
>>> sorted(c.elements())
['a', 'a', 'a', 'a', 'b', 'b']
>>> c.clear()
>>> RedisCounter('abracadabra', redis=redis, key='my-counter').most_common(3)
[('a', 5), ('b', 2), ('r', 2)]
>>> c.clear()
>>> c = RedisCounter(redis=redis, key='my-counter', a=4, b=2, c=0, d=-2)
>>> from collections import Counter
>>> d = Counter(a=1, b=2, c=3, d=4)
>>> c.subtract(d)
>>> c
RedisCounter{'a': 3, 'b': 0, 'c': -3, 'd': -6}
>>>
```
Notice the first two keyword arguments to `RedisCounter()`: The first is your
Redis client. The second is the Redis key name for your counter. Other than
that, you can use your `RedisCounter` the same way that you use any other
Python `Counter`.
_Limitations:_
1. Keys must be JSON serializable.
## <a name="deques"></a>Deques ðï¸
`RedisDeque` is a Redis-backed container compatible with Python’s
[`collections.deque`](https://docs.python.org/3/library/collections.html#collections.deque).
Example:
```python
>>> from pottery import RedisDeque
>>> d = RedisDeque('ghi', redis=redis, key='letters')
>>> for elem in d:
... print(elem.upper())
G
H
I
>>> d.append('j')
>>> d.appendleft('f')
>>> d
RedisDeque(['f', 'g', 'h', 'i', 'j'])
>>> d.pop()
'j'
>>> d.popleft()
'f'
>>> list(d)
['g', 'h', 'i']
>>> d[0]
'g'
>>> d[-1]
'i'
>>> list(reversed(d))
['i', 'h', 'g']
>>> 'h' in d
True
>>> d.extend('jkl')
>>> d
RedisDeque(['g', 'h', 'i', 'j', 'k', 'l'])
>>> d.rotate(1)
>>> d
RedisDeque(['l', 'g', 'h', 'i', 'j', 'k'])
>>> d.rotate(-1)
>>> d
RedisDeque(['g', 'h', 'i', 'j', 'k', 'l'])
>>> RedisDeque(reversed(d), redis=redis)
RedisDeque(['l', 'k', 'j', 'i', 'h', 'g'])
>>> d.clear()
>>> d.extendleft('abc')
>>> d
RedisDeque(['c', 'b', 'a'])
>>>
```
Notice the two keyword arguments to `RedisDeque()`: The first is your Redis
client. The second is the Redis key name for your deque. Other than that, you
can use your `RedisDeque` the same way that you use any other Python `dequ
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
共28个文件
py:18个
txt:4个
pkg-info:2个
资源分类:Python库 所属语言:Python 资源全名:pottery-1.4.1.tar.gz 资源来源:官方 安装方法:https://lanzao.blog.csdn.net/article/details/101784059
资源推荐
资源详情
资源评论
收起资源包目录
pottery-1.4.1.tar.gz (28个子文件)
pottery-1.4.1
PKG-INFO 28KB
pottery.egg-info
PKG-INFO 28KB
requires.txt 36B
SOURCES.txt 504B
top_level.txt 8B
dependency_links.txt 1B
LICENSE 11KB
setup.cfg 38B
setup.py 3KB
README.md 27KB
pottery
redlock.py 26KB
monkey.py 4KB
py.typed 0B
executor.py 4KB
hyper.py 5KB
deque.py 7KB
list.py 12KB
timer.py 5KB
__init__.py 4KB
bloom.py 12KB
nextid.py 10KB
dict.py 5KB
annotations.py 2KB
base.py 11KB
cache.py 13KB
exceptions.py 4KB
set.py 7KB
counter.py 10KB
共 28 条
- 1
资源评论
挣扎的蓝藻
- 粉丝: 13w+
- 资源: 15万+
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功