Insert
Pydantic-redis can be used to insert new model instances into redis.
Create and register the Model
A model is a class that inherits from Model
with its _primary_key_field
attribute set.
In order for the store to know the existence of the given model,
register it using the register_model
method of Store
import pprint
from pydantic_redis import Model, Store, RedisConfig
class Book(Model):
_primary_key_field: str = "title"
title: str
author: str
if __name__ == "__main__":
pp = pprint.PrettyPrinter(indent=4)
store = Store(
name="some_name", redis_config=RedisConfig(), life_span_in_seconds=86400
)
store.register_model(Book)
Book.insert(Book(title="Oliver Twist", author="Charles Dickens"))
Book.insert(
Book(title="Great Expectations", author="Charles Dickens"),
life_span_seconds=1800,
)
Book.insert(
[
Book(title="Jane Eyre", author="Emily Bronte"),
Book(title="Pride and Prejudice", author="Jane Austen"),
]
)
Book.insert(
[
Book(title="Jane Eyre", author="Emily Bronte"),
Book(title="Pride and Prejudice", author="Jane Austen"),
],
life_span_seconds=3600,
)
response = Book.select()
pp.pprint(response)
Insert One Record
To add a single record to the redis instance, pass that model's instance as first argument to the model's insert
method
import pprint
from pydantic_redis import Model, Store, RedisConfig
class Book(Model):
_primary_key_field: str = "title"
title: str
author: str
if __name__ == "__main__":
pp = pprint.PrettyPrinter(indent=4)
store = Store(
name="some_name", redis_config=RedisConfig(), life_span_in_seconds=86400
)
store.register_model(Book)
Book.insert(Book(title="Oliver Twist", author="Charles Dickens"))
Book.insert(
Book(title="Great Expectations", author="Charles Dickens"),
life_span_seconds=1800,
)
Book.insert(
[
Book(title="Jane Eyre", author="Emily Bronte"),
Book(title="Pride and Prejudice", author="Jane Austen"),
]
)
Book.insert(
[
Book(title="Jane Eyre", author="Emily Bronte"),
Book(title="Pride and Prejudice", author="Jane Austen"),
],
life_span_seconds=3600,
)
response = Book.select()
pp.pprint(response)
Insert One Record With TTL
To make the record added to redis temporary, add a life_span_seconds
(Time To Live i.e. TTL) key-word argument
when calling the model's insert
method.
Info
When the life_span_seconds
argument is not specified, the life_span_in_seconds
passed to the store during
initialization is used.
The life_span_in_seconds
in both cases is None
by default. This means records never expire by default.
import pprint
from pydantic_redis import Model, Store, RedisConfig
class Book(Model):
_primary_key_field: str = "title"
title: str
author: str
if __name__ == "__main__":
pp = pprint.PrettyPrinter(indent=4)
store = Store(
name="some_name", redis_config=RedisConfig(), life_span_in_seconds=86400
)
store.register_model(Book)
Book.insert(Book(title="Oliver Twist", author="Charles Dickens"))
Book.insert(
Book(title="Great Expectations", author="Charles Dickens"),
life_span_seconds=1800,
)
Book.insert(
[
Book(title="Jane Eyre", author="Emily Bronte"),
Book(title="Pride and Prejudice", author="Jane Austen"),
]
)
Book.insert(
[
Book(title="Jane Eyre", author="Emily Bronte"),
Book(title="Pride and Prejudice", author="Jane Austen"),
],
life_span_seconds=3600,
)
response = Book.select()
pp.pprint(response)
Insert Many Records
To add many records to the redis instance, pass a list of that model's instances as first argument to the model's
insert
method.
Info
Adding many records at once is more performant than adding one record at a time repeatedly because less network requests are made in the former.
import pprint
from pydantic_redis import Model, Store, RedisConfig
class Book(Model):
_primary_key_field: str = "title"
title: str
author: str
if __name__ == "__main__":
pp = pprint.PrettyPrinter(indent=4)
store = Store(
name="some_name", redis_config=RedisConfig(), life_span_in_seconds=86400
)
store.register_model(Book)
Book.insert(Book(title="Oliver Twist", author="Charles Dickens"))
Book.insert(
Book(title="Great Expectations", author="Charles Dickens"),
life_span_seconds=1800,
)
Book.insert(
[
Book(title="Jane Eyre", author="Emily Bronte"),
Book(title="Pride and Prejudice", author="Jane Austen"),
]
)
Book.insert(
[
Book(title="Jane Eyre", author="Emily Bronte"),
Book(title="Pride and Prejudice", author="Jane Austen"),
],
life_span_seconds=3600,
)
response = Book.select()
pp.pprint(response)
Insert Many Records With TTL
To add temporary records to redis, add a life_span_seconds
(Time To Live i.e. TTL) argument
when calling the model's insert
method.
Info
When the life_span_seconds
argument is not specified, the life_span_in_seconds
passed to the store during
initialization is used.
The life_span_in_seconds
in both cases is None
by default. This means records never expire by default.
import pprint
from pydantic_redis import Model, Store, RedisConfig
class Book(Model):
_primary_key_field: str = "title"
title: str
author: str
if __name__ == "__main__":
pp = pprint.PrettyPrinter(indent=4)
store = Store(
name="some_name", redis_config=RedisConfig(), life_span_in_seconds=86400
)
store.register_model(Book)
Book.insert(Book(title="Oliver Twist", author="Charles Dickens"))
Book.insert(
Book(title="Great Expectations", author="Charles Dickens"),
life_span_seconds=1800,
)
Book.insert(
[
Book(title="Jane Eyre", author="Emily Bronte"),
Book(title="Pride and Prejudice", author="Jane Austen"),
]
)
Book.insert(
[
Book(title="Jane Eyre", author="Emily Bronte"),
Book(title="Pride and Prejudice", author="Jane Austen"),
],
life_span_seconds=3600,
)
response = Book.select()
pp.pprint(response)
Run the App
Running the above code in a file main.py
would produce:
Tip
Probably FLUSHALL redis first