commit
e35257cf7b
4 changed files with 39 additions and 0 deletions
|
@ -119,6 +119,9 @@ print(andrew.pk)
|
||||||
# We can save the model to Redis by calling `save()`:
|
# We can save the model to Redis by calling `save()`:
|
||||||
andrew.save()
|
andrew.save()
|
||||||
|
|
||||||
|
# Expire the model after 2 mins (120 seconds)
|
||||||
|
andrew.expire(120)
|
||||||
|
|
||||||
# To retrieve this customer with its primary key, we use `Customer.get()`:
|
# To retrieve this customer with its primary key, we use `Customer.get()`:
|
||||||
assert Customer.get(andrew.pk) == andrew
|
assert Customer.get(andrew.pk) == andrew
|
||||||
```
|
```
|
||||||
|
|
|
@ -1130,6 +1130,15 @@ class RedisModel(BaseModel, abc.ABC, metaclass=ModelMeta):
|
||||||
async def save(self, pipeline: Optional[Pipeline] = None) -> "RedisModel":
|
async def save(self, pipeline: Optional[Pipeline] = None) -> "RedisModel":
|
||||||
raise NotImplementedError
|
raise NotImplementedError
|
||||||
|
|
||||||
|
async def expire(self, num_seconds: int, pipeline: Optional[Pipeline] = None):
|
||||||
|
if pipeline is None:
|
||||||
|
db = self.db()
|
||||||
|
else:
|
||||||
|
db = pipeline
|
||||||
|
|
||||||
|
# TODO: Wrap any Redis response errors in a custom exception?
|
||||||
|
await db.expire(self.make_primary_key(self.pk), num_seconds)
|
||||||
|
|
||||||
@validator("pk", always=True, allow_reuse=True)
|
@validator("pk", always=True, allow_reuse=True)
|
||||||
def validate_pk(cls, v):
|
def validate_pk(cls, v):
|
||||||
if not v:
|
if not v:
|
||||||
|
|
|
@ -553,6 +553,15 @@ andrew = Customer(
|
||||||
andrew.save()
|
andrew.save()
|
||||||
```
|
```
|
||||||
|
|
||||||
|
## Expiring Models
|
||||||
|
|
||||||
|
We can expire an instance of a model using `expire`, and passing it the number of seconds after which we want the instance to expire in Redis:
|
||||||
|
|
||||||
|
```python
|
||||||
|
# Expire Andrew in 2 minutes (120 seconds)
|
||||||
|
andrew.expire(120)
|
||||||
|
```
|
||||||
|
|
||||||
## Examining Your Data In Redis
|
## Examining Your Data In Redis
|
||||||
|
|
||||||
You can view the data stored in Redis for any Redis OM model.
|
You can view the data stored in Redis for any Redis OM model.
|
||||||
|
|
|
@ -391,6 +391,24 @@ async def test_delete(m):
|
||||||
assert response == 1
|
assert response == 1
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.asyncio
|
||||||
|
async def test_expire(m):
|
||||||
|
member = m.Member(
|
||||||
|
first_name="Expire",
|
||||||
|
last_name="Test",
|
||||||
|
email="e@example.com",
|
||||||
|
join_date=today,
|
||||||
|
age=93,
|
||||||
|
bio="This is a test user for expiry",
|
||||||
|
)
|
||||||
|
|
||||||
|
await member.save()
|
||||||
|
await member.expire(60)
|
||||||
|
|
||||||
|
ttl = await m.Member.db().ttl(member.key())
|
||||||
|
assert ttl > 0
|
||||||
|
|
||||||
|
|
||||||
def test_raises_error_with_embedded_models(m):
|
def test_raises_error_with_embedded_models(m):
|
||||||
class Address(m.BaseHashModel):
|
class Address(m.BaseHashModel):
|
||||||
address_line_1: str
|
address_line_1: str
|
||||||
|
|
Loading…
Reference in a new issue