rename class RedisStore -> RedisDB (uniform <Backend>DB naming); v0.1.1
Signed-off-by: disqualifier <dev@disqualifier.me>
This commit is contained in:
parent
237e3ff765
commit
dc3ea2de13
14
README.md
14
README.md
@ -7,34 +7,34 @@ datastore trio (`redis` / `psql` / `mysql`), a sibling of the `mongo` lib.
|
|||||||
> **Import name ≠ repo name.** The repo/distribution is **`redis`**, but you import
|
> **Import name ≠ repo name.** The repo/distribution is **`redis`**, but you import
|
||||||
> **`redis_store`** — the driver package owns the `redis` import namespace, so the lib
|
> **`redis_store`** — the driver package owns the `redis` import namespace, so the lib
|
||||||
> can't also be `redis`. Install resolves `redis.git`; code does
|
> can't also be `redis`. Install resolves `redis.git`; code does
|
||||||
> `from redis_store import RedisStore`.
|
> `from redis_store import RedisDB`.
|
||||||
|
|
||||||
## Install
|
## Install
|
||||||
|
|
||||||
`requirements.txt`:
|
`requirements.txt`:
|
||||||
|
|
||||||
```
|
```
|
||||||
redis_store @ git+ssh://git@git.rethinkstudios.io/rethink-public/redis.git@v0.1.0
|
redis_store @ git+ssh://git@git.rethinkstudios.io/rethink-public/redis.git@v0.1.1
|
||||||
```
|
```
|
||||||
|
|
||||||
Direct:
|
Direct:
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
pip install "redis_store @ git+ssh://git@git.rethinkstudios.io/rethink-public/redis.git@v0.1.0"
|
pip install "redis_store @ git+ssh://git@git.rethinkstudios.io/rethink-public/redis.git@v0.1.1"
|
||||||
```
|
```
|
||||||
|
|
||||||
Pulls `redis>=5` (redis-py, which ships the asyncio client — **not** the dead standalone
|
Pulls `redis>=5` (redis-py, which ships the asyncio client — **not** the dead standalone
|
||||||
`aioredis`).
|
`aioredis`).
|
||||||
|
|
||||||
Drop the `@v0.1.0` suffix from the line above to install the latest unpinned.
|
Drop the `@v0.1.1` suffix from the line above to install the latest unpinned.
|
||||||
|
|
||||||
## Usage
|
## Usage
|
||||||
|
|
||||||
```python
|
```python
|
||||||
from redis_store import RedisStore
|
from redis_store import RedisDB
|
||||||
|
|
||||||
# construction is sync and opens no socket; connect() pings to fail loud on bad config
|
# construction is sync and opens no socket; connect() pings to fail loud on bad config
|
||||||
kv = await RedisStore(host="localhost", port=6379, db=0, password=None).connect()
|
kv = await RedisDB(host="localhost", port=6379, db=0, password=None).connect()
|
||||||
|
|
||||||
await kv.set("user:1:name", "ada", ex=3600) # ex = ttl seconds (optional)
|
await kv.set("user:1:name", "ada", ex=3600) # ex = ttl seconds (optional)
|
||||||
name = await kv.get("user:1:name") # "ada" (None if absent)
|
name = await kv.get("user:1:name") # "ada" (None if absent)
|
||||||
@ -50,7 +50,7 @@ await kv.close() # on shutdown
|
|||||||
Context-manager form:
|
Context-manager form:
|
||||||
|
|
||||||
```python
|
```python
|
||||||
async with RedisStore(host="localhost") as kv:
|
async with RedisDB(host="localhost") as kv:
|
||||||
await kv.incr("hits")
|
await kv.incr("hits")
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|||||||
@ -4,7 +4,7 @@ build-backend = "hatchling.build"
|
|||||||
|
|
||||||
[project]
|
[project]
|
||||||
name = "redis_store"
|
name = "redis_store"
|
||||||
version = "0.1.0"
|
version = "0.1.1"
|
||||||
description = "async redis wrapper over redis-py asyncio with a raw escape hatch, fail-loud and config-free"
|
description = "async redis wrapper over redis-py asyncio with a raw escape hatch, fail-loud and config-free"
|
||||||
requires-python = ">=3.10"
|
requires-python = ">=3.10"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
|
|||||||
@ -1,3 +1,3 @@
|
|||||||
from .redis_store import RedisStore
|
from .redis_store import RedisDB
|
||||||
|
|
||||||
__all__ = ["RedisStore"]
|
__all__ = ["RedisDB"]
|
||||||
|
|||||||
@ -2,14 +2,14 @@
|
|||||||
async redis wrapper over redis-py's asyncio client
|
async redis wrapper over redis-py's asyncio client
|
||||||
|
|
||||||
object pattern (one client per process), attach to the app:
|
object pattern (one client per process), attach to the app:
|
||||||
from redis_store import RedisStore
|
from redis_store import RedisDB
|
||||||
app.kv = await RedisStore(host="localhost", port=6379, db=0).connect()
|
app.kv = await RedisDB(host="localhost", port=6379, db=0).connect()
|
||||||
await app.kv.set("user:1:name", "ada", ex=3600)
|
await app.kv.set("user:1:name", "ada", ex=3600)
|
||||||
name = await app.kv.get("user:1:name") # "ada"
|
name = await app.kv.get("user:1:name") # "ada"
|
||||||
await app.kv.close() # on shutdown
|
await app.kv.close() # on shutdown
|
||||||
|
|
||||||
context manager:
|
context manager:
|
||||||
async with RedisStore(host="localhost") as kv:
|
async with RedisDB(host="localhost") as kv:
|
||||||
await kv.incr("hits")
|
await kv.incr("hits")
|
||||||
|
|
||||||
lifecycle:
|
lifecycle:
|
||||||
@ -45,7 +45,7 @@ from redis.exceptions import RedisError
|
|||||||
log = logging.getLogger(__name__)
|
log = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
class RedisStore:
|
class RedisDB:
|
||||||
"""async redis wrapper; one client per process, attach as app.kv"""
|
"""async redis wrapper; one client per process, attach as app.kv"""
|
||||||
|
|
||||||
def __init__(
|
def __init__(
|
||||||
@ -84,11 +84,11 @@ class RedisStore:
|
|||||||
)
|
)
|
||||||
self._client = redis.Redis(connection_pool=self._pool)
|
self._client = redis.Redis(connection_pool=self._pool)
|
||||||
|
|
||||||
async def connect(self) -> "RedisStore":
|
async def connect(self) -> "RedisDB":
|
||||||
"""open + validate the connection with a ping; fail loud on bad config
|
"""open + validate the connection with a ping; fail loud on bad config
|
||||||
|
|
||||||
returns self so callers can write
|
returns self so callers can write
|
||||||
`kv = await RedisStore(...).connect()`.
|
`kv = await RedisDB(...).connect()`.
|
||||||
"""
|
"""
|
||||||
try:
|
try:
|
||||||
await self._client.ping()
|
await self._client.ping()
|
||||||
@ -106,7 +106,7 @@ class RedisStore:
|
|||||||
log.exception("redis.close()")
|
log.exception("redis.close()")
|
||||||
raise
|
raise
|
||||||
|
|
||||||
async def __aenter__(self) -> "RedisStore":
|
async def __aenter__(self) -> "RedisDB":
|
||||||
return await self.connect()
|
return await self.connect()
|
||||||
|
|
||||||
async def __aexit__(self, exc_type, exc, tb) -> None:
|
async def __aexit__(self, exc_type, exc, tb) -> None:
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user