用于编排多个缓存层的缓存抽象层
项目描述
数据管道收集、存储和处理数据。这个包提供了一个促进这个过程的框架。
数据从DataSources收集,存储在DataSinks中,并使用Transformers进行处理。
数据源是向管道提供数据的实体。示例包括数据库、内存缓存和 REST API。
DataSinks是存储由DataSources提供的数据的实体。示例包括数据库和内存缓存。几乎所有数据接收器也将成为数据源,因为如果您无法将数据取出,则存储数据通常无济于事。我们将既是数据源又是数据接收器的实体称为数据存储。
转换器是将数据从一种数据类型转换或处理到另一种数据类型的实体。例如,转换器可以将 Word 文档转换为 PDF。
DataPipeline由一系列通过Transformers 通信的DataStore和DataSink组成。
数据源和接收器在数据管道中是有序的,它们的顺序决定了请求数据的顺序。一般来说,较慢的数据存储/接收器应该在管道的末端。
并非每个数据接收器或数据存储都需要支持每种数据类型。如果数据接收器/存储不支持请求的数据类型,则该数据接收器/源将在管道中简单地跳过。
例子
例如,如果您的数据管道由内存缓存、数据库和 REST API 服务(按此顺序)组成,当您执行查询时,DataPipeline将首先查找内存缓存,然后查找数据库,然后在 REST API 中。如果在缓存中找到数据,则将其返回,并且不会查询数据库和 REST API。同样,如果在数据库中找到数据,则不会查询 REST API。
在数据源中找到数据后,数据会沿数据管道从其来源传回。沿途遇到的任何数据接收器都将存储该数据。因此,继续上面的示例,如果您的查询是由 REST API 提供的,则返回的数据将存储在您的数据库中,然后存储在缓存中。数据接收器只会存储它支持的数据。
每个数据接收器都可以为其支持的每种数据类型定义过期期限,但这取决于要实现的特定数据接收器。
用法
下面的代码是简化的代码,用于说明将 SQL 数据库请求的 Word 文档转换为 PDF 文档的示例。
请注意,没有 PDF 文档存储在数据库中,但如果请求,数据管道仍然可以返回一个。
# The four classes below implement a simple DataPipeline. The code would need to be filled in by the user.
class WordDoc:
...
class PDF:
...
class SQLDatabase(DataSource, DataSink):
@get.register(WordDoc) # Tells the DataPipeline that this SQL database can provide a WordDoc
def get_word_doc(query: Dict[str, Any]) -> WordDoc:
"""Returns a WordDoc from an SQL database based on the `filename` in the query."""
@put.register(WordDoc) # Tell the DataPipeline that this SQL database can store a WordDoc
def put_word_doc(doc: WordDoc, query: Dict[str, Any]):
"""Stores the document in the SQL database using the query as an identifier."""
class DocumentTransformer(Transformer):
@transform.register(WordDoc, PDF) # Tells the DataPipeline that we know how to convert a WordDoc to a PDF
def Word_to_PDF(doc: WordDoc) -> PDF:
"""Converts a WordDoc to a PDF and returns the PDF."""
# The line of code below can now be used to request a PDF.
# The WordDoc with the filename `find_me` will be pulled from the SQL database then converted to a PDF and returned to the user.
my_pdf = pipeline.get(PDF, query={"filename": "find_me"})
# Note also that because we implemented a `put(WordDoc)` method in the SQLDatabase that it will also store WordDocs that pass through the SQL database via the pipeline but are not already in the database.
项目详情
datapipelines -1.0.7.tar.gz 的哈希值
| 算法 | 哈希摘要 | |
|---|---|---|
| SHA256 | 752bc71a1e03a45d723fc4d9b56c641ed0b872ed1df73b08896ee34d93c56e8f |
|
| MD5 | 537e875824fe2328a5fd4982ce946dfd |
|
| 布莱克2-256 | b486812f428f88d3b9e10be6178b54508580a2355ae67d50797e62ea47fef9c3 |